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

GDI/图象编程

开发平台:

Visual C++

  1. // DrawRect.cpp: implementation of the CDrawRect class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "DrawSys.h"
  6. #include "DrawRect.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(CDrawRect, CObject, 1)
  17. CDrawRect::CDrawRect()
  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. CDrawRect::~CDrawRect()
  25. {
  26. }
  27. void CDrawRect::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->Rectangle(&rcDraw);
  39. pDC->SelectObject(pOldBrush);
  40. }
  41. void CDrawRect::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->Rectangle(&rcDraw);
  59. m_ptTemp = point;
  60. rcDraw.right = m_ptTemp.x;
  61. rcDraw.bottom = m_ptTemp.y;
  62. pDC->Rectangle(&rcDraw);
  63. pDC->SetROP2(iDrawMode);
  64. pDC->SelectObject(pOldBrush);
  65. }
  66. }
  67. }
  68. void CDrawRect::OnLButtonDown(CDC* pDC, UINT nFlags, CPoint point)
  69. {
  70. m_ptFirst = point;
  71. m_ptTemp = point;
  72. m_bStart = TRUE;
  73. }
  74. BOOL CDrawRect::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->Rectangle(&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 CDrawRect::Serialize(CArchive& ar)
  112. {
  113. CDrawShape::Serialize(ar);
  114. if (ar.IsStoring())
  115. {
  116. // TODO: add storing code here
  117. ar << m_ptFirst << m_ptSecond;
  118. }
  119. else
  120. {
  121. // TODO: add loading code here
  122. ar >> m_ptFirst >> m_ptSecond;
  123. }
  124. }
  125. BOOL CDrawRect::PtInShape(CPoint point)
  126. {
  127. CRect rcDraw;
  128. rcDraw.left = MIN(m_ptFirst.x, m_ptSecond.x);
  129. rcDraw.top = MIN(m_ptFirst.y, m_ptSecond.y);
  130. rcDraw.right = MAX(m_ptFirst.x, m_ptSecond.x);
  131. rcDraw.bottom = MAX(m_ptFirst.y, m_ptSecond.y);
  132. return rcDraw.PtInRect(point);
  133. }
  134. void CDrawRect::ShowProperty()
  135. {
  136. CRect rcDraw;
  137. CString strID;
  138. CString strTime;
  139. CViewProperty dlg;
  140. rcDraw.left = m_ptFirst.x;
  141. rcDraw.top = m_ptFirst.y;
  142. rcDraw.right = m_ptSecond.x;
  143. rcDraw.bottom = m_ptSecond.y;
  144. dlg.m_strType = "矩形";
  145. MyFormatString(dlg.m_strID, m_iID);
  146. dlg.m_iLength = rcDraw.Width();
  147. dlg.m_iWidth = rcDraw.Height();
  148. dlg.m_strTime = m_tmCreate.Format("%Y - %m - %d");
  149. dlg.DoModal();
  150. }