Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drawing Shapes in MFC

Status
Not open for further replies.

snailophone

Programmer
Nov 13, 2004
2
US
I am trying to write a simle program in MFC (MSVC++ 2003 .NET) that prints a rectangle to the dialog box. Thats all. I created a class to do this:

//------rectangle class definition---------------
class DrawRectangle : public CDialog
{
private:
CRect rect;
CClientDC *pDC;
CPoint A, B, C, D;

public:
void DrawRect();
};
//--------------------end class def.------------

And here is the DrawRect() function:

//-------------start function def.---------------
void DrawRectangle:DrawRect()
{
GetClientRect(&rect);
pDC = new CClientDC(this);
pDC->Rectangle(0, 0, 100, 100);
delete pDC;
}
//----------------end function def-------------

And here is the call to DrawRect():

//-----------------start call------------------
DrawRectangle *wawa = new DrawRectangle;
wawa->DrawRect();
//--------------------end call----------------

I get a "Debug Assertation Failed" error when I click the button I made that calls DrawRect(). Any ideas? Thanks.

 
First, never dynamically allocate memory when there's no reason to, as you are doing here. Second, you should use GetDC() and ReleaseDC() to get the DC for drawing. Also, if DrawRectangle is not an actual dialog as it appears to be, you should NOT be inheriting from CDialog.

Actually, it makes no sense to have a class for this at all if this is all it does (unless it's really a dialog and you just chose not to show all of the other methods and member variables).
 
How about the "Debug Assertation Failed" error? The code above compiles and runs normally when I put it in the (default) CxxxxxDlg class, but when I try to put it in a separate class, I get the error. Thanks again.
 
Of course it does, because it's expecting the DrawRectangle object to be a created dialog with a valid non-NULL HWND but it doesn't have one.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top