the program can run, but when I use Rectangle to draw something, it will pop up a Debug Assertion Failed window.
but if I don't add
CPen aPen
in the base class, I use like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
void CRectangle::Draw(CDC* pDC)
{
Cpen aPen; // create in the function
if(!aPen.CreatePen(PS_SOLID, m_PenWidth, m_Color))
{
// abort stuff
}
// ...drawing stuff
}
[/quote]
I'm wonder why? I have to put the CreatePen in the constructor?
plus, I want to know how to implement the program have select pen and brush style function, can you give me some tip about how to do it?
I have a element.h, element.cpp store the shape, and a elementconstant.h store the COLORREF and some variable stuff.
// this is the function name of my CLine class, the style thing stucks me, I don't know how to handle it.
I've downloaded it and will look at it later today -
but I'm sure it will come down to the usual resource management issue - in that
You cannot repeatly call CreatePen on the same CPen object without first
detaching the previous pen .
But that we mean:
1 2 3 4
CPen somePen;
somePen.CreatePen( /*example blue pen*/);
//do something with the blue pen
somePen.CreatePen(/*say red pen*/); //error somePen is still associated with blue pen
I'm sure that was the problem when you had the Cpen as part of the CElement classs.
You were probably calling the Draw function (for example for a CLine) which was repeatedly calling CreatePen on the Cpen object in the CElement base class.
The crash doesn't happen when you put a Cpen in the Draw function, because each time the Draw function is called, a new Cpen is created ecah time.
**why is this thread in the Linux forum ??*
EDIT:
You a two choices really:
Choice 1:
Continue the way you have it now - which is creating the pen in the Draw function.
Choice 2
If you want a pen as part of the class - then make a CPen member variable part of the CElement class - create the pen as part of the constructor of the subclasses - and in the Draw function just use the pen (no need to create another pen).
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class CElement : public CObject
{
protected:
int m_PenWidth;
COLORREF m_Color;
CRect m_EnclosingRect;
CPen m_ThePen; //<<===================
public:
virtual ~CElement() {};
virtualvoid Draw(CDC* pDC) {};
CRect GetBoundRect()const;
protected: // make it accessible for inheritance
CElement() {} ;
};