DrawSysView.cpp
上传用户:mosfetic
上传日期:2022-06-16
资源大小:4612k
文件大小:9k
源码类别:

GDI/图象编程

开发平台:

Visual C++

  1. // DrawSysView.cpp : implementation of the CDrawSysView class
  2. //
  3. #include "stdafx.h"
  4. #include "DrawSys.h"
  5. #include "DrawSysDoc.h"
  6. #include "DrawSysView.h"
  7. #include "MainFrm.h"
  8. #include "DrawShape.h"
  9. #include "DrawRect.h"
  10. #include "DrawEllipse.h"
  11. #include "DrawCircle.h"
  12. #include "DrawFillRect.h"
  13. #include "DrawFillEllipse.h"
  14. #include "DrawLine.h"
  15. #ifdef _DEBUG
  16. #define new DEBUG_NEW
  17. #undef THIS_FILE
  18. static char THIS_FILE[] = __FILE__;
  19. #endif
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CDrawSysView
  22. IMPLEMENT_DYNCREATE(CDrawSysView, CScrollView)
  23. BEGIN_MESSAGE_MAP(CDrawSysView, CScrollView)
  24. //{{AFX_MSG_MAP(CDrawSysView)
  25. ON_COMMAND(ID_BUTTON_RECT, OnButtonRect)
  26. ON_WM_MOUSEMOVE()
  27. ON_WM_LBUTTONDOWN()
  28. ON_WM_LBUTTONUP()
  29. ON_COMMAND(ID_BUTTON_ELLIPSE, OnButtonEllipse)
  30. ON_COMMAND(ID_BUTTON_FILLELLIPSE, OnButtonFillellipse)
  31. ON_COMMAND(ID_BUTTON_CIRCLE, OnButtonCircle)
  32. ON_COMMAND(ID_BUTTON_FILLRECT, OnButtonFillrect)
  33. ON_COMMAND(ID_BUTTON_LINE, OnButtonLine)
  34. ON_MESSAGE(WM_CHOOSE, OnChoose)
  35. ON_WM_TIMER()
  36. ON_WM_DESTROY()
  37. ON_WM_RBUTTONDOWN()
  38. //}}AFX_MSG_MAP
  39. // Standard printing commands
  40. ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
  41. ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
  42. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  43. END_MESSAGE_MAP()
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CDrawSysView construction/destruction
  46. CDrawSysView::CDrawSysView()
  47. {
  48. // TODO: add construction code here
  49. m_pDrawing = NULL;
  50. m_iMode = -1;
  51. m_nTimer = 0;
  52. m_dwNewSel = 0;
  53. m_dwOldSel = 0;
  54. m_bNeed = FALSE;
  55. }
  56. CDrawSysView::~CDrawSysView()
  57. {
  58. if(m_pDrawing)
  59. {
  60. delete m_pDrawing;
  61. m_pDrawing = NULL;
  62. }
  63. }
  64. BOOL CDrawSysView::PreCreateWindow(CREATESTRUCT& cs)
  65. {
  66. // TODO: Modify the Window class or styles here by modifying
  67. //  the CREATESTRUCT cs
  68. return CScrollView::PreCreateWindow(cs);
  69. }
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CDrawSysView drawing
  72. void CDrawSysView::OnDraw(CDC* pDC)
  73. {
  74. CDrawSysDoc* pDoc = GetDocument();
  75. ASSERT_VALID(pDoc);
  76. // TODO: add draw code for native data here
  77. int iTotal = pDoc->m_obArray.GetSize();
  78. for(int i=0; i<iTotal; i++)
  79. {
  80. ((CDrawShape*)pDoc->m_obArray.GetAt(i))->Draw(pDC);
  81. }
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CDrawSysView printing
  85. BOOL CDrawSysView::OnPreparePrinting(CPrintInfo* pInfo)
  86. {
  87. // default preparation
  88. return DoPreparePrinting(pInfo);
  89. }
  90. void CDrawSysView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  91. {
  92. // TODO: add extra initialization before printing
  93. }
  94. void CDrawSysView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  95. {
  96. // TODO: add cleanup after printing
  97. }
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CDrawSysView diagnostics
  100. #ifdef _DEBUG
  101. void CDrawSysView::AssertValid() const
  102. {
  103. CScrollView::AssertValid();
  104. }
  105. void CDrawSysView::Dump(CDumpContext& dc) const
  106. {
  107. CScrollView::Dump(dc);
  108. }
  109. CDrawSysDoc* CDrawSysView::GetDocument() // non-debug version is inline
  110. {
  111. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawSysDoc)));
  112. return (CDrawSysDoc*)m_pDocument;
  113. }
  114. #endif //_DEBUG
  115. void CDrawSysView::OnInitialUpdate() 
  116. {
  117. CScrollView ::OnInitialUpdate();
  118. // TODO: Add your specialized code here and/or call the base class
  119. SetScrollSizes(MM_TEXT, CSize(800,600));
  120. }
  121. /////////////////////////////////////////////////////////////////////////////
  122. // CDrawSysView message handlers
  123. void CDrawSysView::OnButtonRect() 
  124. {
  125. // TODO: Add your command handler code here
  126. if(m_pDrawing)
  127. {
  128. delete m_pDrawing;
  129. m_pDrawing = NULL;
  130. }
  131. m_pDrawing = new CDrawRect();
  132. m_iMode = 0;
  133. MyKillTimer();
  134. }
  135. void CDrawSysView::OnMouseMove(UINT nFlags, CPoint point) 
  136. {
  137. // TODO: Add your message handler code here and/or call default
  138. CDC* pDC = NULL;
  139. if(m_pDrawing)
  140. {
  141. pDC = GetDC();
  142. OnPrepareDC(pDC);
  143. pDC->DPtoLP(&point);
  144. m_pDrawing->OnMouseMove(pDC, nFlags, point);
  145. ReleaseDC(pDC);
  146. }
  147. CScrollView::OnMouseMove(nFlags, point);
  148. }
  149. void CDrawSysView::OnLButtonDown(UINT nFlags, CPoint point) 
  150. {
  151. // TODO: Add your message handler code here and/or call default
  152. MyKillTimer();
  153. CDC* pDC = NULL;
  154. if(m_pDrawing)
  155. {
  156. pDC = GetDC();
  157. OnPrepareDC(pDC);
  158. pDC->DPtoLP(&point);
  159. m_pDrawing->OnLButtonDown(pDC, nFlags, point);
  160. ReleaseDC(pDC);
  161. }
  162. CScrollView::OnLButtonDown(nFlags, point);
  163. }
  164. void CDrawSysView::OnLButtonUp(UINT nFlags, CPoint point) 
  165. {
  166. // TODO: Add your message handler code here and/or call default
  167. CDrawSysDoc* pDoc = NULL;
  168. CDC* pDC = NULL;
  169. int iIndex = 0;
  170. BOOL bSave = FALSE;
  171. if(m_pDrawing)
  172. {
  173. pDC = GetDC();
  174. OnPrepareDC(pDC);
  175. pDC->DPtoLP(&point);
  176. bSave = m_pDrawing->OnLButtonUp(pDC, nFlags, point);
  177. ReleaseDC(pDC);
  178. if(bSave)
  179. {
  180. //存储对象
  181. m_pDrawing->m_tmCreate = CTime::GetCurrentTime();
  182. m_pDrawing->m_iID = ((CMainFrame*)AfxGetMainWnd())->m_wndShapeTree.GetID(m_pDrawing);
  183. pDoc = GetDocument();
  184. pDoc->AddShape(m_pDrawing);
  185. //更新树
  186. iIndex = pDoc->m_obArray.GetSize();
  187. ((CMainFrame*)AfxGetMainWnd())->m_wndShapeTree.InsertShape(m_pDrawing, iIndex);
  188. }
  189. else
  190. {
  191. //释放对象
  192. delete m_pDrawing;
  193. }
  194. m_pDrawing = NULL;
  195. //创建新对象
  196. switch(m_iMode)
  197. {
  198. case 0:
  199. m_pDrawing = new CDrawRect();
  200. break;
  201. case 1:
  202. m_pDrawing = new CDrawEllipse();
  203. break;
  204. case 2:
  205. m_pDrawing = new CDrawCircle();
  206. break;
  207. case 3:
  208. m_pDrawing = new CDrawFillRect();
  209. break;
  210. case 4:
  211. m_pDrawing = new CDrawFillEllipse();
  212. break;
  213. case 5:
  214. m_pDrawing = new CDrawLine();
  215. break;
  216. default:
  217. //选择模式
  218. break;
  219. }
  220. }
  221. CScrollView::OnLButtonUp(nFlags, point);
  222. }
  223. void CDrawSysView::OnButtonEllipse() 
  224. {
  225. // TODO: Add your command handler code here
  226. if(m_pDrawing)
  227. {
  228. delete m_pDrawing;
  229. m_pDrawing = NULL;
  230. }
  231. m_pDrawing = new CDrawEllipse();
  232. m_iMode = 1;
  233. MyKillTimer();
  234. }
  235. void CDrawSysView::OnButtonFillellipse() 
  236. {
  237. // TODO: Add your command handler code here
  238. if(m_pDrawing)
  239. {
  240. delete m_pDrawing;
  241. m_pDrawing = NULL;
  242. }
  243. m_pDrawing = new CDrawFillEllipse();
  244. m_iMode = 4;
  245. MyKillTimer();
  246. }
  247. void CDrawSysView::OnButtonCircle() 
  248. {
  249. // TODO: Add your command handler code here
  250. if(m_pDrawing)
  251. {
  252. delete m_pDrawing;
  253. m_pDrawing = NULL;
  254. }
  255. m_pDrawing = new CDrawCircle();
  256. m_iMode = 2;
  257. MyKillTimer();
  258. }
  259. void CDrawSysView::OnButtonFillrect() 
  260. {
  261. // TODO: Add your command handler code here
  262. if(m_pDrawing)
  263. {
  264. delete m_pDrawing;
  265. m_pDrawing = NULL;
  266. }
  267. m_pDrawing = new CDrawFillRect();
  268. m_iMode = 3;
  269. MyKillTimer();
  270. }
  271. void CDrawSysView::OnButtonLine() 
  272. {
  273. // TODO: Add your command handler code here
  274. if(m_pDrawing)
  275. {
  276. delete m_pDrawing;
  277. m_pDrawing = NULL;
  278. }
  279. m_pDrawing = new CDrawLine();
  280. m_iMode = 5;
  281. MyKillTimer();
  282. }
  283. LRESULT CDrawSysView::OnChoose(WPARAM wParam, LPARAM lParam)
  284. {
  285. MyKillTimer();
  286. m_dwNewSel = wParam;
  287. m_dwOldSel = lParam;
  288. m_nTimer = SetTimer(0, 200, NULL);
  289. return 0;
  290. }
  291. void CDrawSysView::OnTimer(UINT nIDEvent) 
  292. {
  293. // TODO: Add your message handler code here and/or call default
  294. FlashShape();
  295. CScrollView::OnTimer(nIDEvent);
  296. }
  297. void CDrawSysView::MyKillTimer()
  298. {
  299. if(m_nTimer)
  300. {
  301. KillTimer(m_nTimer);
  302. m_nTimer = 0;
  303. }
  304. //清除以前闪烁
  305. if(m_bNeed)
  306. {
  307. FlashShape();
  308. }
  309. m_dwNewSel = 0;
  310. m_dwOldSel = 0;
  311. m_bNeed = FALSE;
  312. }
  313. void CDrawSysView::FlashShape()
  314. {
  315. CDC* pDC = NULL;
  316. CDrawSysDoc* pDoc = NULL;
  317. int iDrawMode = 0;
  318. CPen pen(PS_SOLID, 3, RGB(255, 0, 0));
  319. CPen* pOldPen = NULL;
  320. if(m_dwNewSel)
  321. {
  322. pDoc = GetDocument();
  323. pDC = GetDC();
  324. OnPrepareDC(pDC);
  325. iDrawMode = pDC->SetROP2(R2_NOTXORPEN);
  326. pOldPen = pDC->SelectObject(&pen);
  327. ((CDrawShape*)(pDoc->m_obArray.GetAt(m_dwNewSel-1)))->Draw(pDC);
  328. pDC->SelectObject(pOldPen);
  329. pen.DeleteObject();
  330. pDC->SetROP2(iDrawMode);
  331. ReleaseDC(pDC);
  332. pDC = NULL;
  333. m_bNeed = !m_bNeed;
  334. }
  335. }
  336. void CDrawSysView::OnDestroy() 
  337. {
  338. CScrollView::OnDestroy();
  339. // TODO: Add your message handler code here
  340. MyKillTimer();
  341. }
  342. BOOL CDrawSysView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
  343. {
  344. // TODO: Add your specialized code here and/or call the base class
  345. MyKillTimer();
  346. return CScrollView::OnScroll(nScrollCode, nPos, bDoScroll);
  347. }
  348. BOOL CDrawSysView::OnScrollBy(CSize sizeScroll, BOOL bDoScroll) 
  349. {
  350. // TODO: Add your specialized code here and/or call the base class
  351. MyKillTimer();
  352. return CScrollView::OnScrollBy(sizeScroll, bDoScroll);
  353. }
  354. void CDrawSysView::OnRButtonDown(UINT nFlags, CPoint point) 
  355. {
  356. // TODO: Add your message handler code here and/or call default
  357. CDrawSysDoc* pDoc = GetDocument();
  358. int iTotal = pDoc->m_obArray.GetSize();
  359. BOOL m_bIn = FALSE;
  360. CDC* pDC = NULL;
  361. ASSERT_VALID(pDoc);
  362. pDC = GetDC();
  363. OnPrepareDC(pDC);
  364. pDC->DPtoLP(&point);
  365. for(int i=0; i<iTotal; i++)
  366. {
  367. m_bIn = ((CDrawShape*)pDoc->m_obArray.GetAt(i))->PtInShape(point);
  368. if(m_bIn)
  369. break;
  370. }
  371. if(m_bIn)
  372. {
  373. ((CDrawShape*)pDoc->m_obArray.GetAt(i))->ShowProperty();
  374. }
  375. pDC->LPtoDP(&point);
  376. ReleaseDC(pDC);
  377. CScrollView::OnRButtonDown(nFlags, point);
  378. }