drawDoc.cpp
资源名称:GDIUSE.rar [点击查看]
上传用户:jinlangri
上传日期:2022-07-17
资源大小:10774k
文件大小:10k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- // drawDoc.cpp : implementation of the CDrawDoc class
- //
- #include "stdafx.h"
- #include "draw.h"
- #include "drawDoc.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc
- IMPLEMENT_DYNCREATE(CDrawDoc, CDocument)
- BEGIN_MESSAGE_MAP(CDrawDoc, CDocument)
- //{{AFX_MSG_MAP(CDrawDoc)
- ON_COMMAND(ID_EDIT_CLEARALL, OnEditClearAll)
- ON_UPDATE_COMMAND_UI(ID_EDIT_CLEARALL, OnUpdateEditClearAll)
- ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
- ON_UPDATE_COMMAND_UI(ID_EDIT_UNDO, OnUpdateEditUndo)
- //}}AFX_MSG_MAP
- ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
- ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc construction/destruction
- CDrawDoc::CDrawDoc()
- {
- // TODO: add one-time construction code here
- }
- CDrawDoc::~CDrawDoc()
- {
- }
- BOOL CDrawDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc serialization
- void CDrawDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- m_DrawArray.Serialize(ar);
- }
- else
- {
- m_DrawArray.Serialize(ar);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc diagnostics
- #ifdef _DEBUG
- void CDrawDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CDrawDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CDrawDoc commands
- void CDrawDoc::AddDraw(CDrawBase *pDraw)
- {
- m_DrawArray.Add(pDraw);
- SetModifiedFlag();
- }
- CDrawBase *CDrawDoc::GetDraw(int Index)
- {
- if(Index<0||Index>m_DrawArray.GetUpperBound())
- return 0;
- return (CDrawBase *)m_DrawArray.GetAt(Index);
- }
- int CDrawDoc::GetNumDraws()
- {
- return m_DrawArray.GetSize();
- }
- //CDrawBase类的实现
- IMPLEMENT_SERIAL(CDrawBase, CObject, 1)
- CRect CDrawBase::GetDimRect()
- {
- return CRect(min(m_X1, m_X2),
- min(m_Y1, m_Y2),
- max(m_X1, m_X2) + 1,
- max(m_Y1, m_Y2) + 1);
- }
- void CDrawBase::Serialize(CArchive& ar)
- {
- if(ar.IsStoring())
- ar << m_X1 << m_Y1 << m_X2 << m_Y2 << m_Color;
- else
- ar >> m_X1 >> m_Y1 >> m_X2 >> m_Y2 >> m_Color;
- }
- //CLine类的实现代码
- IMPLEMENT_SERIAL(CLine, CDrawBase, 1)
- CLine::CLine(int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness)
- {
- m_X1 = X1;
- m_Y1 = Y1;
- m_X2 = X2;
- m_Y2 = Y2;
- m_Color = Color;
- m_Thickness = Thickness;
- }
- void CLine::Serialize(CArchive& ar)
- {
- CDrawBase::Serialize(ar);
- if(ar.IsStoring())
- ar << m_Thickness;
- else
- ar >> m_Thickness;
- }
- void CLine::Draw(CDC *pDC)
- {
- CPen Pen, *pOldPen;
- Pen.CreatePen(PS_SOLID, m_Thickness, m_Color);
- pOldPen = pDC->SelectObject(&Pen);
- pDC->MoveTo(m_X1, m_Y1);
- pDC->LineTo(m_X2, m_Y2);
- pDC->SelectObject(pOldPen);
- }
- //CRectangle类的实现代码
- IMPLEMENT_SERIAL(CRectangle, CDrawBase, 1)
- //数据成员初始化
- CRectangle::CRectangle(int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness)
- {
- m_X1 = X1;
- m_Y1 = Y1;
- m_X2 = X2;
- m_Y2 = Y2;
- m_Color = Color;
- m_Thickness = Thickness;
- }
- void CRectangle::Serialize(CArchive& ar)
- {
- CDrawBase::Serialize(ar); //基类数据成员序列化
- if(ar.IsStoring()) //m_Thickness序列化
- ar << m_Thickness;
- else
- ar >> m_Thickness;
- }
- void CRectangle::Draw(CDC *pDC)
- {
- CPen Pen, *pOldPen;
- Pen.CreatePen(PS_INSIDEFRAME, m_Thickness, m_Color);//创建画笔
- pOldPen = pDC->SelectObject(&Pen); //选入画笔,并保存原来的画笔
- //选入库存画刷,并保存原来的画刷
- CBrush *pOldBrush = (CBrush *)pDC->SelectStockObject(NULL_BRUSH);
- //绘制矩形
- pDC->Rectangle(m_X1,m_Y1,m_X2,m_Y2);
- //恢复原来的画笔和画刷
- pDC->SelectObject(pOldPen);
- pDC->SelectObject(pOldBrush);
- }
- //CRectFill类的实现代码
- IMPLEMENT_SERIAL(CRectFill, CDrawBase, 1)
- CRectFill::CRectFill(int X1, int Y1, int X2, int Y2, COLORREF Color)
- {
- m_X1 = X1;
- m_Y1 = Y1;
- m_X2 = X2;
- m_Y2 = Y2;
- m_Color = Color;
- }
- void CRectFill::Draw(CDC *pDC)
- {
- CBrush Brush, *pOldBrush;
- CPen Pen, *pOldPen;
- //创建并选入画笔
- Pen.CreatePen(PS_INSIDEFRAME, 1, m_Color);
- pOldPen = pDC->SelectObject(&Pen);
- //创建并选入画刷
- Brush.CreateSolidBrush(m_Color);
- pOldBrush = pDC->SelectObject(&Brush);
- //绘制填充矩形
- pDC->Rectangle(m_X1, m_Y1, m_X2, m_Y2);
- //恢复原来的画笔和画刷
- pDC->SelectObject(pOldPen);
- pDC->SelectObject(pOldBrush);
- }
- //CRectRound类的实现代码
- IMPLEMENT_SERIAL(CRectRound,CDrawBase,1)
- CRectRound::CRectRound(int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness)
- {
- m_X1 = min(X1, X2);
- m_Y1 = min(Y1, Y2);
- m_X2 = max(X1, X2);
- m_Y2 = max(Y1, Y2);
- m_Color = Color;
- m_Thickness = Thickness;
- }
- void CRectRound::Serialize(CArchive& ar)
- {
- CDrawBase::Serialize(ar);//基类数据成员序列化
- if(ar.IsStoring()) //m_Thickness序列化
- ar << m_Thickness;
- else
- ar >> m_Thickness;
- }
- void CRectRound::Draw(CDC *pDC)
- {
- CPen Pen, *pOldPen;
- //创建并选入画笔
- Pen.CreatePen(PS_INSIDEFRAME,m_Thickness,m_Color);
- pOldPen = pDC->SelectObject(&Pen);
- //选入库存画刷,并保存原来的画刷
- CBrush *pOldBrush = (CBrush *)pDC->SelectStockObject(NULL_BRUSH);
- int SizeRound = (m_X2 - m_X1 + m_Y2 - m_Y1)/6;//计算圆角尺寸
- //绘制圆角矩形
- pDC->RoundRect(m_X1, m_Y1, m_X2, m_Y2, SizeRound, SizeRound);
- //恢复原来的画笔和画刷
- pDC->SelectObject(pOldPen);
- pDC->SelectObject(pOldBrush);
- }
- //CRectRoundFill类的实现代码
- IMPLEMENT_SERIAL(CRectRoundFill, CDrawBase, 1)
- CRectRoundFill::CRectRoundFill(int X1, int Y1, int X2, int Y2, COLORREF Color)
- {
- m_X1 = min(X1, X2);
- m_Y1 = min(Y1, Y2);
- m_X2 = max(X1, X2);
- m_Y2 = max(Y1, Y2);
- m_Color = Color;
- }
- void CRectRoundFill::Draw(CDC *pDC)
- {
- CBrush Brush, *pOldBrush;
- CPen Pen, *pOldPen;
- //创建并选入画笔和画刷
- Pen.CreatePen(PS_INSIDEFRAME, 1, m_Color);
- pOldPen = pDC->SelectObject(&Pen);
- Brush.CreateSolidBrush(m_Color);
- pOldBrush = pDC->SelectObject(&Brush);
- int SizeRound = (m_X2 - m_X1 + m_Y2 - m_Y1)/6;//计算圆角尺寸
- //绘制填充圆角矩形
- pDC->RoundRect(m_X1, m_Y1, m_X2, m_Y2, SizeRound, SizeRound);
- //恢复原来的画笔和画刷
- pDC->SelectObject(pOldPen);
- pDC->SelectObject(pOldBrush);
- }
- //CCircle类的实现代码
- IMPLEMENT_SERIAL(CCircle, CDrawBase, 1)
- CCircle::CCircle(int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness)
- {
- m_X1 = min(X1, X2);
- m_Y1 = min(Y1, Y2);
- m_X2 = max(X1, X2);
- m_Y2 = max(Y1, Y2);
- m_Color = Color;
- m_Thickness = Thickness;
- }
- void CCircle::Serialize(CArchive& ar)
- {
- CDrawBase::Serialize(ar);//基类数据成员序列化
- if(ar.IsStoring()) //m_Thickness序列化
- ar << m_Thickness;
- else
- ar >> m_Thickness;
- }
- void CCircle::Draw(CDC *pDC)
- {
- CPen Pen, *pOldPen;
- Pen.CreatePen(PS_INSIDEFRAME, m_Thickness, m_Color);//创建画笔
- pOldPen = pDC->SelectObject(&Pen); //选入画笔,并保存原来的画笔
- //选入库存画刷,并保存原来的画刷
- CBrush *pOldBrush = (CBrush *)pDC->SelectStockObject(NULL_BRUSH);
- //绘制圆或椭圆
- pDC->Ellipse(m_X1, m_Y1, m_X2, m_Y2);
- //恢复原来的画笔和画刷
- pDC->SelectObject(pOldPen);
- pDC->SelectObject(pOldBrush);
- }
- //CCircleFill类的实现代码
- IMPLEMENT_SERIAL(CCircleFill, CDrawBase, 1)
- CCircleFill::CCircleFill(int X1, int Y1, int X2, int Y2, COLORREF Color)
- {
- m_X1 = min(X1, X2);
- m_Y1 = min(Y1, Y2);
- m_X2 = max(X1, X2);
- m_Y2 = max(Y1, Y2);
- m_Color = Color;
- }
- void CCircleFill::Draw(CDC *pDC)
- {
- CPen Pen, *pOldPen;
- CBrush Brush, *pOldBrush;
- //创建并选入画笔和画刷
- Pen.CreatePen(PS_INSIDEFRAME, 1, m_Color);
- pOldPen = pDC->SelectObject(&Pen);
- Brush.CreateSolidBrush(m_Color);
- pOldBrush = pDC->SelectObject(&Brush);
- //绘制填充圆或椭圆
- pDC->Ellipse(m_X1, m_Y1, m_X2, m_Y2);
- //恢复原来的画笔和画刷
- pDC->SelectObject(pOldPen);
- pDC->SelectObject(pOldBrush);
- }
- //CText类的实现代码
- IMPLEMENT_SERIAL(CText, CDrawBase, 1)
- CText::CText(int X1, int Y1, int X2, int Y2, COLORREF Color, LOGFONT lf, CString str)
- {
- m_X1 = min(X1, X2);
- m_Y1 = min(Y1, Y2);
- m_X2 = max(X1, X2);
- m_Y2 = max(Y1, Y2);
- m_Color = Color;
- m_lf = lf;
- m_String = str;
- }
- void CText::Serialize(CArchive &ar)
- {
- CDrawBase::Serialize(ar);//基类数据成员序列化
- if(ar.IsStoring()) //m_lf和m_String的序列化
- {
- ar.Write(&m_lf, sizeof(m_lf));
- ar << m_String;
- }
- else
- { ar.Read(&m_lf, sizeof(m_lf));
- ar >> m_String;
- }
- }
- void CText::Draw(CDC *pDC)
- {
- CFont Font, *pOldFont;
- //创建并选入字体
- Font.CreateFontIndirect(&m_lf);
- pOldFont = pDC->SelectObject(&Font);
- //设置字体颜色
- pDC->SetTextColor(m_Color);
- //设置字体背景透明
- pDC->SetBkMode(TRANSPARENT);
- //输出字体
- pDC->TextOut(m_X1, m_Y1, m_String);
- //恢复原来的字体
- pDC->SelectObject(pOldFont);
- }
- void CDrawDoc::DeleteContents()
- {
- // TODO: Add your specialized code here and/or call the base class
- int Index = m_DrawArray.GetSize();
- while(Index--)
- delete m_DrawArray.GetAt(Index);
- m_DrawArray.RemoveAll();
- CDocument::DeleteContents();
- }
- void CDrawDoc::OnEditClearAll()
- {
- // TODO: Add your command handler code here
- DeleteContents();
- UpdateAllViews(NULL);
- SetModifiedFlag();
- }
- void CDrawDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->Enable(m_DrawArray.GetSize());
- }
- void CDrawDoc::OnEditUndo()
- {
- // TODO: Add your command handler code here
- int Index = m_DrawArray.GetUpperBound();
- if(Index > -1)
- {
- delete m_DrawArray.GetAt(Index);
- m_DrawArray.RemoveAt(Index);
- }
- UpdateAllViews(NULL);
- SetModifiedFlag();
- }
- void CDrawDoc::OnUpdateEditUndo(CCmdUI* pCmdUI)
- {
- // TODO: Add your command update UI handler code here
- pCmdUI->Enable(m_DrawArray.GetSize());
- }