I wirte a simple paint project, and the CPen usage puzzles me.
CElement is the base class, I derived CLine, CRectangle, CRect..., overwirtes the Draw(CDC* pDC) function.
if I add a private data member [quote]CPen aPen; |
in the base class, then I use the in the Draw, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void CRectangle::Draw(CDC* pDC)
{
if(!aPen.CreatePen(PS_SOLID, m_PenWidth, m_Color))
{
AfxMessageBox(TEXT("Creating pen failed"), MB_ICONERROR);
AfxAbort();
}
// ...drawing stuff
}
|
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
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.
1 2
|
virtual void Draw(CDC* pDC);
CLine(const CPoint& first, const CPoint& end, COLORREF penColor, COLORREF, brushColor);
|