CPolygon.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. #include"stdafx.h"
  2. #include"CPolygon.h"
  3. #include"EastDrawView.h"
  4. IMPLEMENT_SERIAL(CPolygon,CUnit,1)
  5. void CPolygon::Serialize(CArchive &ar)
  6. {
  7. CUnit::Serialize(ar);
  8. if(ar.IsStoring())
  9. {
  10. ar<<m_PenColor<<m_PenStyle<<m_PenWidth;
  11. }
  12. else
  13. {
  14. ar>>m_PenColor>>m_PenStyle>>m_PenWidth;
  15. }
  16. m_PointList->Serialize(ar);
  17. }
  18. void CPolygon::Initial()
  19. {
  20. CUnit::Initial();
  21. m_PointList=new CArray<CPoint,CPoint>;
  22. }
  23. void CPolygon::DrawStatic(CDC *pDC)
  24. {
  25. CPen m_pen;
  26. CBrush m_brush;
  27. int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  28. m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  29. CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  30. LOGBRUSH brushlog;
  31. brushlog.lbColor=m_BrushColor;
  32. brushlog.lbHatch=m_BrushHatch;
  33. brushlog.lbStyle=m_BrushStyle;
  34. m_brush.CreateBrushIndirect(&brushlog);
  35. CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  36. int OldBkMode=pDC->SetBkMode(m_BkMode);
  37. COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  38. m_FirstPoint=m_PointList->GetAt(0);
  39. pDC->MoveTo(m_FirstPoint);
  40. for(int i=0;i<m_PointList->GetSize();i++)
  41. {
  42. pDC->LineTo(m_PointList->GetAt(i));
  43. }
  44. pDC->LineTo(m_PointList->GetAt(0));
  45. pDC->SelectObject(brush);
  46. pDC->SelectObject(pen);
  47. pDC->SetBkMode(OldBkMode);
  48. pDC->SetBkColor(OldColor);
  49. pDC->SetROP2(oldDrawingMode);
  50. }
  51. void CPolygon::DrawMask(CDC *pDC, CPoint point)
  52. {
  53. CPen m_pen;
  54. CBrush m_brush;
  55. //int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  56. m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  57. CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  58. LOGBRUSH brushlog;
  59. brushlog.lbColor=m_BrushColor;
  60. brushlog.lbHatch=m_BrushHatch;
  61. brushlog.lbStyle=m_BrushStyle;
  62. m_brush.CreateBrushIndirect(&brushlog);
  63. CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  64. int OldBkMode=pDC->SetBkMode(m_BkMode);
  65. COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  66. pDC->MoveTo(m_PointList->GetAt(m_PointList->GetSize()-1));
  67. pDC->LineTo(point);
  68. pDC->SelectObject(brush);
  69. pDC->SelectObject(pen);
  70. pDC->SetBkMode(OldBkMode);
  71. pDC->SetBkColor(OldColor);
  72. //pDC->SetROP2(oldDrawingMode);
  73. }
  74. CPolygon::CPolygon()
  75. {
  76. Initial();
  77. }
  78. void CPolygon::DrawEnd(CDC*pDC,CPoint point)
  79. {
  80. CPen m_pen;
  81. CBrush m_brush;
  82. int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  83. m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  84. CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  85. LOGBRUSH brushlog;
  86. brushlog.lbColor=m_BrushColor;
  87. brushlog.lbHatch=m_BrushHatch;
  88. brushlog.lbStyle=m_BrushStyle;
  89. m_brush.CreateBrushIndirect(&brushlog);
  90. CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  91. int OldBkMode=pDC->SetBkMode(m_BkMode);
  92. COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  93. pDC->MoveTo(point);
  94. pDC->LineTo(m_PointList->GetAt(0));
  95. pDC->SelectObject(brush);
  96. pDC->SelectObject(pen);
  97. pDC->SetBkMode(OldBkMode);
  98. pDC->SetBkColor(OldColor);
  99. pDC->SetROP2(oldDrawingMode);
  100. }
  101. int CPolygon::IsOnMarginPoint(CPoint point)
  102. {
  103. CRect rect;
  104. CPoint firstPoint;
  105. CPoint secondPoint;
  106. firstPoint=secondPoint=m_PointList->GetAt(0);
  107. for(int i=0;i<m_PointList->GetSize();i++)
  108. {
  109. secondPoint=m_PointList->GetAt(i);
  110. rect=CRect(m_PointList->GetAt(i),m_PointList->GetAt(i));
  111. rect.InflateRect(4,4);
  112. if(rect.PtInRect(point))
  113. {
  114. m_FoundIndex=i;
  115. m_FoundPoint=m_PointList->GetAt(i);
  116. return i;
  117. }
  118. rect.SetRect(CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2),CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2));
  119. rect.InflateRect(4,4);
  120. if(rect.PtInRect(point))
  121. {
  122. m_FoundPoint=CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2);
  123. return -i-10;
  124. }
  125. firstPoint=secondPoint;
  126. }
  127. return -1;
  128. }
  129. BOOL CPolygon::IsInRgn(CPoint point)
  130. {
  131. CPoint point4[4];
  132. CPoint FirstPoint;
  133. CPoint SecondPoint;
  134. m_FirstPoint=m_PointList->GetAt(m_PointList->GetSize()-1);
  135. CRgn rgn;
  136. for(int i=0;i<m_PointList->GetSize();i++)
  137. {
  138. m_SecondPoint=m_PointList->GetAt(i);
  139. if((m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.y-m_SecondPoint.y)<0)
  140. {
  141. if(m_FirstPoint.x>m_SecondPoint.x)
  142. {
  143. FirstPoint=m_SecondPoint;
  144. SecondPoint=m_FirstPoint;
  145. }
  146. else
  147. {
  148. FirstPoint=m_FirstPoint;
  149. SecondPoint=m_SecondPoint;
  150. }
  151. FirstPoint.Offset(-4-m_PenWidth,4+m_PenWidth);
  152. SecondPoint.Offset(4+m_PenWidth,4+m_PenWidth);
  153. point4[0].x=FirstPoint.x-3-m_PenWidth;
  154. point4[0].y=FirstPoint.y-4-m_PenWidth;
  155. point4[1].x=FirstPoint.x+3+m_PenWidth;
  156. point4[1].y=FirstPoint.y+4+m_PenWidth;
  157. point4[2].x=SecondPoint.x+3+m_PenWidth;
  158. point4[2].y=SecondPoint.y+4+m_PenWidth;
  159. point4[3].x=SecondPoint.x-3-m_PenWidth;
  160. point4[3].y=SecondPoint.y-4-m_PenWidth;
  161. }
  162. if((m_FirstPoint.x-m_SecondPoint.x)*(m_FirstPoint.y-m_SecondPoint.y)>0)
  163. {
  164. if(m_FirstPoint.x>m_SecondPoint.x)
  165. {
  166. FirstPoint=m_FirstPoint;
  167. SecondPoint=m_SecondPoint;
  168. }
  169. else
  170. {
  171. FirstPoint=m_SecondPoint;
  172. SecondPoint=m_FirstPoint;
  173. }
  174. FirstPoint.Offset(-4-m_PenWidth,-4-m_PenWidth);
  175. SecondPoint.Offset(4+m_PenWidth,4+m_PenWidth);
  176. point4[0].x=FirstPoint.x+3+m_PenWidth;
  177. point4[0].y=FirstPoint.y-4-m_PenWidth;
  178. point4[1].x=FirstPoint.x-3-m_PenWidth;
  179. point4[1].y=FirstPoint.y+4+m_PenWidth;
  180. point4[2].x=SecondPoint.x-3-m_PenWidth;
  181. point4[2].y=SecondPoint.y+4+m_PenWidth;
  182. point4[3].x=SecondPoint.x+3+m_PenWidth;
  183. point4[3].y=SecondPoint.y-4-m_PenWidth;
  184. }
  185. if((m_FirstPoint.x-m_SecondPoint.x)==0)
  186. {
  187. point4[0].x=m_FirstPoint.x-3-m_PenWidth;
  188. point4[0].y=m_FirstPoint.y-4-m_PenWidth;
  189. point4[1].x=m_FirstPoint.x+3+m_PenWidth;
  190. point4[1].y=m_FirstPoint.y-4-m_PenWidth;
  191. point4[2].x=m_SecondPoint.x+3+m_PenWidth;
  192. point4[2].y=m_SecondPoint.y+4+m_PenWidth;
  193. point4[3].x=m_SecondPoint.x-3-m_PenWidth;
  194. point4[3].y=m_SecondPoint.y+4+m_PenWidth;
  195. }
  196. if((m_FirstPoint.y-m_SecondPoint.y)==0)
  197. {
  198. point4[0].x=m_FirstPoint.x-4-m_PenWidth;
  199. point4[0].y=m_FirstPoint.y-3-m_PenWidth;
  200. point4[1].x=m_FirstPoint.x-4-m_PenWidth;
  201. point4[1].y=m_FirstPoint.y+3+m_PenWidth;
  202. point4[2].x=m_SecondPoint.x+4+m_PenWidth;
  203. point4[2].y=m_SecondPoint.y+3+m_PenWidth;
  204. point4[3].x=m_SecondPoint.x+4+m_PenWidth;
  205. point4[3].y=m_SecondPoint.y-3-m_PenWidth;
  206. }
  207. m_FirstPoint=m_SecondPoint;
  208. rgn.CreatePolygonRgn(point4,4,ALTERNATE);
  209. if(rgn.PtInRegion(point))
  210. return true;
  211. rgn.DeleteObject();
  212. }
  213. return false;
  214. }
  215. void CPolygon::ExchangeDraw(CDC *pDC, CPoint point)
  216. {
  217. //m_FoundIndex
  218. CPen m_pen;
  219. CBrush m_brush;
  220. int oldDrawingMode=pDC->SetROP2(this->m_DrawingMode);
  221. m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  222. CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  223. LOGBRUSH brushlog;
  224. brushlog.lbColor=m_BrushColor;
  225. brushlog.lbHatch=m_BrushHatch;
  226. brushlog.lbStyle=m_BrushStyle;
  227. m_brush.CreateBrushIndirect(&brushlog);
  228. CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  229. int OldBkMode=pDC->SetBkMode(m_BkMode);
  230. COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  231. m_SecondPoint=m_PointList->GetAt((m_FoundIndex+1)%m_PointList->GetSize());
  232. m_FirstPoint=m_PointList->GetAt(m_FoundIndex==0? m_PointList->GetSize()-1:m_FoundIndex-1);
  233. if(m_FoundIndex!=0)
  234. {
  235. pDC->MoveTo(m_FirstPoint);
  236. pDC->LineTo(point);
  237. pDC->MoveTo(point);
  238. pDC->LineTo(m_SecondPoint);
  239. }
  240. if(m_FoundIndex==0)
  241. {
  242. pDC->MoveTo(m_SecondPoint);
  243. pDC->LineTo(point);
  244. pDC->MoveTo(point);
  245. pDC->LineTo(m_FirstPoint);
  246. }
  247. m_PointList->SetAt(m_FoundIndex,point);
  248. pDC->SelectObject(brush);
  249. pDC->SelectObject(pen);
  250. pDC->SetBkMode(OldBkMode);
  251. pDC->SetBkColor(OldColor);
  252. pDC->SetROP2(oldDrawingMode);
  253. }
  254. void CPolygon::ShowSelectPoint(CDC *pDC)
  255. {
  256. CRect rect;
  257. CPoint firstPoint;
  258. CPoint secondPoint;
  259. firstPoint=secondPoint=m_PointList->GetAt(0);
  260. CBrush brush;
  261. brush.CreateSolidBrush(RGB(0,0,255));
  262. CPen m_pen;
  263. m_pen.CreatePen(PS_SOLID,1,RGB(0,0,255));
  264. CPen *OldPen=pDC->SelectObject(&m_pen);
  265. int oldBkMode=pDC->SetBkMode(OPAQUE);
  266. CBrush *OldBrush=pDC->SelectObject(&brush);
  267. int oldDrawingMode=pDC->SetROP2(R2_NOTXORPEN);
  268. for(int i=0;i<m_PointList->GetSize();i++)
  269. {
  270. rect=CRect(m_PointList->GetAt(i),m_PointList->GetAt(i));
  271. rect.InflateRect(3,3);
  272. pDC->Rectangle(rect);
  273. }
  274. pDC->SelectObject(OldBrush);
  275. brush.DeleteObject();
  276. brush.CreateSolidBrush(RGB(255,0,0));
  277. pDC->SelectObject(&brush);
  278. firstPoint=m_PointList->GetAt(0);
  279. //secondPoint=m_PointList->GetAt(1);
  280. for(i=1;i<m_PointList->GetSize();i++)
  281. {
  282. secondPoint=m_PointList->GetAt(i);
  283. rect.SetRect(CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2),CPoint((firstPoint.x+secondPoint.x)/2,(firstPoint.y+secondPoint.y)/2));
  284. rect.InflateRect(3,3);
  285. pDC->Rectangle(rect);
  286. firstPoint=secondPoint;
  287. }
  288. pDC->SelectObject(OldPen);
  289. pDC->SetBkMode(oldBkMode);
  290. pDC->SelectObject(OldBrush);
  291. pDC->SetROP2(oldDrawingMode);
  292. }
  293. void CPolygon::DrawActive(CDC *pDC,CPoint point)
  294. {
  295. CPen m_pen;
  296. CBrush m_brush;
  297. m_pen.CreatePen(m_PenStyle,m_PenWidth,m_PenColor);
  298. CPen* pen=(CPen*)pDC->SelectObject(&m_pen);
  299. LOGBRUSH brushlog;
  300. brushlog.lbColor=m_BrushColor;
  301. brushlog.lbHatch=m_BrushHatch;
  302. brushlog.lbStyle=m_BrushStyle;
  303. m_brush.CreateBrushIndirect(&brushlog);
  304. CBrush*brush=(CBrush*)pDC->SelectObject(&m_brush);
  305. int OldBkMode=pDC->SetBkMode(m_BkMode);
  306. COLORREF OldColor=pDC->SetBkColor(m_BackgroundColor);
  307. m_FirstPoint=m_PointList->GetAt(0);
  308. pDC->MoveTo(m_FirstPoint);
  309. for(int i=0;i<m_PointList->GetSize();i++)
  310. {
  311. pDC->LineTo(m_PointList->GetAt(i));
  312. }
  313. pDC->LineTo(m_PointList->GetAt(0));
  314. pDC->SelectObject(brush);
  315. pDC->SelectObject(pen);
  316. pDC->SetBkMode(OldBkMode);
  317. pDC->SetBkColor(OldColor);
  318. }
  319. void CPolygon::OnLButtonDown(CDC *pDC, CEastDrawView *pView, CPoint point)
  320. {
  321. if(m_HaveFindFirst)
  322. {
  323. pView->L_iPointCount=IsOnMarginPoint(point);
  324. if(pView->L_iPointCount>=0)
  325. {
  326. pView->m_bHaveFindSecond=true;
  327. pView->m_CurrentDrawTool=Polygon_Tool;
  328. pView->m_CurrentDrawStatus=Change_Status;
  329. }
  330. if(pView->L_iPointCount<-1)
  331. {
  332. pView->m_bHaveFindSecond=true;
  333. pView->m_CurrentDrawTool=Polygon_Tool;
  334. pView->m_CurrentDrawStatus=Drag_Status;
  335. }
  336. }//*******if(L_pPolygon->m_HaveFindFirst)
  337. if(!pView->m_bHaveFindSecond&&IsInRgn(point))
  338. {
  339. if(!m_HaveFindFirst)
  340. {
  341. m_HaveFindFirst=true;
  342. pView->m_bHaveFindFirst=true;
  343. ShowSelectPoint(pDC);
  344. }
  345. else
  346. {
  347. this->m_HaveFindFirst=false;
  348. pView->m_bHaveFindFirst=true;
  349. this->ShowSelectPoint(pDC);
  350. }
  351. }
  352. if(pView->m_bHaveFindSecond)
  353. {
  354. pView->m_pCurrentUnit=this;
  355. m_DrawingMode=pDC->GetROP2();
  356. ShowSelectPoint(pDC);
  357. DrawOldReferencePoint(pDC,m_FoundPoint);
  358. DrawStatic(pDC);
  359. }
  360. }
  361. void CPolygon::OnMouseMove(CDC *pDC, CEastDrawView *pView, CPoint point)
  362. {
  363. if(pView->m_CurrentDrawStatus==Draw_Status)
  364. {
  365. if(pView->m_LBDownTimes!=0)
  366. {
  367. pDC->SetROP2(R2_NOTXORPEN);
  368. //m_DrawingMode=pDC->GetROP2();
  369. DrawMask(pDC,pView->m_SecondPoint);
  370. pView->m_SecondPoint=point;
  371. pDC->SetROP2(this->m_DrawingMode);
  372. DrawMask(pDC,pView->m_SecondPoint);
  373. }
  374. }
  375. if(pView->m_CurrentDrawStatus==Change_Status)
  376. {
  377. pDC->SetROP2(R2_NOTXORPEN);
  378. ExchangeDraw(pDC,pView->m_SecondPoint);
  379. pView->m_SecondPoint=point;
  380. ExchangeDraw(pDC,pView->m_SecondPoint);
  381. }
  382. if(pView->m_CurrentDrawStatus==Drag_Status)
  383. {
  384. for(int i=0;i<m_PointList->GetSize();i++)
  385. {
  386. CPoint point=m_PointList->GetAt(i);
  387. point.Offset(pView->m_SecondPoint.x-pView->m_FirstPoint.x,pView->m_SecondPoint.y-pView->m_FirstPoint.y);
  388. m_PointList->SetAt(i,point);
  389. }
  390. }
  391. }