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

GDI/图象编程

开发平台:

Visual C++

  1. // DrawFillRect.cpp: implementation of the CDrawFillRect class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "DrawSys.h"
  6. #include "DrawFillRect.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(CDrawFillRect, CObject, 1)
  17. CDrawFillRect::CDrawFillRect()
  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. CDrawFillRect::~CDrawFillRect()
  25. {
  26. }
  27. void CDrawFillRect::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->Rectangle(&rcDraw);
  42. pDC->SelectObject(hOldBrush);
  43. pDC->SelectObject(pOldPen);
  44. brush.DeleteObject();
  45. }
  46. void CDrawFillRect::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->Rectangle(&rcDraw);
  60. m_ptTemp = point;
  61. rcDraw.right = m_ptTemp.x;
  62. rcDraw.bottom = m_ptTemp.y;
  63. pDC->Rectangle(&rcDraw);
  64. pDC->SetROP2(iDrawMode);
  65. }
  66. }
  67. }
  68. void CDrawFillRect::OnLButtonDown(CDC* pDC, UINT nFlags, CPoint point)
  69. {
  70. m_ptFirst = point;
  71. m_ptTemp = point;
  72. m_bStart = TRUE;
  73. }
  74. BOOL CDrawFillRect::OnLButtonUp(CDC* pDC, UINT nFlags, CPoint point)
  75. {
  76. int iDrawMode = 0;
  77. CRect rcDraw;
  78. CBrush brush(RGB(0, 255, 0));
  79. if(m_bStart)
  80. {
  81. if(pDC)
  82. {
  83. iDrawMode = pDC->SetROP2(R2_NOTXORPEN);
  84. rcDraw.left = m_ptFirst.x;
  85. rcDraw.top = m_ptFirst.y;
  86. rcDraw.right = m_ptTemp.x;
  87. rcDraw.bottom = m_ptTemp.y;
  88. pDC->Rectangle(&rcDraw);
  89. m_ptSecond = point;
  90. m_ptTemp = point;
  91. rcDraw.left = MIN(m_ptFirst.x, m_ptSecond.x);
  92. rcDraw.top = MIN(m_ptFirst.y, m_ptSecond.y);
  93. rcDraw.right = MAX(m_ptFirst.x, m_ptSecond.x);
  94. rcDraw.bottom = MAX(m_ptFirst.y, m_ptSecond.y);
  95. m_ptFirst.x = rcDraw.left;
  96. m_ptFirst.y = rcDraw.top;
  97. m_ptSecond.x = rcDraw.right;
  98. m_ptSecond.y = rcDraw.bottom;
  99. pDC->SetROP2(R2_COPYPEN);
  100. Draw(pDC);
  101. brush.DeleteObject();
  102. pDC->SetROP2(iDrawMode);
  103. }
  104. m_bStart = FALSE;
  105. }
  106. return (!rcDraw.IsRectEmpty());
  107. }
  108. void CDrawFillRect::Serialize(CArchive& ar)
  109. {
  110. if (ar.IsStoring())
  111. {
  112. // TODO: add storing code here
  113. ar << m_ptFirst << m_ptSecond;
  114. }
  115. else
  116. {
  117. // TODO: add loading code here
  118. ar >> m_ptFirst >> m_ptSecond;
  119. }
  120. }
  121. BOOL CDrawFillRect::PtInShape(CPoint point)
  122. {
  123. CRect rcDraw;
  124. rcDraw.left = MIN(m_ptFirst.x, m_ptSecond.x);
  125. rcDraw.top = MIN(m_ptFirst.y, m_ptSecond.y);
  126. rcDraw.right = MAX(m_ptFirst.x, m_ptSecond.x);
  127. rcDraw.bottom = MAX(m_ptFirst.y, m_ptSecond.y);
  128. return rcDraw.PtInRect(point);
  129. }
  130. void CDrawFillRect::ShowProperty()
  131. {
  132. CRect rcDraw;
  133. CString strID;
  134. CString strTime;
  135. CViewProperty dlg;
  136. rcDraw.left = m_ptFirst.x;
  137. rcDraw.top = m_ptFirst.y;
  138. rcDraw.right = m_ptSecond.x;
  139. rcDraw.bottom = m_ptSecond.y;
  140. dlg.m_strType = "填充矩形";
  141. MyFormatString(dlg.m_strID, m_iID);
  142. dlg.m_iLength = rcDraw.Width();
  143. dlg.m_iWidth = rcDraw.Height();
  144. dlg.m_strTime = m_tmCreate.Format("%Y - %m - %d");
  145. dlg.DoModal();
  146. }