Figures Sample C++ Project

Transkrypt

Figures Sample C++ Project
Figures Sample C++ Project
CImage
CFigure
colorPen
colorBrush
Draw
Store
Load
Draw
Store
Load
CPoint
x
y
CRectangle
CLine
CEllipse
CTriangle
Draw
Draw
Draw
Draw
Store
Load
////////////////////////////////////////////////////////////////////////////
// Figure.h Definition File
////////////////////////////////////////////////////////////////////////////
class CFigure
{
public:
CFigure(CPoint upperLeft, CPoint bottomRight);
CFigure();
protected:
CPoint m_ptUpperLeft;
CPoint m_ptBottomRight;
COLORREF m_colorPen;
COLORREF m_colorBrush;
// position & size
// colors
public:
COLORREF GetPenColor()
{ return m_colorPen; }
void SetPenColor(COLORREF c)
{ m_colorPen = c; }
void SetPenColor(BYTE r, BYTE g, BYTE b)
{ m_colorPen = RGB(r, g, b); }
COLORREF GetBrushColor()
{ return m_colorBrush; }
void SetBrushColor(COLORREF c)
{ m_colorBrush = c; }
void SetBrushColor(BYTE r, BYTE g, BYTE b){ m_colorBrush = RGB(r, g, b); }
public:
virtual void Draw(CGC&) = 0;
virtual void Store(CArchive&);
virtual void Load(CArchive&);
// storing & loading operators
friend CArchive &operator <<(CArchive &ar, CFigure &f);
friend CArchive &operator >>(CArchive &ar, CFigure &f);
};
class CRectangle : public CFigure
{
public:
CRectangle(CPoint upperLeft, CPoint bottomRight);
CRectangle();
virtual void Draw(CGC&);
};
class CLine : public CFigure
{
public:
CLine(CPoint upperLeft, CPoint bottomRight);
CLine();
virtual void Draw(CGC&);
};
class CEllipse : public CFigure
{
public:
CEllipse(CPoint upperLeft, CPoint bottomRight);
CEllipse();
virtual void Draw(CGC&);
};
class CTriangle : public CFigure
{
public:
CTriangle(CPoint upperLeft, CPoint bottomRight, CPoint inside);
CTriangle();
protected:
CPoint m_ptInside;
// 3rd point defines the triangle
public:
virtual void Draw(CGC&);
virtual void Store(CArchive&);
virtual void Load(CArchive&);
};
class CImage
{
public:
CImage();
~CImage();
protected:
struct NODE
// used to build a list of figures
{
NODE(CFigure *pF) : pFig(pF) { pNext = NULL; }
CFigure *pFig;
// ptr to the figure
NODE *pNext;
// ptr to the next item
};
struct NODE *m_pList;
// The List of Figures (Image Items)
public:
void Add(CFigure*);
void Delete(CFigure*);
void Draw(CGC&);
void Store(CArchive&);
void Load(CArchive&);
};
////////////////////////////////////////////////////////////////////////////
// Figure.cpp Implementation File
////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// class CFigure
CFigure::CFigure(CPoint upperLeft, CPoint bottomRight)
: m_ptUpperLeft(upperLeft), m_ptBottomRight(bottomRight)
{
m_colorPen = RGB(0, 0, 0);
// black
m_colorBrush = RGB(255, 255, 255); // white
}
CFigure::CFigure() { }
void CFigure::Store(CArchive &ar)
{
// store all the items
ar << m_ptUpperLeft;
ar << m_ptBottomRight;
ar << m_colorPen;
ar << m_colorBrush;
}
void CFigure::Load(CArchive &ar)
{
// load all the items
ar >> m_ptUpperLeft;
ar >> m_ptBottomRight;
ar >> m_colorPen;
ar >> m_colorBrush;
}
CArchive &operator <<(CArchive &ar, CFigure &f)
{
f.Store(ar);
return ar;
}
CArchive &operator >>(CArchive &ar, CFigure &f)
{
f.Load(ar);
return ar;
}
/////////////////////////////////////////////////////////////////////////////
// class CRectangle : public CFigure
CRectangle::CRectangle(CPoint upperLeft, CPoint bottomRight)
: CFigure(upperLeft, bottomRight)
{
}
CRectangle::CRectangle()
{ }
void CRectangle::Draw(CGC &gc)
{
gc.SetPenColor(m_colorPen);
gc.SetBrushColor(m_colorBrush);
gc.Rectangle(m_ptUpperLeft, m_ptBottomRight);
}
/////////////////////////////////////////////////////////////////////////////
// class CLine : public CFigure
CLine::CLine(CPoint upperLeft, CPoint bottomRight)
: CFigure(upperLeft, bottomRight)
{
}
CLine::CLine()
{ }
void CLine::Draw(CGC &gc)
{
gc.SetPenColor(m_colorPen);
gc.SetBrushColor(m_colorBrush);
gc.MoveTo(m_ptUpperLeft);
gc.LineTo(m_ptBottomRight);
}
/////////////////////////////////////////////////////////////////////////////
// class CEllipse : public CFigure
CEllipse::CEllipse(CPoint upperLeft, CPoint bottomRight)
: CFigure(upperLeft, bottomRight)
{
}
CEllipse::CEllipse()
{ }
void CEllipse::Draw(CGC &gc)
{
gc.SetPenColor(m_colorPen);
gc.SetBrushColor(m_colorBrush);
gc.Ellipse(m_ptUpperLeft, m_ptBottomRight);
}
/////////////////////////////////////////////////////////////////////////////
// class CTriangle : public CFigure
CTriangle::CTriangle(CPoint upperLeft, CPoint bottomRight, CPoint inside)
: CFigure(upperLeft, bottomRight), m_ptInside(inside)
{
}
CTriangle::CTriangle() { }
void CTriangle::Draw(CGC &gc)
{
gc.SetPenColor(m_colorPen);
gc.SetBrushColor(m_colorBrush);
gc.Triangle(m_ptUpperLeft, CPoint(m_ptBottomRight.x, m_ptInside.y),
CPoint(m_ptInside.x, m_ptBottomRight.y));
}
void CTriangle::Store(CArchive &ar)
{
CFigure::Store(ar);
// store standard members
ar << m_ptInside;
// store a special member for triangles
}
void CTriangle::Load(CArchive &ar)
{
CFigure::Load(ar);
// load standard members
ar >> m_ptInside;
// load a special member for triangles
}
/////////////////////////////////////////////////////////////////////////////
// class CImage
CImage::CImage()
{
// initialization: make an empty list
m_pList = NULL;
}
CImage::~CImage()
{
// destroy the list and all the figures
while (m_pList)
{
NODE *p = m_pList;
m_pList = m_pList->pNext;
delete p->pFig;
delete p;
}
}
void CImage::Add(CFigure* pFig)
{
// add a new node
if (m_pList == NULL)
m_pList = new NODE(pFig);
else
{
NODE *p = m_pList;
while (p->pNext != NULL) // browse to the end of the list
p = p->pNext;
p->pNext = new NODE(pFig);
}
}
void CImage::Draw(CGC &gc)
{
// call Draw for each image item
for (NODE *p = m_pList; p != NULL; p = p->pNext)
p->pFig->Draw(gc);
}
void CImage::Store(CArchive &ar)
{
// ...
}
void CImage::Load(CArchive &ar)
{
// ...
}