CRound.cpp 8.4 KB

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