MyManageGraphObj.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:30k
源码类别:

绘图程序

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "MyManageGraphObj.h"
  3. #include "math.h"
  4. #include "MyDefine.h"
  5. #include "resource.h"
  6. //被选择图形对象信息
  7. CSelectedObjectInfo::CSelectedObjectInfo(int type, int index)
  8. {
  9. m_type = type;
  10. m_index = index;
  11. }
  12. ///////////////////////////////////////////////////////////////////////////////////////////////
  13. IMPLEMENT_DYNCREATE(CManageGraphObject, CObject)
  14. CManageGraphObject::CManageGraphObject()
  15. {
  16. Proportion = 1.0;
  17. LRepeatTimes = 0;
  18. RRepeatTimes = 0;
  19. }
  20. CManageGraphObject::~CManageGraphObject()
  21. {
  22. DelAllLine();
  23. DelAllRect();
  24. DelAllCircle();
  25. DelAllArc();
  26. }
  27. void CManageGraphObject::Initialize(int style, COLORREF color)
  28. {
  29. LineStyle = style;
  30. PenColor = color;
  31. }
  32. void CManageGraphObject::SetLineStyle(int style)
  33. {
  34. LineStyle = style;
  35. }
  36. void CManageGraphObject::SetPenColor(COLORREF color)
  37. {
  38. PenColor = color;
  39. }
  40. //获取命令堆栈指针
  41. void CManageGraphObject::GetCommandStack(CCommandStack* pStack)
  42. {
  43. pCommandStack = pStack;
  44. }
  45. void CManageGraphObject::GetDC(CClientDC* pClientDC)
  46. {
  47. ASSERT(pClientDC != NULL);
  48. pDC = pClientDC;
  49. }
  50. void CManageGraphObject::Serialize(CArchive& ar)
  51. {
  52. int num;
  53. int index;
  54. CGraphObjectInfo* pLineInfo;
  55. CGraphObjectInfo* pRectInfo;
  56. CGraphObjectInfo* pCircleInfo;
  57. CGraphObjectInfo* pArcInfo;
  58. if(ar.IsStoring())
  59. {
  60. ar << m_LineArray.GetSize() << m_RectArray.GetSize()
  61.    << m_CircleArray.GetSize() << m_ArcArray.GetSize();
  62. }
  63. else
  64. {
  65. ar >> num;
  66. for(index = 0; index < num; index++)
  67. {
  68. pLineInfo = new CGraphObjectInfo(0, FALSE, FALSE);
  69. m_LineInfoArray.Add(pLineInfo);
  70. }
  71. ar >> num;
  72. for(index = 0; index < num; index++)
  73. {
  74. pRectInfo = new CGraphObjectInfo(0, FALSE, FALSE);
  75. m_RectInfoArray.Add(pRectInfo);
  76. }
  77. ar >> num;
  78. for(index = 0; index < num; index++)
  79. {
  80. pCircleInfo = new CGraphObjectInfo(0, FALSE, FALSE);
  81. m_CircleInfoArray.Add(pCircleInfo);
  82. }
  83. ar >> num;
  84. for(index = 0; index < num; index++)
  85. {
  86. pArcInfo = new CGraphObjectInfo(0, FALSE, FALSE);
  87. m_ArcInfoArray.Add(pArcInfo);
  88. }
  89. }
  90. //注意当打开文件时,下面的Serialize()函数会自动创建CMyLine,CMyRect等对象
  91.     m_LineArray.Serialize(ar);
  92. m_RectArray.Serialize(ar);
  93. m_CircleArray.Serialize(ar);
  94. m_ArcArray.Serialize(ar);
  95. }
  96. ///////////////////////////////////////////////////////////////////////////////////////////////
  97. //处理命令
  98. void CManageGraphObject::OnLButtonDown(UINT ID, CPoint point)
  99. {
  100. ASSERT(pDC != NULL);
  101. ASSERT(pCommandStack != NULL);
  102. int ObjectType;
  103. int ObjetcIndex;
  104. switch(ID)
  105. {
  106. case DRAW_SELECT:
  107. SelectObject(pDC, point);
  108.     break;
  109. case DRAW_LINE:
  110. {
  111. LRepeatTimes++;
  112. switch(LRepeatTimes)
  113. {
  114. case 1:
  115. StartPoint = point;
  116. m_DrawGrapObject.SetOnceFlag();
  117. break;
  118. case 2:
  119. EndPoint = point;
  120.         m_DrawGrapObject.DrawLine(pDC, StartPoint, EndPoint, LineStyle, PenColor);
  121.         AddLine(StartPoint, EndPoint, LineStyle, PenColor);
  122.         ObjectType = OBJECT_LINE;
  123.         ObjetcIndex = m_LineArray.GetUpperBound();
  124.         //因为执行一项新操作后,将不再有可以Redo的命令
  125.         pCommandStack->PushCommand(DRAW_LINE, 1, &ObjectType, &ObjetcIndex);
  126.         
  127.         LRepeatTimes--;
  128.         StartPoint = EndPoint;
  129. oldEndPoint = StartPoint;
  130.         break;
  131. }
  132. }
  133. break;
  134.     case DRAW_RECT:
  135. {
  136. LRepeatTimes++;
  137. switch(LRepeatTimes)
  138. {
  139. case 1:
  140. StartPoint = point;
  141. m_DrawGrapObject.SetOnceFlag();
  142. break;
  143. case 2:
  144. EndPoint = point;
  145. m_DrawGrapObject.DrawRect(pDC, StartPoint, EndPoint, LineStyle, PenColor);
  146. AddRect(StartPoint, EndPoint, LineStyle, PenColor);
  147.         ObjectType = OBJECT_RECT;
  148. ObjetcIndex = m_RectArray.GetUpperBound();
  149. pCommandStack->PushCommand(DRAW_RECT, 1, &ObjectType, &ObjetcIndex);
  150.         LRepeatTimes = 0;
  151. break;
  152. }
  153. }
  154. break;
  155.     case DRAW_CIRCLE:
  156. {
  157. LRepeatTimes++;
  158.     switch(LRepeatTimes)
  159. {
  160. case 1:
  161. StartPoint = point;
  162.         m_DrawGrapObject.SetOnceFlag();
  163. break;
  164. case 2:
  165. EndPoint = point;
  166. m_DrawGrapObject.DrawCircle(pDC, StartPoint, EndPoint, LineStyle, PenColor);
  167.         AddCircle(StartPoint, EndPoint, LineStyle, PenColor);
  168.             ObjectType = OBJECT_CIRCLE;
  169.         ObjetcIndex = m_CircleArray.GetUpperBound();
  170.         pCommandStack->PushCommand(DRAW_CIRCLE, 1, &ObjectType, &ObjetcIndex);
  171.         LRepeatTimes = 0;
  172. break;
  173. }
  174. }
  175. break;
  176.     case DRAW_ARC:
  177. {
  178. LRepeatTimes++;
  179.     switch(LRepeatTimes)
  180. {
  181. case 1:
  182. StartPoint = point;
  183. m_DrawGrapObject.SetOnceFlag();
  184.         break;
  185. case 2:
  186. MidPoint = point;
  187. m_DrawGrapObject.SetOnceFlag();
  188. break;
  189. case 3:
  190. EndPoint = point;
  191. m_DrawGrapObject.DrawArc(pDC, StartPoint, MidPoint, EndPoint, 
  192.                      LineStyle, PenColor);
  193. AddArc(StartPoint, MidPoint, EndPoint, LineStyle, PenColor); 
  194.      ObjectType = OBJECT_ARC;
  195. ObjetcIndex = m_ArcArray.GetUpperBound();
  196. pCommandStack->PushCommand(DRAW_ARC, 1, &ObjectType, &ObjetcIndex);
  197. LRepeatTimes = 0;
  198. break;
  199. }
  200. }
  201. break;
  202. case DRAW_TEXT:
  203. break;
  204. }
  205. }
  206. UINT CManageGraphObject::OnRButtonDown(UINT ID, CPoint point)
  207. {
  208. ASSERT(pDC != NULL);
  209. switch(ID)
  210. {
  211. case DRAW_SELECT:
  212. //if(UnselectObject())
  213. //Invalidate();
  214. break;
  215. case DRAW_LINE:
  216. if(LRepeatTimes == 1)
  217. {
  218. m_DrawGrapObject.CancelDrawLine(pDC, StartPoint, EndPoint);
  219.     LRepeatTimes = 0;
  220. }
  221.     else 
  222. {
  223. ID = DRAW_SELECT;
  224. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  225. LRepeatTimes = 0;
  226. }
  227. break;
  228. case DRAW_RECT:
  229. if(LRepeatTimes == 1)
  230. {
  231. m_DrawGrapObject.CancelDrawRect(pDC, StartPoint, EndPoint);
  232. LRepeatTimes = 0;
  233. }
  234. else
  235. {
  236. ID = DRAW_SELECT;
  237. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  238. LRepeatTimes = 0;
  239. }
  240. break;
  241. case DRAW_CIRCLE:
  242. if(LRepeatTimes == 1)
  243. {
  244. m_DrawGrapObject.CancelDrawCircle(pDC, StartPoint, EndPoint);
  245. LRepeatTimes = 0;
  246. }
  247. else
  248. {
  249. ID = DRAW_SELECT;
  250. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  251. LRepeatTimes = 0;
  252. }
  253. break;
  254. case DRAW_ARC:
  255. if(LRepeatTimes > 0)
  256. {
  257. m_DrawGrapObject.CancelDrawArc(pDC, StartPoint, MidPoint, EndPoint, LRepeatTimes);
  258.     LRepeatTimes = 0;
  259. }
  260. else
  261. {
  262. ID = DRAW_SELECT;
  263. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  264. LRepeatTimes = 0;
  265. }
  266. break;
  267. }
  268. return ID;
  269. }
  270. void CManageGraphObject::OnMouseMove(UINT ID, CPoint point)
  271. {
  272. ASSERT(pDC != NULL);
  273. switch(ID)
  274. {
  275. case DRAW_SELECT:
  276. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  277. break;
  278. case DRAW_LINE:
  279. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  280. if(LRepeatTimes == 1)
  281. {
  282. EndPoint = point;
  283. m_DrawGrapObject.DrawLineXOR(pDC, StartPoint, EndPoint, oldEndPoint);
  284. oldEndPoint = EndPoint;
  285. }
  286. break;
  287. case DRAW_RECT:
  288. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  289. if(LRepeatTimes == 1)
  290. {
  291. EndPoint = point;
  292. m_DrawGrapObject.DrawRectXOR(pDC, StartPoint, EndPoint, oldEndPoint);
  293. oldEndPoint = EndPoint;
  294. }
  295. break;
  296. case DRAW_CIRCLE:
  297. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  298. if(LRepeatTimes == 1)
  299. {
  300. EndPoint = point;
  301. m_DrawGrapObject.DrawCircleXOR(pDC, StartPoint, EndPoint, oldEndPoint);
  302.                 oldEndPoint = EndPoint;
  303. }
  304. break;
  305. case DRAW_ARC:
  306. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  307. switch(LRepeatTimes)
  308. {
  309. case 1:
  310. MidPoint = point;
  311. m_DrawGrapObject.DrawArcXOR(pDC, StartPoint, MidPoint, oldMidPoint,
  312.                         EndPoint, oldEndPoint, LRepeatTimes);
  313. oldMidPoint = MidPoint;
  314. break;
  315. case 2:
  316. EndPoint = point;
  317. m_DrawGrapObject.DrawArcXOR(pDC, StartPoint, MidPoint, oldMidPoint,
  318.                         EndPoint, oldEndPoint, LRepeatTimes);
  319. oldEndPoint = EndPoint;
  320. break;
  321. }
  322. break;
  323. }
  324. }
  325. void CManageGraphObject::OnScroll(UINT ID)
  326. {
  327. ASSERT(pDC != NULL);
  328. switch(ID)
  329. {
  330. case DRAW_LINE:
  331. if(LRepeatTimes == 1)
  332. {
  333. m_DrawGrapObject.CancelDrawLine(pDC, StartPoint, EndPoint);
  334.                 oldEndPoint = StartPoint;
  335.     EndPoint = StartPoint;
  336. m_DrawGrapObject.SetOnceFlag();
  337. }
  338. break;
  339. case DRAW_RECT:
  340. if(LRepeatTimes == 1)
  341. {
  342. m_DrawGrapObject.CancelDrawRect(pDC, StartPoint, EndPoint);
  343. oldEndPoint = StartPoint;
  344.     EndPoint = StartPoint;
  345. m_DrawGrapObject.SetOnceFlag();
  346. }
  347. break;
  348. case DRAW_CIRCLE:
  349. if(LRepeatTimes == 1)
  350. {
  351. m_DrawGrapObject.CancelDrawCircle(pDC, StartPoint, EndPoint);
  352. oldEndPoint = StartPoint;
  353. EndPoint = StartPoint;
  354. m_DrawGrapObject.SetOnceFlag();
  355. }
  356. break;
  357. case DRAW_ARC:
  358. m_DrawGrapObject.CancelDrawArc(pDC, StartPoint, MidPoint, EndPoint, LRepeatTimes);
  359. if(LRepeatTimes == 1)
  360. {
  361. oldMidPoint = StartPoint;
  362. MidPoint = StartPoint;
  363. }
  364. if(LRepeatTimes == 2)
  365. {
  366. oldEndPoint = StartPoint;
  367. EndPoint = StartPoint;
  368. }
  369.             m_DrawGrapObject.SetOnceFlag();
  370. break;
  371. }
  372.  
  373. }
  374. //实现“撤销”命令
  375. void CManageGraphObject::OnUndo()
  376. {
  377. CCommandStruct* pCommand;
  378. CGraphObjectInfo* pLineInfo;
  379. CGraphObjectInfo* pRectInfo;
  380. CGraphObjectInfo* pCircleInfo;
  381. CGraphObjectInfo* pArcInfo;
  382. pCommand = (CCommandStruct*)pCommandStack->Undo();
  383.     if(pCommand == NULL)
  384. return;
  385.     switch(pCommand->m_command)
  386. {
  387. case DRAW_LINE:
  388. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[0]);
  389.         pLineInfo->m_del = TRUE;
  390. break;
  391. case DRAW_RECT:
  392. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[0]);
  393.         pRectInfo->m_del = TRUE;
  394. break;
  395. case DRAW_CIRCLE:
  396. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[0]);
  397.         pCircleInfo->m_del = TRUE;
  398. break;
  399. case DRAW_ARC:
  400. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[0]);
  401.         pArcInfo->m_del = TRUE;
  402. break;
  403. case CHANGE_DELETE:
  404. for(int index =0; index < pCommand->m_num; index++)
  405. {
  406. switch(pCommand->m_object[index])
  407. {
  408. case OBJECT_LINE:
  409. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[index]);
  410. pLineInfo->m_del = FALSE;
  411. break;
  412. case OBJECT_RECT:
  413. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[index]);
  414. pRectInfo->m_del = FALSE;
  415. break;
  416. case OBJECT_CIRCLE:
  417. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[index]);
  418. pCircleInfo->m_del = FALSE;
  419. break;
  420. case OBJECT_ARC:
  421. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[index]);
  422. pArcInfo->m_del = FALSE;
  423. break;
  424. }
  425. }
  426. break;
  427. }
  428. }
  429. //实现“重做”命令
  430. void CManageGraphObject::OnRedo()
  431. {
  432. CCommandStruct* pCommand;
  433. CGraphObjectInfo* pLineInfo;
  434. CGraphObjectInfo* pRectInfo;
  435. CGraphObjectInfo* pCircleInfo;
  436. CGraphObjectInfo* pArcInfo;
  437. pCommand = (CCommandStruct*)pCommandStack->Redo();
  438. if(pCommand == NULL)
  439. return;
  440.     switch(pCommand->m_command)
  441. {
  442. case DRAW_LINE:
  443. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[0]);
  444.         pLineInfo->m_del = FALSE;
  445. break;
  446. case DRAW_RECT:
  447. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[0]);
  448.         pRectInfo->m_del = FALSE;
  449. break;
  450. case DRAW_CIRCLE:
  451. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[0]);
  452.         pCircleInfo->m_del = FALSE;
  453. break;
  454. case DRAW_ARC:
  455. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[0]);
  456.         pArcInfo->m_del = FALSE;
  457. break;
  458. case CHANGE_DELETE:
  459. for(int index =0; index < pCommand->m_num; index++)
  460. {
  461. switch(pCommand->m_object[index])
  462. {
  463. case OBJECT_LINE:
  464. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[index]);
  465. pLineInfo->m_del = TRUE;
  466. break;
  467. case OBJECT_RECT:
  468. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[index]);
  469. pRectInfo->m_del = TRUE;
  470. break;
  471. case OBJECT_CIRCLE:
  472. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[index]);
  473. pCircleInfo->m_del = TRUE;
  474. break;
  475. case OBJECT_ARC:
  476. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[index]);
  477. pArcInfo->m_del = TRUE;
  478. break;
  479. }
  480. }
  481. break;
  482. }
  483. }
  484. //////////////////////////////////////////////////////////////////////////////////////////////
  485. POINT CManageGraphObject::DrawingToLogic(FPOINT fpoint)
  486. {
  487. POINT point;
  488. point.x = long(fpoint.x * Proportion) + EXTRA_WIDTH;
  489. point.y = PageSize.cy - (long)(fpoint.y * Proportion) - EXTRA_HIGTH;
  490. return point;
  491. }
  492. long CManageGraphObject::DrawingToLogic(float fdistance)
  493. {
  494. long distance;
  495. distance = (long)(fdistance * Proportion);
  496. return distance;
  497. }
  498. FPOINT CManageGraphObject::LogicToDrawing(POINT point)
  499. {
  500. FPOINT fpoint;
  501. fpoint.x = (float)(point.x - EXTRA_WIDTH) / Proportion;
  502. fpoint.y = (float)(PageSize.cy - point.y  - EXTRA_HIGTH) / Proportion;
  503. return fpoint;
  504. }
  505. float CManageGraphObject::LogicToDrawing(long distance)
  506. {
  507. float fdistance;
  508. fdistance = (float)distance / Proportion;
  509. return fdistance;
  510. }
  511. ////////////////////////////////////////////////////////////////////////////////////////////////
  512. void CManageGraphObject::RedrawAllObject(CDC* pDC)
  513. {
  514. RedrawLine(pDC);
  515. RedrawRect(pDC);
  516. RedrawCircle(pDC);
  517. RedrawArc(pDC);
  518. }
  519. void CManageGraphObject::SelectObject(CDC* pDC, CPoint point)
  520. {
  521. SelectLine(pDC, point);   
  522. SelectRect(pDC, point);
  523. SelectCircle(pDC, point);
  524. SelectArc(pDC, point);
  525. }
  526. BOOL CManageGraphObject::UnselectObject()
  527. {
  528. BOOL flag = FALSE;
  529. int nIndex;
  530. if(UnselectLine())
  531. flag = TRUE;
  532. if(UnselectRect())
  533. flag = TRUE;
  534. if(UnselectCircle())
  535. flag = TRUE;
  536.     if(UnselectArc())
  537. flag = TRUE;
  538.     
  539. nIndex = m_SelObjInfoArray.GetSize();
  540. while(nIndex--)
  541. delete (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  542.     m_SelObjInfoArray.RemoveAll();
  543. return flag;
  544. }
  545. //////////////////////////////////////////////////////////////////////////////////////////////
  546. void CManageGraphObject::RedrawLine(CDC* pDC)
  547. {
  548. int index;
  549. CMyLine* pLine;
  550. CGraphObjectInfo* pLineInfo;
  551. index = m_LineArray.GetSize();
  552. while(index--)
  553. {
  554. pLine = (CMyLine*)m_LineArray.GetAt(index);
  555. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(index);
  556. if(pLineInfo->m_del == TRUE)
  557. continue;
  558. if(pLineInfo->m_selected == TRUE)
  559. {
  560. m_DrawGrapObject.DrawSelLine(pDC, DrawingToLogic(pLine->m_start), DrawingToLogic(pLine->m_end),
  561.                          pLine->m_style, pLine->m_color);
  562. continue;
  563. }
  564.         
  565. m_DrawGrapObject.DrawLine(pDC, DrawingToLogic(pLine->m_start), DrawingToLogic(pLine->m_end),
  566.                    pLine->m_style, pLine->m_color);
  567.      }
  568. }
  569. void CManageGraphObject::SelectLine(CDC* pDC, CPoint point)
  570. {
  571. int nIndex;
  572. long distance;
  573. CRect rect;
  574. CGraphObjectInfo* pLineInfo;
  575. CSelectedObjectInfo* pSelObjInfo;
  576. CMyLine* pLine; 
  577. for(nIndex = 0; nIndex < m_LineInfoArray.GetSize(); nIndex++)
  578. {
  579. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(nIndex);
  580. if(pLineInfo ->m_del == TRUE || pLineInfo ->m_selected ==TRUE)
  581. continue;
  582. pLine = (CMyLine*)m_LineArray.GetAt(nIndex);
  583. rect.left = DrawingToLogic(pLine->m_start).x;
  584. rect.top = DrawingToLogic(pLine->m_start).y;
  585. rect.right = DrawingToLogic(pLine->m_end).x;
  586. rect.bottom = DrawingToLogic(pLine->m_end).y;
  587. if( (point.x >= min(rect.left, rect.right) - SELECT_RANGE) && (point.x <= max(rect.left, rect.right) + SELECT_RANGE) &&
  588.     (point.y >= min(rect.top, rect.bottom) - SELECT_RANGE) && (point.y <= max(rect.top, rect.bottom) + SELECT_RANGE) )
  589. {
  590. distance = (long)( abs( (rect.bottom - rect.top) * point.x - (rect.right - rect.left) * point.y + rect.top * (rect.right - rect.left) - rect.left * (rect.bottom - rect.top) )
  591.        / sqrt( (rect.right - rect.left) * (rect.right - rect.left) + (rect.bottom - rect.top) * (rect.bottom - rect.top) ) );
  592. if(distance <= SELECT_RANGE)
  593. {
  594.            m_DrawGrapObject.DrawSelLine(pDC, DrawingToLogic(pLine->m_start), DrawingToLogic(pLine->m_end),
  595.                          pLine->m_style, pLine->m_color);
  596. pLineInfo->m_selected = TRUE;
  597. pSelObjInfo = new CSelectedObjectInfo(OBJECT_LINE, nIndex);
  598. m_SelObjInfoArray.Add(pSelObjInfo);
  599. }
  600. }
  601. }
  602. }
  603. BOOL CManageGraphObject::UnselectLine()
  604. {
  605. BOOL flag = FALSE;
  606. int nIndex;
  607. CSelectedObjectInfo* pSelObjInfo;
  608. CGraphObjectInfo* pLineInfo;
  609. for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
  610. {
  611. pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  612. if(pSelObjInfo->m_type ==OBJECT_LINE)
  613. {
  614. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pSelObjInfo->m_index);
  615. pLineInfo->m_selected = FALSE;
  616. flag = TRUE;
  617.         }
  618.     }
  619. return flag;
  620. }
  621. void CManageGraphObject::AddLine(POINT SPoint, POINT EPoint, int Style, COLORREF Color)
  622. {
  623. FPOINT start;
  624. FPOINT end;
  625. start = LogicToDrawing(SPoint);
  626. end = LogicToDrawing(EPoint);
  627. CMyLine* pMyLine = new CMyLine(start, end, Style, Color);
  628. m_LineArray.Add(pMyLine);
  629. CGraphObjectInfo * pLineInfo = new CGraphObjectInfo(0, FALSE, FALSE);
  630. m_LineInfoArray.Add(pLineInfo);
  631. }
  632. CMyLine* CManageGraphObject::GetLine(int index)
  633. {
  634. if(index < 0 || index > m_LineArray.GetUpperBound())
  635. return 0;
  636. return (CMyLine*)m_LineArray.GetAt(index);
  637. }
  638. void CManageGraphObject::DelAllLine()
  639. {
  640. int index;
  641. index = m_LineArray.GetSize();
  642. while(index--)
  643. delete (CMyLine*)m_LineArray.GetAt(index);
  644. m_LineArray.RemoveAll();
  645.      
  646. index = m_LineInfoArray.GetSize();
  647. while(index--)
  648. delete (CGraphObjectInfo*)m_LineInfoArray.GetAt(index);
  649. m_LineInfoArray.RemoveAll();
  650. }
  651. //////////////////////////////////////////////////////////////////////////////////////////////
  652. void CManageGraphObject::RedrawRect(CDC* pDC)
  653. {
  654. int index;
  655. CMyRect* pRect;
  656. CGraphObjectInfo* pRectInfo;
  657. index = m_RectArray.GetSize();
  658. //设置不填充状态
  659. pDC->SelectStockObject(NULL_BRUSH);
  660. while(index--)
  661. {
  662. pRect = (CMyRect*)m_RectArray.GetAt(index);
  663. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(index);
  664. if(pRectInfo->m_del == TRUE)
  665. continue;
  666. if(pRectInfo->m_selected == TRUE)
  667. {
  668. m_DrawGrapObject.DrawSelRect(pDC, DrawingToLogic(pRect->m_start), DrawingToLogic(pRect->m_end),
  669.                          pRect->m_style, pRect->m_color);
  670. continue;
  671. }
  672. m_DrawGrapObject.DrawRect(pDC, DrawingToLogic(pRect->m_start), DrawingToLogic(pRect->m_end),
  673.                    pRect->m_style, pRect->m_color);
  674.       
  675. }
  676. }
  677. void CManageGraphObject::SelectRect(CDC* pDC, CPoint point)
  678. {
  679. int nIndex;
  680. CRect rect;
  681. CGraphObjectInfo* pRectInfo;
  682. CSelectedObjectInfo* pSelObjInfo;
  683. CMyRect* pRect; 
  684. for(nIndex = 0; nIndex < m_RectInfoArray.GetSize(); nIndex++)
  685. {
  686. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(nIndex);
  687. if(pRectInfo ->m_del == TRUE || pRectInfo ->m_selected ==TRUE)
  688. continue;
  689. pRect = (CMyRect*)m_RectArray.GetAt(nIndex);
  690. rect.left = DrawingToLogic(pRect->m_start).x;
  691. rect.top = DrawingToLogic(pRect->m_start).y;
  692. rect.right = DrawingToLogic(pRect->m_end).x;
  693. rect.bottom = DrawingToLogic(pRect->m_end).y;
  694. if( ( (point.x >= min(rect.left, rect.right) - SELECT_RANGE) && (point.x <= max(rect.left, rect.right) + SELECT_RANGE) &&
  695.     (point.y >= min(rect.top, rect.bottom) - SELECT_RANGE) && (point.y <= max(rect.top, rect.bottom) + SELECT_RANGE) ) &&
  696. ( (point.x <= min(rect.left, rect.right) + SELECT_RANGE) || (point.x >= max(rect.left, rect.right) - SELECT_RANGE) ||
  697.             (point.y <= min(rect.top, rect.bottom) + SELECT_RANGE) || (point.y >= max(rect.top, rect.bottom) - SELECT_RANGE) ) ) 
  698. {
  699. m_DrawGrapObject.DrawSelRect(pDC, rect.TopLeft(), rect.BottomRight(), 
  700.                          pRect->m_style, pRect->m_color);
  701. pRectInfo->m_selected = TRUE;
  702. pSelObjInfo = new CSelectedObjectInfo(OBJECT_RECT, nIndex);
  703. m_SelObjInfoArray.Add(pSelObjInfo);
  704. }
  705. }
  706. }
  707. BOOL CManageGraphObject::UnselectRect()
  708. {
  709. BOOL flag = FALSE;
  710. int nIndex;
  711. CSelectedObjectInfo* pSelObjInfo;
  712. CGraphObjectInfo* pRectInfo;
  713. for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
  714. {
  715. pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  716. if(pSelObjInfo->m_type ==OBJECT_RECT)
  717. {
  718. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pSelObjInfo->m_index);
  719. pRectInfo->m_selected = FALSE;
  720. flag = TRUE;
  721.         }
  722.     }
  723. return flag;
  724. }
  725. void CManageGraphObject::AddRect(POINT SPoint, POINT EPoint, int Style, COLORREF Color)
  726. {
  727. FPOINT start;
  728. FPOINT end;
  729. start = LogicToDrawing(SPoint);
  730. end = LogicToDrawing(EPoint);
  731.     
  732. CMyRect* pMyRect = new CMyRect(start, end, Style, Color);
  733. m_RectArray.Add(pMyRect);
  734. CGraphObjectInfo * pRectInfo = new CGraphObjectInfo(0, FALSE, FALSE);
  735. m_RectInfoArray.Add(pRectInfo);
  736. }
  737. CMyRect* CManageGraphObject::GetRect(int index)
  738. {
  739. if(index < 0 || index > m_RectArray.GetUpperBound())
  740. return 0;
  741. return (CMyRect*)m_RectArray.GetAt(index);
  742. }
  743. void CManageGraphObject::DelAllRect()
  744. {
  745. int index;
  746. index = m_RectArray.GetSize();
  747. while(index--)
  748. delete (CMyRect*)m_RectArray.GetAt(index);
  749. m_RectArray.RemoveAll();
  750. index = m_RectInfoArray.GetSize();
  751. while(index--)
  752. delete (CGraphObjectInfo*)m_RectInfoArray.GetAt(index);
  753. m_RectInfoArray.RemoveAll();
  754. }
  755. //////////////////////////////////////////////////////////////////////////////////////////////
  756. void CManageGraphObject::RedrawCircle(CDC* pDC)
  757. {
  758. int index;
  759. CMyCircle* pCircle;
  760. CGraphObjectInfo* pCircleInfo;
  761. index = m_CircleArray.GetSize();
  762. //设置不填充状态
  763. pDC->SelectStockObject(NULL_BRUSH);
  764. while(index--)
  765. {
  766. pCircle = (CMyCircle*)m_CircleArray.GetAt(index);
  767. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(index);
  768. if(pCircleInfo->m_del == TRUE)
  769. continue;
  770. if(pCircleInfo->m_selected == TRUE)
  771. {
  772. m_DrawGrapObject.DrawSelCircle(pDC, DrawingToLogic(pCircle->m_origin), DrawingToLogic(pCircle->m_radius), 
  773.                            pCircle->m_style, pCircle->m_color);
  774. continue;
  775. }
  776. m_DrawGrapObject.DrawCircle(pDC, DrawingToLogic(pCircle->m_origin), DrawingToLogic(pCircle->m_radius),
  777.                          pCircle->m_style, pCircle->m_color);
  778.    }
  779. }
  780. void CManageGraphObject::SelectCircle(CDC* pDC, CPoint point)
  781. {
  782. int nIndex;
  783. POINT origin;
  784. long r;
  785. long distance;
  786. CGraphObjectInfo* pCircleInfo;
  787. CSelectedObjectInfo* pSelObjInfo;
  788. CMyCircle* pCircle; 
  789. for(nIndex = 0; nIndex < m_CircleInfoArray.GetSize(); nIndex++)
  790. {
  791. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(nIndex);
  792. if(pCircleInfo ->m_del == TRUE || pCircleInfo ->m_selected ==TRUE)
  793. continue;
  794. pCircle = (CMyCircle*)m_CircleArray.GetAt(nIndex);
  795. origin = DrawingToLogic(pCircle->m_origin);
  796. r = DrawingToLogic(pCircle->m_radius);
  797. distance = (long)sqrt( (origin.x - point.x) * (origin.x - point.x) + (origin.y - point.y) * (origin.y - point.y) );
  798. if(distance <= r + SELECT_RANGE && distance >= r - SELECT_RANGE)
  799. {
  800. m_DrawGrapObject.DrawSelCircle(pDC, DrawingToLogic(pCircle->m_origin), DrawingToLogic(pCircle->m_radius),
  801.                            pCircle->m_style, pCircle->m_color);
  802. pCircleInfo->m_selected = TRUE;
  803. pSelObjInfo = new CSelectedObjectInfo(OBJECT_CIRCLE, nIndex);
  804. m_SelObjInfoArray.Add(pSelObjInfo);
  805. }
  806. }
  807. }
  808. BOOL CManageGraphObject::UnselectCircle()
  809. {
  810. BOOL flag = FALSE;
  811. int nIndex;
  812. CSelectedObjectInfo* pSelObjInfo;
  813. CGraphObjectInfo* pCircleInfo;
  814. for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
  815. {
  816. pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  817. if(pSelObjInfo->m_type ==OBJECT_CIRCLE)
  818. {
  819. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pSelObjInfo->m_index);
  820. pCircleInfo->m_selected = FALSE;
  821. flag = TRUE;
  822.         }
  823.     }
  824. return flag;
  825. }
  826. void CManageGraphObject::AddCircle(POINT SPoint, POINT EPoint, int Style, COLORREF Color)
  827. {
  828. FPOINT start;
  829. FPOINT end;
  830. float r;
  831. start = LogicToDrawing(SPoint);
  832. end = LogicToDrawing(EPoint);
  833. r = (float)sqrt(pow(start.x - end.x, 2) + pow(start.y - end.y, 2));
  834. CMyCircle* pMyCircle = new CMyCircle(start, r, Style, Color);
  835. m_CircleArray.Add(pMyCircle);
  836. CGraphObjectInfo* pCircleInfo = new CGraphObjectInfo(0, FALSE, FALSE);
  837. m_CircleInfoArray.Add(pCircleInfo);
  838. }
  839. CMyCircle* CManageGraphObject::GetCircle(int index)
  840. {
  841. if(index < 0 || index > m_CircleArray.GetUpperBound())
  842. return 0;
  843. return (CMyCircle*)m_CircleArray.GetAt(index);
  844. }
  845. void CManageGraphObject::DelAllCircle()
  846. {
  847. int index;
  848. index = m_CircleArray.GetSize();
  849. while(index--)
  850. delete (CMyCircle*)m_CircleArray.GetAt(index);
  851. m_CircleArray.RemoveAll();
  852. index = m_CircleInfoArray.GetSize();
  853. while(index--)
  854. delete (CGraphObjectInfo*)m_CircleInfoArray.GetAt(index);
  855. m_CircleInfoArray.RemoveAll();
  856. }
  857. //////////////////////////////////////////////////////////////////////////////////////////////
  858. void CManageGraphObject::RedrawArc(CDC* pDC)
  859. {
  860. int index;
  861. CMyArc* pArc;
  862. CGraphObjectInfo* pArcInfo;
  863. index = m_ArcArray.GetSize();
  864. //设置不填充状态
  865. pDC->SelectStockObject(NULL_BRUSH);
  866. while(index--)
  867. {
  868. pArc = (CMyArc*)m_ArcArray.GetAt(index);
  869. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(index);
  870. if(pArcInfo->m_del == TRUE)
  871. continue;
  872. if(pArcInfo->m_selected == TRUE)
  873. {
  874. m_DrawGrapObject.DrawSelArc(pDC, DrawingToLogic(pArc->m_origin), DrawingToLogic(pArc->m_start), 
  875.                         DrawingToLogic(pArc->m_end),
  876. DrawingToLogic(pArc->m_radius), LineStyle, PenColor);
  877. continue;
  878. }
  879.          
  880. m_DrawGrapObject.DrawArc(pDC, DrawingToLogic(pArc->m_origin),
  881.                      DrawingToLogic(pArc->m_start), DrawingToLogic(pArc->m_end),
  882.                                  DrawingToLogic(pArc->m_radius), pArc->m_style, pArc->m_color);
  883. }
  884. }
  885. void CManageGraphObject::SelectArc(CDC* pDC, CPoint point)
  886. {
  887. int nIndex;
  888. POINT origin;
  889. long r;
  890. long distance;
  891. CPoint start;
  892. CPoint end;
  893. CGraphObjectInfo* pArcInfo;
  894. CSelectedObjectInfo* pSelObjInfo;
  895. CMyArc* pArc; 
  896. for(nIndex = 0; nIndex < m_ArcInfoArray.GetSize(); nIndex++)
  897. {
  898. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(nIndex);
  899. if(pArcInfo ->m_del == TRUE || pArcInfo ->m_selected ==TRUE)
  900. continue;
  901. pArc = (CMyArc*)m_ArcArray.GetAt(nIndex);
  902. origin = DrawingToLogic(pArc->m_origin);
  903. start = DrawingToLogic(pArc->m_start);
  904. end = DrawingToLogic(pArc->m_end);
  905. r = DrawingToLogic(pArc->m_radius);
  906. distance = (long)sqrt( (origin.x - point.x) * (origin.x - point.x) + (origin.y - point.y) * (origin.y - point.y) );
  907. if(distance <= r + SELECT_RANGE && distance >= r - SELECT_RANGE)
  908. {
  909. m_DrawGrapObject.DrawSelArc(pDC, origin, start, end, r, pArc->m_style, pArc->m_color);
  910. pArcInfo->m_selected = TRUE;
  911. pSelObjInfo = new CSelectedObjectInfo(OBJECT_ARC, nIndex);
  912. m_SelObjInfoArray.Add(pSelObjInfo);
  913. }
  914. }
  915. }
  916. BOOL CManageGraphObject::UnselectArc()
  917. {
  918. BOOL flag = FALSE;
  919. int nIndex;
  920. CSelectedObjectInfo* pSelObjInfo;
  921. CGraphObjectInfo* pArcInfo;
  922. for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
  923. {
  924. pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  925. if(pSelObjInfo->m_type ==OBJECT_ARC)
  926. {
  927. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pSelObjInfo->m_index);
  928. pArcInfo->m_selected = FALSE;
  929. flag = TRUE;
  930.         }
  931.     }
  932. return flag;
  933. }
  934. void CManageGraphObject::AddArc(POINT SPoint, POINT MPoint, POINT EPoint, int Style, COLORREF Color)
  935. {
  936. long x1, y1, x2, y2;
  937. double originX;
  938. double originY;
  939. double k1;
  940. double k2;
  941. double mid1X;
  942. double mid1Y;
  943. double mid2X;
  944. double mid2Y;
  945. POINT OPoint;
  946. long Radius;
  947. CMyArc* pMyArc;
  948. if(SPoint.y == MPoint.y)
  949. k1 = 1000000.0;
  950. else 
  951. k1 = (- (double)(SPoint.x - MPoint.x) / (double)(SPoint.y - MPoint.y));
  952. if(EPoint.y == MPoint.y)
  953. k2 = 1000000.0;
  954. else 
  955. k2 =  (- (double)(EPoint.x - MPoint.x) / (double)(EPoint.y - MPoint.y));
  956. mid1X = (double)(SPoint.x + MPoint.x) / 2.0;
  957. mid1Y = (double)(SPoint.y + MPoint.y) / 2.0;
  958. mid2X = (double)(EPoint.x + MPoint.x) / 2.0;
  959. mid2Y = (double)(EPoint.y + MPoint.y) / 2.0;
  960. if(k1 == k2)
  961. return;
  962. if(k1 == 1000000.0)
  963. {
  964. originX = mid1X;
  965. originY = k2 * (originX - mid2X) + mid2Y;
  966. }
  967. else
  968. if(k2 == 1000000.0)
  969. {
  970. originX = mid2X;
  971. originY = k1 * (originX - mid1X) + mid1Y;
  972. }
  973. else
  974. {
  975. originX = ((k2 * mid2X - k1 * mid1X) - (mid2Y - mid1Y)) / (k2 - k1);
  976. originY = k2 * (originX - mid2X) + mid2Y;
  977. }
  978. Radius = (long)sqrt(pow((double)(originX - SPoint.x), 2) + pow((double)(originY - SPoint.y), 2));
  979. OPoint.x = (long)originX;
  980. OPoint.y = (long)originY ;
  981. x1 = MidPoint.x - SPoint.x;
  982. y1 = MPoint.y - SPoint.y;
  983. x2 = EPoint.x - SPoint.x;
  984. y2 = EPoint.y - SPoint.y;
  985. if(x1*y2 - x2*y1 < 0)
  986. pMyArc = new CMyArc(LogicToDrawing(OPoint), LogicToDrawing(SPoint), 
  987.                     LogicToDrawing(EPoint), LogicToDrawing(Radius), 
  988. LineStyle, PenColor);
  989. else
  990. pMyArc = new CMyArc(LogicToDrawing(OPoint), LogicToDrawing(EPoint), 
  991.                     LogicToDrawing(SPoint), LogicToDrawing(Radius), 
  992. LineStyle, PenColor);
  993. m_ArcArray.Add(pMyArc);
  994. CGraphObjectInfo* pArcInfo = new CGraphObjectInfo(0, FALSE, FALSE);
  995. m_ArcInfoArray.Add(pArcInfo);
  996. }
  997. CMyArc* CManageGraphObject::GetArc(int index)
  998. {
  999. if(index < 0 || index > m_ArcArray.GetUpperBound())
  1000. return 0;
  1001. return (CMyArc*)m_ArcArray.GetAt(index);
  1002. }
  1003. void CManageGraphObject::DelAllArc()
  1004. {
  1005. int index;
  1006. index = m_ArcArray.GetSize();
  1007. while(index--)
  1008. delete (CMyArc*)m_ArcArray.GetAt(index);
  1009. m_ArcArray.RemoveAll();
  1010. index = m_ArcInfoArray.GetSize();
  1011. while(index--)
  1012. delete (CGraphObjectInfo*)m_ArcInfoArray.GetAt(index);
  1013. m_ArcInfoArray.RemoveAll();
  1014. }