arjacsoh
arjacsoh

Reputation: 9232

Rectangle in MFC

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);  

Upvotes: 1

Views: 12345

Answers (3)

karthik
karthik

Reputation: 17832

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.

Upvotes: 0

fefe
fefe

Reputation: 3432

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.

Upvotes: 1

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.

Upvotes: 2

Related Questions