GraphicView.cpp
上传用户:herolh
上传日期:2022-07-18
资源大小:3590k
文件大小:7k
源码类别:

绘图程序

开发平台:

Visual C++

  1. // GraphicView.cpp : implementation of the CGraphicView class
  2. //
  3. #include "stdafx.h"
  4. #include "Graphic.h"
  5. #include "GraphicDoc.h"
  6. #include "GraphicView.h"
  7. #include "SettingDialog.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CGraphicView
  15. IMPLEMENT_DYNCREATE(CGraphicView, CView)
  16. BEGIN_MESSAGE_MAP(CGraphicView, CView)
  17. //{{AFX_MSG_MAP(CGraphicView)
  18. ON_COMMAND(ID_POINT, OnPoint)
  19. ON_COMMAND(ID_LINE, OnLine)
  20. ON_COMMAND(ID_RETANGLE, OnRetangle)
  21. ON_COMMAND(ID_ELLIPSE, OnEllipse)
  22. ON_WM_LBUTTONDOWN()
  23. ON_WM_LBUTTONUP()
  24. ON_UPDATE_COMMAND_UI(ID_LINE, OnUpdateLine)
  25. ON_UPDATE_COMMAND_UI(ID_POINT, OnUpdatePoint)
  26. ON_UPDATE_COMMAND_UI(ID_ELLIPSE, OnUpdateEllipse)
  27. ON_UPDATE_COMMAND_UI(ID_RETANGLE, OnUpdateRetangle)
  28. ON_COMMAND(ID_LINEWIDTHSETTING, OnLinewidthsetting)
  29. ON_COMMAND(ID_COLOR, OnColor)
  30. //}}AFX_MSG_MAP
  31. // Standard printing commands
  32. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  33. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  34. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  35. END_MESSAGE_MAP()
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CGraphicView construction/destruction
  38. CGraphicView::CGraphicView()
  39. {
  40. // TODO: add construction code here
  41. m_nDrawType=0;
  42. m_originpt=0;
  43. m_nLineWidth=1;
  44. m_nColorSetting=RGB(0,0,255);
  45. m_nLineStyle=0;
  46. }
  47. CGraphicView::~CGraphicView()
  48. {
  49. }
  50. BOOL CGraphicView::PreCreateWindow(CREATESTRUCT& cs)
  51. {
  52. // TODO: Modify the Window class or styles here by modifying
  53. //  the CREATESTRUCT cs
  54. return CView::PreCreateWindow(cs);
  55. }
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CGraphicView drawing
  58. void CGraphicView::OnDraw(CDC* pDC)
  59. {
  60. CGraphicDoc* pDoc = GetDocument();
  61. ASSERT_VALID(pDoc);
  62. CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
  63. pDC->SelectObject(pBrush);
  64. for(int i=0;i<m_ptrArray.GetSize();i++)
  65. {
  66. CPen pen(((StorePoint*)m_ptrArray.GetAt(i))->m_nLineStyle,((StorePoint*)m_ptrArray.GetAt(i))->m_nLineWidth,
  67. /*RGB(0,0,255)m_nColorSetting*/((StorePoint*)m_ptrArray.GetAt(i))->m_nColor);//画笔
  68.     pDC->SelectObject(&pen);
  69. switch(((StorePoint*)m_ptrArray.GetAt(i))->m_nDrawType)
  70. {
  71. case 1:
  72. pDC->SetPixel(((StorePoint*)m_ptrArray.GetAt(i))->m_ptEnd,RGB(0,0,0));
  73. break;
  74. case 2:
  75. pDC->MoveTo(((StorePoint*)m_ptrArray.GetAt(i))->m_ptOrigin);
  76. pDC->LineTo(((StorePoint*)m_ptrArray.GetAt(i))->m_ptEnd);
  77. break;
  78. case 3:
  79. pDC->Rectangle(CRect(((StorePoint*)m_ptrArray.GetAt(i))->m_ptOrigin,
  80. ((StorePoint*)m_ptrArray.GetAt(i))->m_ptEnd));
  81. break;
  82. case 4:pDC->Ellipse(CRect(((StorePoint*)m_ptrArray.GetAt(i))->m_ptOrigin,
  83.    ((StorePoint*)m_ptrArray.GetAt(i))->m_ptEnd));
  84. }
  85. }
  86. // TODO: add draw code for native data here
  87. }
  88. /////////////////////////////////////////////////////////////////////////////
  89. // CGraphicView printing
  90. BOOL CGraphicView::OnPreparePrinting(CPrintInfo* pInfo)
  91. {
  92. // default preparation
  93. return DoPreparePrinting(pInfo);
  94. }
  95. void CGraphicView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  96. {
  97. // TODO: add extra initialization before printing
  98. }
  99. void CGraphicView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  100. {
  101. // TODO: add cleanup after printing
  102. }
  103. /////////////////////////////////////////////////////////////////////////////
  104. // CGraphicView diagnostics
  105. #ifdef _DEBUG
  106. void CGraphicView::AssertValid() const
  107. {
  108. CView::AssertValid();
  109. }
  110. void CGraphicView::Dump(CDumpContext& dc) const
  111. {
  112. CView::Dump(dc);
  113. }
  114. CGraphicDoc* CGraphicView::GetDocument() // non-debug version is inline
  115. {
  116. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphicDoc)));
  117. return (CGraphicDoc*)m_pDocument;
  118. }
  119. #endif //_DEBUG
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CGraphicView message handlers
  122. void CGraphicView::OnPoint() 
  123. {
  124. // TODO: Add your command handler code here
  125. m_nDrawType=1 ;
  126. }
  127. void CGraphicView::OnLine() 
  128. {
  129. // TODO: Add your command handler code here
  130. m_nDrawType=2;
  131. }
  132. void CGraphicView::OnRetangle() 
  133. {
  134. // TODO: Add your command handler code here
  135. m_nDrawType=3;
  136. }
  137. void CGraphicView::OnEllipse() 
  138. {
  139. // TODO: Add your command handler code here
  140. m_nDrawType=4;
  141. }
  142. void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point) 
  143. {
  144. // TODO: Add your message handler code here and/or call default
  145. m_originpt=point;
  146. CView::OnLButtonDown(nFlags, point);
  147. }
  148. void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point) 
  149. {
  150. // TODO: Add your message handler code here and/or call default
  151. CClientDC dc(this);
  152. CPen pen(m_nLineStyle,m_nLineWidth,/*RGB(0,0,255)*/m_nColorSetting);//画笔
  153. pOldpen=dc.SelectObject(&pen);
  154. CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));//画刷
  155. pOldbrush=dc.SelectObject(pBrush);
  156. switch(m_nDrawType)
  157. {
  158. case 1:
  159. dc.SetPixel(point,m_nColorSetting);
  160. break;
  161. case 2:
  162. dc.MoveTo(m_originpt);
  163. dc.LineTo(point);
  164. break;
  165. case 3:
  166. dc.Rectangle(CRect(m_originpt,point));
  167. break;
  168. case 4:
  169. dc.Ellipse(CRect(m_originpt,point));
  170. break;
  171. }
  172.     
  173. tag_Point *pStorePoint=new tag_Point(m_originpt,point,m_nDrawType,m_nColorSetting,m_nLineWidth,m_nLineStyle);
  174. m_ptrArray.Add(pStorePoint);
  175. CView::OnLButtonUp(nFlags, point);
  176. }
  177. void CGraphicView::OnUpdateLine(CCmdUI* pCmdUI) 
  178. {
  179. // TODO: Add your command update UI handler code here
  180. pCmdUI->SetCheck((m_nDrawType==2));
  181. }
  182. void CGraphicView::OnUpdatePoint(CCmdUI* pCmdUI) 
  183. {
  184. // TODO: Add your command update UI handler code here
  185. pCmdUI->SetCheck((m_nDrawType==1));
  186. }
  187. void CGraphicView::OnUpdateEllipse(CCmdUI* pCmdUI) 
  188. {
  189. // TODO: Add your command update UI handler code here
  190. pCmdUI->SetCheck((m_nDrawType==4));
  191. }
  192. void CGraphicView::OnUpdateRetangle(CCmdUI* pCmdUI) 
  193. {
  194. // TODO: Add your command update UI handler code here
  195. pCmdUI->SetCheck((m_nDrawType==3));
  196. }
  197. void CGraphicView::OnLinewidthsetting() 
  198. {
  199. // TODO: Add your command handler code here
  200. SettingDialog dlg;
  201. dlg.m_nLineStyle=m_nLineStyle;
  202. dlg.m_nLineWidth=m_nLineWidth;
  203. if(IDOK==dlg.DoModal())
  204. {
  205. m_nLineWidth=dlg.m_nLineWidth;
  206. m_nLineStyle=dlg.m_nLineStyle;
  207. }
  208. }
  209. void CGraphicView::OnColor() 
  210. {
  211. // TODO: Add your command handler code here
  212. CColorDialog dlg;
  213. dlg.m_cc.Flags|=CC_RGBINIT|CC_FULLOPEN;//初始化
  214. dlg.m_cc.rgbResult=m_nColorSetting;
  215. if(IDOK==dlg.DoModal())
  216. {
  217. m_nColorSetting=dlg.m_cc.rgbResult;
  218. };
  219. }