CRoundRectangle.cpp 7.8 KB

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