CDLine.cpp 8.0 KB

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