

class CShape:public CObject 
...{
protected:
COLORREF m_pColor; //线条颜色
int m_pWidth;//线条宽度
int m_pStyle;//线条样式
CShape()...{}
DECLARE_SERIAL(CShape)
public:
virtual void Drawing(CDC* pDC)...{}//画图操作
virtual void Serialize(CArchive& ar);//
virtual ~CShape();
};
CClientDC dc(this);
OnPrepareDC(&dc);
dc.DPtoLP(&point);//滚动条坐标转换代码
dc.SelectStockObject(NULL_BRUSH);
SetCursor(m_HCursor);
if(m_Drag)...{
CDooDleDoc* pDoc = GetDocument();
CPen NewPen,*pOldPen;
switch(pDoc->m_pStyle)
...{
case PEN_STYLE_SOLID:
NewPen.CreatePen(PS_SOLID,pDoc->m_pWidth,pDoc->m_pColor);
Break;
//…………….
}
pOldPen = dc.SelectObject(&NewPen);
dc.SetROP2(R2_NOT);

if(pDoc->CurrentDraw == DRAW_LINE)...{//直线
dc.MoveTo(m_pOrigin);
dc.LineTo(m_pPrev);//擦掉上一次画的线
dc.MoveTo(m_pOrigin);
dc.LineTo(point);//画线
}
if(pDoc->CurrentDraw == DRAW_RECTANGLE)...{//矩形
//……………………….
void CDooDleDoc::OnEditUndo() 
...{
// TODO: Add your command handler code here
int index = m_ObArray.GetUpperBound();
if(index>=0 && m_pModifiedCount > 0)...{ //m_pModifiedCount为撤消标志
delete m_ObArray.GetAt(index);
m_ObArray.RemoveAt(index);
m_pModifiedCount--;
}
UpdateAllViews(NULL);
SetModifiedFlag();
}
void CDooDleDoc::Serialize(CArchive& ar) //序列化
...{
if (ar.IsStoring())
...{
// TODO: add storing code here
m_ObArray.Serialize(ar);
}
else
...{
// TODO: add loading code here
m_ObArray.Serialize(ar);
}
}
void CEllipse::Serialize(CArchive& ar)
...{
if(ar.IsStoring())...{
ar<<m_pFirstCorner.x<<m_pFirstCorner.y<<m_pSecondCorner.x
<<m_pSecondCorner.y;
}
else...{
ar>>m_pFirstCorner.x>>m_pFirstCorner.y>>m_pSecondCorner.x
>>m_pSecondCorner.y;
}
CShape::Serialize(ar);
}
void CDooDleDoc::AddShape(CShape* pShape)
...{
//CLine* pLine = new CLine(pStart,pEnd,Width,Style,Color);
m_ObArray.Add(pShape);
SetModifiedFlag();
m_pModifiedCount++;
}

class CLine:public CShape 
...{
protected:
CPoint m_pStart;//起点
CPoint m_pEnd;//终点
DECLARE_SERIAL(CLine)//序列化宏
CLine()...{}
public:
CLine(CPoint pStart,CPoint pEnd,int Width,int Style,COLORREF Color);
void Drawing(CDC* pDC);//画直线
virtual void Serialize(CArchive& ar);
virtual ~CLine();
};
void CDooDleView::OnLButtonUp(UINT nFlags, CPoint point) 
...{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
OnPrepareDC(&dc);
dc.DPtoLP(&point);//滚动条坐标转换代码
dc.SelectStockObject(NULL_BRUSH);

if(m_Drag)...{
m_Drag = 0;
ReleaseCapture();
ClipCursor(NULL);
CDooDleDoc* pDoc = GetDocument();
CShape* pShape;
if(pDoc->CurrentDraw == DRAW_LINE)...{//直线
pShape = new CLine(m_pOrigin,point,pDoc->m_pWidth,pDoc->m_pStyle,pDoc->m_pColor);//所画直线数据
}
if(pDoc->CurrentDraw == DRAW_RECTANGLE)...{//矩形
pShape = new CRectangle(m_pOrigin,point,pDoc->m_pWidth,pDoc->m_pStyle,pDoc->m_pColor);
}
if(pDoc->CurrentDraw == DRAW_ROUNDRECT)...{//圆矩形
int r = (abs(m_pOrigin.x-point.x)+abs(m_pOrigin.y-point.y))/6;
pShape = new CRoundRect(m_pOrigin,point,r,pDoc->m_pWidth,pDoc->m_pStyle,pDoc->m_pColor);
}
if(pDoc->CurrentDraw == DRAW_FILLRECT)...{//填充矩形
pShape = new CFillRect(m_pOrigin,point,pDoc->m_pWidth,pDoc->m_pStyle,pDoc->m_pColor,
pDoc->m_pFColor,pDoc->m_pHatch,pDoc->m_pHatchFlag);
}
if(pDoc->CurrentDraw == DRAW_CIRCLE)...{//圆
pShape = new CCircle(m_pOrigin,m_pPrev,pDoc->m_pWidth,pDoc->m_pStyle,pDoc->m_pColor);
}
if(pDoc->CurrentDraw == DRAW_ELLIPSE)...{//椭圆
pShape = new CEllipse(m_pOrigin,m_pPrev,pDoc->m_pWidth,pDoc->m_pStyle,pDoc->m_pColor);
}
pDoc->AddShape(pShape);//保存图形数据
pDoc->UpdateAllViews(NULL);
}
CScrollView::OnLButtonUp(nFlags, point);
}

欢迎大家评论!