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

GDI/图象编程

开发平台:

Visual C++

  1. // DrawEllipse.cpp: implementation of the CDrawEllipse class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "DrawSys.h"
  6. #include "DrawEllipse.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(CDrawEllipse, CObject, 1)
  17. CDrawEllipse::CDrawEllipse()
  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. CDrawEllipse::~CDrawEllipse()
  25. {
  26. }
  27. void CDrawEllipse::Draw(CDC* pDC)
  28. {
  29. CRect rcDraw;
  30. CBrush* pBrush = NULL;
  31. CBrush* pOldBrush = NULL;
  32. pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
  33. pOldBrush = pDC->SelectObject(pBrush);
  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. pDC->Ellipse(&rcDraw);
  39. pDC->SelectObject(pOldBrush);
  40. }
  41. void CDrawEllipse::OnMouseMove(CDC* pDC, UINT nFlags, CPoint point)
  42. {
  43. int iDrawMode = 0;
  44. CRect rcDraw;
  45. CBrush* pBrush = NULL;
  46. CBrush* pOldBrush = NULL;
  47. if(m_bStart)
  48. {
  49. if(pDC)
  50. {
  51. pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
  52. pOldBrush = pDC->SelectObject(pBrush);
  53. iDrawMode = pDC->SetROP2(R2_NOTXORPEN);
  54. rcDraw.left = m_ptFirst.x;
  55. rcDraw.top = m_ptFirst.y;
  56. rcDraw.right = m_ptTemp.x;
  57. rcDraw.bottom = m_ptTemp.y;
  58. pDC->Ellipse(&rcDraw);
  59. m_ptTemp = point;
  60. rcDraw.right = m_ptTemp.x;
  61. rcDraw.bottom = m_ptTemp.y;
  62. pDC->Ellipse(&rcDraw);
  63. pDC->SetROP2(iDrawMode);
  64. pDC->SelectObject(pOldBrush);
  65. }
  66. }
  67. }
  68. void CDrawEllipse::OnLButtonDown(CDC* pDC, UINT nFlags, CPoint point)
  69. {
  70. m_ptFirst = point;
  71. m_ptTemp = point;
  72. m_bStart = TRUE;
  73. }
  74. BOOL CDrawEllipse::OnLButtonUp(CDC* pDC, UINT nFlags, CPoint point)
  75. {
  76. int iDrawMode = 0;
  77. CRect rcDraw;
  78. CBrush* pBrush = NULL;
  79. CBrush* pOldBrush = NULL;
  80. if(m_bStart)
  81. {
  82. if(pDC)
  83. {
  84. pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
  85. pOldBrush = pDC->SelectObject(pBrush);
  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. Draw(pDC);
  104. pDC->SetROP2(iDrawMode);
  105. pDC->SelectObject(pOldBrush);
  106. }
  107. m_bStart = FALSE;
  108. }
  109. return (!rcDraw.IsRectEmpty());
  110. }
  111. void CDrawEllipse::Serialize(CArchive& ar)
  112. {
  113. if (ar.IsStoring())
  114. {
  115. // TODO: add storing code here
  116. ar << m_ptFirst << m_ptSecond;
  117. }
  118. else
  119. {
  120. // TODO: add loading code here
  121. ar >> m_ptFirst >> m_ptSecond;
  122. }
  123. }
  124. BOOL CDrawEllipse::PtInShape(CPoint point)
  125. {
  126. CRect rcDraw;
  127. CRgn rgn;
  128. BOOL m_bRetVal = FALSE;
  129. rcDraw.left = MIN(m_ptFirst.x, m_ptSecond.x);
  130. rcDraw.top = MIN(m_ptFirst.y, m_ptSecond.y);
  131. rcDraw.right = MAX(m_ptFirst.x, m_ptSecond.x);
  132. rcDraw.bottom = MAX(m_ptFirst.y, m_ptSecond.y);
  133. rgn.CreateEllipticRgnIndirect(&rcDraw);
  134. m_bRetVal = rgn.PtInRegion(point);
  135. rgn.DeleteObject();
  136. return m_bRetVal;
  137. }
  138. void CDrawEllipse::ShowProperty()
  139. {
  140. CRect rcDraw;
  141. CString strID;
  142. CString strTime;
  143. CViewProperty dlg;
  144. rcDraw.left = m_ptFirst.x;
  145. rcDraw.top = m_ptFirst.y;
  146. rcDraw.right = m_ptSecond.x;
  147. rcDraw.bottom = m_ptSecond.y;
  148. dlg.m_strType = "椭圆";
  149. MyFormatString(dlg.m_strID, m_iID);
  150. dlg.m_iLength = rcDraw.Width();
  151. dlg.m_iWidth = rcDraw.Height();
  152. dlg.m_strTime = m_tmCreate.Format("%Y - %m - %d");
  153. dlg.DoModal();
  154. }