1
votes

I try to draw a rectangle on a mfc window using the instructions by: http://msdn.microsoft.com/en-US/library/8w4fzfxf%28v=VS.80%29.aspx . Much though I tried, the Rectangle appears on the border of the window covering the whole of it. What is the problem with the following code int the function OnDraw(CDC* pDC) ? What can be done to draw a Rectangle with particular coordinates in the window?

 CPen penBlack;
 penBlack.CreatePen(PS_SOLID, 3, RGB(0, 0, 0));
 CPen* pOldPen = pDC->SelectObject(&penBlack);

 CPoint pt(10, 10);
 CSize sz(100, 50);
 CRect myRect(pt, sz);
 GetClientRect(&myRect);

 pDC->Rectangle(&myRect);  
3
Why do you call GetClientRect? It would seem that this call overwrites your previously set-up coordinates in myRect.stakx - no longer contributing
You must be right, but it was suggested in the instructions.arjacsoh
You haven't copied the instructions exactly. You've just copied and pasted parts in random order. It retrieves the client rectangle, then shrinks it by 20 pixels in each direction, then draws that rectangle.Cody Gray

3 Answers

2
votes

Drop the call to GetClientRect.

That function will write to the rectangle object passed to it, so by calling, you're overwriting your specific coordinates that you set up just before the call using pt and sz.

1
votes

As @stakx suggested you should remove the GetClientRect, which gets the whole window client area, and overwrites your own rectangle.

As to the instruction, it first gets the whole client area, and shrinks the rectangle to get the rectangle to draw, so GetClientRect is needed there.

0
votes

This site will help you to draw the rectangle in mfc Dialog-based-application.

http://cboard.cprogramming.com/windows-programming/37788-drawing-mfc.html

http://cboard.cprogramming.com/cplusplus-programming/102490-cplusplus-mfc-rectangle-class.html

Don't use GetClientRect().It will override your previous coordinates.