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

GDI/图象编程

开发平台:

Visual C++

  1. // DrawFillEllipse.cpp: implementation of the CDrawFillEllipse class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "DrawSys.h"
  6. #include "DrawFillEllipse.h"
  7. #include "ViewProperty.h"
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. IMPLEMENT_SERIAL(CDrawFillEllipse, CObject, 1)
  17. CDrawFillEllipse::CDrawFillEllipse()
  18. {
  19. m_ptFirst.x = m_ptFirst.y = 0;
  20. m_ptSecond.x = m_ptSecond.y = 0;
  21. m_ptTemp.x = m_ptTemp.y = 0;
  22. m_bStart = FALSE;
  23. }
  24. CDrawFillEllipse::~CDrawFillEllipse()
  25. {
  26. }
  27. void CDrawFillEllipse::Draw(CDC* pDC)
  28. {
  29. CRect rcDraw;
  30. CBrush brush(RGB(0, 255, 0));
  31. HBRUSH hOldBrush = NULL;
  32. CPen* pPen = NULL;
  33. CPen* pOldPen = NULL;
  34. rcDraw.left = m_ptFirst.x;
  35. rcDraw.top = m_ptFirst.y;
  36. rcDraw.right = m_ptSecond.x;
  37. rcDraw.bottom = m_ptSecond.y;
  38. pPen = CPen::FromHandle((HPEN)GetStockObject(NULL_PEN));
  39. pOldPen = pDC->SelectObject(pPen);
  40. hOldBrush = (HBRUSH)pDC->SelectObject(&brush);
  41. pDC->Ellipse(&rcDraw);
  42. pDC->SelectObject(hOldBrush);
  43. pDC->SelectObject(pOldPen);
  44. brush.DeleteObject();
  45. }
  46. void CDrawFillEllipse::OnMouseMove(CDC* pDC, UINT nFlags, CPoint point)
  47. {
  48. int iDrawMode = 0;
  49. CRect rcDraw;
  50. if(m_bStart)
  51. {
  52. if(pDC)
  53. {
  54. iDrawMode = pDC->SetROP2(R2_NOTXORPEN);
  55. rcDraw.left = m_ptFirst.x;
  56. rcDraw.top = m_ptFirst.y;
  57. rcDraw.right = m_ptTemp.x;
  58. rcDraw.bottom = m_ptTemp.y;
  59. pDC->Ellipse(&rcDraw);
  60. m_ptTemp = point;
  61. rcDraw.right = m_ptTemp.x;
  62. rcDraw.bottom = m_ptTemp.y;
  63. pDC->Ellipse(&rcDraw);
  64. pDC->SetROP2(iDrawMode);
  65. }
  66. }
  67. }
  68. void CDrawFillEllipse::OnLButtonDown(CDC* pDC, UINT nFlags, CPoint point)
  69. {
  70. m_ptFirst = point;
  71. m_ptTemp = point;
  72. m_bStart = TRUE;
  73. }
  74. BOOL CDrawFillEllipse::OnLButtonUp(CDC* pDC, UINT nFlags, CPoint point)
  75. {
  76. int iDrawMode = 0;
  77. CRect rcDraw;
  78. CBrush brush(RGB(0, 255, 0));
  79. HBRUSH hOldBrush = NULL;
  80. CPen* pPen = NULL;
  81. HPEN hOldPen = NULL;
  82. if(m_bStart)
  83. {
  84. if(pDC)
  85. {
  86. iDrawMode = pDC->SetROP2(R2_NOTXORPEN);
  87. rcDraw.left = m_ptFirst.x;
  88. rcDraw.top = m_ptFirst.y;
  89. rcDraw.right = m_ptTemp.x;
  90. rcDraw.bottom = m_ptTemp.y;
  91. pDC->Ellipse(&rcDraw);
  92. m_ptSecond = point;
  93. m_ptTemp = point;
  94. rcDraw.left = MIN(m_ptFirst.x, m_ptSecond.x);
  95. rcDraw.top = MIN(m_ptFirst.y, m_ptSecond.y);
  96. rcDraw.right = MAX(m_ptFirst.x, m_ptSecond.x);
  97. rcDraw.bottom = MAX(m_ptFirst.y, m_ptSecond.y);
  98. m_ptFirst.x = rcDraw.left;
  99. m_ptFirst.y = rcDraw.top;
  100. m_ptSecond.x = rcDraw.right;
  101. m_ptSecond.y = rcDraw.bottom;
  102. pDC->SetROP2(R2_COPYPEN);
  103. pPen = CPen::FromHandle((HPEN)GetStockObject(NULL_PEN));
  104. hOldPen = (HPEN)pDC->SelectObject(pPen);
  105. hOldBrush = (HBRUSH)pDC->SelectObject(&brush);
  106. Draw(pDC);
  107. pDC->SelectObject(hOldBrush);
  108. pDC->SelectObject(hOldPen);
  109. brush.DeleteObject();
  110. pDC->SetROP2(iDrawMode);
  111. }
  112. m_bStart = FALSE;
  113. }
  114. return (!rcDraw.IsRectEmpty());
  115. }
  116. void CDrawFillEllipse::Serialize(CArchive& ar)
  117. {
  118. if (ar.IsStoring())
  119. {
  120. // TODO: add storing code here
  121. ar << m_ptFirst << m_ptSecond;
  122. }
  123. else
  124. {
  125. // TODO: add loading code here
  126. ar >> m_ptFirst >> m_ptSecond;
  127. }
  128. }
  129. BOOL CDrawFillEllipse::PtInShape(CPoint point)
  130. {
  131. CRect rcDraw;
  132. CRgn rgn;
  133. BOOL m_bRetVal = FALSE;
  134. rcDraw.left = MIN(m_ptFirst.x, m_ptSecond.x);
  135. rcDraw.top = MIN(m_ptFirst.y, m_ptSecond.y);
  136. rcDraw.right = MAX(m_ptFirst.x, m_ptSecond.x);
  137. rcDraw.bottom = MAX(m_ptFirst.y, m_ptSecond.y);
  138. rgn.CreateEllipticRgnIndirect(&rcDraw);
  139. m_bRetVal = rgn.PtInRegion(point);
  140. rgn.DeleteObject();
  141. return m_bRetVal;
  142. }
  143. void CDrawFillEllipse::ShowProperty()
  144. {
  145. CRect rcDraw;
  146. CString strID;
  147. CString strTime;
  148. CViewProperty dlg;
  149. rcDraw.left = m_ptFirst.x;
  150. rcDraw.top = m_ptFirst.y;
  151. rcDraw.right = m_ptSecond.x;
  152. rcDraw.bottom = m_ptSecond.y;
  153. dlg.m_strType = "填充椭圆";
  154. MyFormatString(dlg.m_strID, m_iID);
  155. dlg.m_iLength = rcDraw.Width();
  156. dlg.m_iWidth = rcDraw.Height();
  157. dlg.m_strTime = m_tmCreate.Format("%Y - %m - %d");
  158. dlg.DoModal();
  159. }