CRectangle.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "stdafx.h"
  2. #include "CRectangle.h"
  3. #include"EastDrawView.h"
  4. IMPLEMENT_SERIAL(CRectangle,CUnit,1)
  5. void CRectangle::DrawStatic(CDC*pDC)
  6. {
  7. CPen m_pen;
  8. CBrush m_brush;
  9. int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  10. m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  11. CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  12. LOGBRUSH brushlog;
  13. brushlog.lbColor=m_BrushColor;
  14. brushlog.lbHatch=m_BrushHatch;
  15. brushlog.lbStyle=m_BrushStyle;
  16. m_brush.CreateBrushIndirect(&brushlog);
  17. SetBrushOrg(pDC,&m_brush);
  18. CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  19. int OldBkMode=pDC->SetBkMode(m_BkMode);
  20. COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  21. pDC->Rectangle(CRect(m_FirstPoint,m_SecondPoint));
  22. pDC->SelectObject(brush);
  23. pDC->SelectObject(pen);
  24. pDC->SetBkMode(OldBkMode);
  25. pDC->SetBkColor(OldColor);
  26. pDC->SetROP2(oldDrawingMode);
  27. }
  28. void CRectangle::Serialize(CArchive &ar)
  29. {
  30. CUnit::Serialize(ar);
  31. if(ar.IsStoring())
  32. {
  33. ar<<m_BrushStyle<<m_BrushHatch<<m_BrushColor;
  34. }
  35. else
  36. {
  37. ar>>m_BrushStyle>>m_BrushHatch>>m_BrushColor;
  38. }
  39. }
  40. CRectangle::CRectangle()
  41. {
  42. Initial();
  43. m_BrushStyle=BS_HOLLOW;
  44. m_BrushHatch=HS_CROSS;
  45. }
  46. int CRectangle::IsOnMarginPoint(CPoint point)
  47. {
  48. /*CPoint FirstPoint;
  49. CPoint SecondPoint;
  50. FirstPoint.x=m_FirstPoint.x<m_SecondPoint.x?m_FirstPoint.x:m_SecondPoint.x;
  51. FirstPoint.y=m_FirstPoint.y<m_SecondPoint.y?m_FirstPoint.y:m_SecondPoint.y;
  52. SecondPoint.x=m_FirstPoint.x>m_SecondPoint.x?m_FirstPoint.x:m_SecondPoint.x;
  53. SecondPoint.y=m_FirstPoint.y>m_SecondPoint.y?m_FirstPoint.y:m_SecondPoint.y;
  54. m_FirstPoint=FirstPoint;
  55. m_SecondPoint=SecondPoint;
  56. */
  57. CRect rect(point,point);
  58. rect.InflateRect(CSize(10,10));
  59. if(rect.PtInRect(m_FirstPoint))
  60. {
  61. m_FoundPoint=m_FirstPoint;
  62. return 1;
  63. }
  64. if(rect.PtInRect(CPoint(m_SecondPoint.x,m_FirstPoint.y)))
  65. { m_FoundPoint=(CPoint(m_SecondPoint.x,m_FirstPoint.y));
  66. return 2;
  67. }
  68. if(rect.PtInRect(m_SecondPoint))
  69. {
  70. m_FoundPoint=m_SecondPoint;
  71. return 3;
  72. }
  73. if(rect.PtInRect(CPoint(m_FirstPoint.x,m_SecondPoint.y)))
  74. { m_FoundPoint=CPoint(m_FirstPoint.x,m_SecondPoint.y);
  75. return 4;
  76. }
  77. if(rect.PtInRect(CPoint((m_FirstPoint.x+m_SecondPoint.x)/2,(m_FirstPoint.y+m_SecondPoint.y)/2)))
  78. {
  79. m_FoundPoint=CPoint((m_FirstPoint.x+m_SecondPoint.x)/2,(m_FirstPoint.y+m_SecondPoint.y)/2);
  80. return 5;
  81. }
  82. return 0;
  83. }
  84. BOOL CRectangle::IsInRgn(CPoint point)
  85. {
  86. CPoint FirstPoint;
  87. CPoint SecondPoint;
  88. FirstPoint.x=m_FirstPoint.x<m_SecondPoint.x?m_FirstPoint.x:m_SecondPoint.x;
  89. FirstPoint.y=m_FirstPoint.y<m_SecondPoint.y?m_FirstPoint.y:m_SecondPoint.y;
  90. SecondPoint.x=m_FirstPoint.x>m_SecondPoint.x?m_FirstPoint.x:m_SecondPoint.x;
  91. SecondPoint.y=m_FirstPoint.y>m_SecondPoint.y?m_FirstPoint.y:m_SecondPoint.y;
  92. m_FirstPoint=FirstPoint;
  93. m_SecondPoint=SecondPoint;
  94. CRect rect1(m_FirstPoint,m_SecondPoint);
  95. rect1.InflateRect(4,4);
  96. CRect rect2(m_FirstPoint,m_SecondPoint);
  97. rect2.DeflateRect(4,4);
  98. return (rect1.PtInRect(point)&&!rect2.PtInRect(point));
  99. }
  100. void CRectangle::ShowSelectPoint(CDC *pDC)
  101. {
  102. CBrush brush;
  103. brush.CreateSolidBrush(RGB(0,0,255));
  104. CPen m_pen;
  105. m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  106. CPen *OldPen=pDC->SelectObject(&m_pen);
  107. int oldBkMode=pDC->SetBkMode(OPAQUE);
  108. CBrush *OldBrush=pDC->SelectObject(&brush);
  109. int oldDrawingMode=pDC->SetROP2(R2_NOTXORPEN);
  110. CRect rect;
  111. rect.SetRect(this->m_FirstPoint,this->m_FirstPoint);
  112. rect.InflateRect(3,3);
  113. pDC->Rectangle(rect);
  114. rect.SetRect(this->m_SecondPoint,this->m_SecondPoint);
  115. rect.InflateRect(3,3);
  116. pDC->Rectangle(rect);
  117. rect.SetRect(this->m_FirstPoint.x,this->m_SecondPoint.y,this->m_FirstPoint.x,this->m_SecondPoint.y);
  118. rect.InflateRect(3,3);
  119. pDC->Rectangle(rect);
  120. rect.SetRect(this->m_SecondPoint.x,this->m_FirstPoint.y,this->m_SecondPoint.x,this->m_FirstPoint.y);
  121. rect.InflateRect(3,3);
  122. pDC->Rectangle(rect);
  123. pDC->SelectObject(OldBrush);
  124. brush.DeleteObject();
  125. brush.CreateSolidBrush(RGB(255,0,0));
  126. pDC->SelectObject(&brush);
  127. rect.SetRect((this->m_SecondPoint.x+this->m_FirstPoint.x)/2,(this->m_SecondPoint.y+this->m_FirstPoint.y)/2,(this->m_SecondPoint.x+this->m_FirstPoint.x)/2,(this->m_SecondPoint.y+this->m_FirstPoint.y)/2);
  128. rect.InflateRect(3,3);
  129. pDC->Rectangle(rect);
  130. pDC->SelectObject(OldPen);
  131. pDC->SetBkMode(oldBkMode);
  132. pDC->SelectObject(OldBrush);
  133. pDC->SetROP2(oldDrawingMode);
  134. }
  135. void CRectangle::DrawActive(CDC *pDC,CPoint point)
  136. {
  137. CPen m_pen;
  138. CBrush m_brush;
  139. m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  140. CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  141. LOGBRUSH brushlog;
  142. brushlog.lbColor=m_BrushColor;
  143. brushlog.lbHatch=m_BrushHatch;
  144. brushlog.lbStyle=m_BrushStyle;
  145. m_brush.CreateBrushIndirect(&brushlog);
  146. SetBrushOrg(pDC,&m_brush);
  147. CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  148. int OldBkMode=pDC->SetBkMode(m_BkMode);
  149. COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  150. pDC->Rectangle(CRect(m_FirstPoint,m_SecondPoint));
  151. pDC->SelectObject(brush);
  152. pDC->SelectObject(pen);
  153. pDC->SetBkMode(OldBkMode);
  154. pDC->SetBkColor(OldColor);
  155. }
  156. void CRectangle::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
  157. {
  158. if(m_HaveFindFirst)
  159. {
  160. pView->L_iPointCount=IsOnMarginPoint(point);
  161. if(pView->L_iPointCount!=0)
  162. {
  163. pView->m_bHaveFindSecond=true;
  164. pView->m_CurrentDrawTool=Rectangle_Tool;
  165. pView->m_CurrentDrawStatus=Change_Status;
  166. switch(pView->L_iPointCount)
  167. {
  168. case 1:
  169. pView->m_FirstPoint=m_SecondPoint;
  170. pView->m_SecondPoint=point;
  171. break;
  172. case 2:
  173. pView->m_FirstPoint=CPoint(m_FirstPoint.x,m_SecondPoint.y);
  174. pView->m_SecondPoint=point;
  175. break;
  176. case 3:
  177. pView->m_FirstPoint=m_FirstPoint;
  178. pView->m_SecondPoint=point;
  179. break;
  180. case 4:
  181. pView->m_FirstPoint=CPoint(m_SecondPoint.x,m_FirstPoint.y);
  182. pView->m_SecondPoint=point;
  183. break;
  184. case 5:
  185. pView->m_FirstPoint=m_FirstPoint;
  186. pView->m_SecondPoint=m_SecondPoint;
  187. pView->m_CurrentDrawTool=Rectangle_Tool;
  188. pView->m_CurrentDrawStatus=Drag_Status;
  189. break;
  190. }
  191. }//******if(L_iPointCount!=0)
  192. }//********if(L_pRectangle->m_HaveFindFirst)
  193. if(!pView->m_bHaveFindSecond&&IsInRgn(point))
  194. {
  195. if(!m_HaveFindFirst)
  196. {
  197. m_HaveFindFirst=true;
  198. pView->m_bHaveFindFirst=true;
  199. ShowSelectPoint(pDC);
  200. }
  201. else
  202. {
  203. m_HaveFindFirst=false;
  204. pView->m_bHaveFindFirst=true;
  205. ShowSelectPoint(pDC);
  206. }
  207. }
  208. if(pView->m_bHaveFindSecond)
  209. {
  210. pView->m_pCurrentUnit=this;
  211. m_DrawingMode=pDC->GetROP2();
  212. ShowSelectPoint(pDC);
  213. DrawOldReferencePoint(pDC,m_FoundPoint);
  214. DrawStatic(pDC);
  215. }
  216. }
  217. void CRectangle::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
  218. {
  219. if(pView->m_CurrentDrawStatus==Drag_Status)
  220. {
  221. m_FirstPoint.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  222. m_SecondPoint.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  223. }
  224. else
  225. {
  226. this->DrawActive(pDC,point);
  227. pView->m_SecondPoint=point;
  228. m_SecondPoint=pView->m_SecondPoint;
  229. this->DrawActive(pDC,point);
  230. }
  231. }