CRoundArc.cpp 7.8 KB

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