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

绘图程序

开发平台:

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(SIZE size, int style, COLORREF color)
  28. {
  29. LogicViewSize = size;
  30. LineStyle = style;
  31. PenColor = color;
  32. GetObjectRange();
  33. }
  34. void CManageGraphObject::SetLogicViewSize(SIZE size)
  35. {
  36. LogicViewSize.cx = size.cx;
  37. LogicViewSize.cy = size.cy;
  38. }
  39. void CManageGraphObject::SetLineStyle(int style)
  40. {
  41. LineStyle = style;
  42. }
  43. void CManageGraphObject::SetPenColor(COLORREF color)
  44. {
  45. PenColor = color;
  46. }
  47. //获取命令堆栈指针
  48. void CManageGraphObject::GetCommandStack(CCommandStack* pStack)
  49. {
  50. pCommandStack = pStack;
  51. }
  52. void CManageGraphObject::SetCurrentLayerName(CString layername)
  53. {
  54. CurrentLayerName = layername;
  55. }
  56. void CManageGraphObject::GetDC(CDC* pClientDC)
  57. {
  58. ASSERT(pClientDC != NULL);
  59. pDC = pClientDC;
  60. }
  61. void CManageGraphObject::GetView(CView* view)
  62. {
  63. ASSERT(view != NULL);
  64. pView = view;
  65. }
  66. void CManageGraphObject::GetManageFileLayer(CManageFileLayer* pManage)
  67. {
  68. pManageLayer = pManage;
  69. }
  70. void CManageGraphObject::Serialize(CArchive& ar)
  71. {
  72. int index;
  73. CMyLine*      pLine;
  74. CMyRect*      pRect;
  75. CMyCircle*    pCircle;
  76. CMyArc*       pArc;
  77. CGraphObjectInfo* pLineInfo;
  78. CGraphObjectInfo* pRectInfo;
  79. CGraphObjectInfo* pCircleInfo;
  80. CGraphObjectInfo* pArcInfo;
  81. if(ar.IsStoring())
  82. {
  83. m_LineArray.Serialize(ar);
  84. m_RectArray.Serialize(ar);
  85. m_CircleArray.Serialize(ar);
  86. m_ArcArray.Serialize(ar);
  87. }
  88. else
  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. //注意创建CGraphObjectInfo对象
  97. for(index = 0; index < m_LineArray.GetSize(); index++)
  98. {
  99. pLine = GetLine(index);
  100. pLineInfo = new CGraphObjectInfo();
  101. m_LineInfoArray.Add(pLineInfo);
  102. }
  103. for(index = 0; index < m_RectArray.GetSize(); index++)
  104. {
  105. pRect = GetRect(index);
  106. pRectInfo = new CGraphObjectInfo();
  107. m_RectInfoArray.Add(pRectInfo);
  108. }
  109. for(index = 0; index < m_CircleArray.GetSize(); index++)
  110. {
  111. pCircle = GetCircle(index);
  112. pCircleInfo = new CGraphObjectInfo();
  113. m_CircleInfoArray.Add(pCircleInfo);
  114. }
  115. for(index = 0; index < m_ArcArray.GetSize(); index++)
  116. {
  117. pArc = GetArc(index);
  118. pArcInfo = new CGraphObjectInfo();
  119. m_ArcInfoArray.Add(pArcInfo);
  120. }
  121. }
  122. }
  123. ///////////////////////////////////////////////////////////////////////////////////////////////
  124. //处理命令
  125. void CManageGraphObject::OnLButtonDown(UINT ID, CPoint point)
  126. {
  127. ASSERT(pDC != NULL);
  128. ASSERT(pCommandStack != NULL);
  129. UINT msg = MESSAGE_LBUTTONDOWN;
  130. switch(ID)
  131. {
  132. case DRAW_SELECT:
  133. SelectObject(pDC, point);
  134.     break;
  135. case DRAW_LINE:
  136. OnDrawLine(msg, point);
  137. break;
  138.     case DRAW_RECT:
  139. OnDrawRect(msg, point);
  140. break;
  141.     case DRAW_CIRCLE:
  142.             OnDrawCircle(msg, point);
  143. break;
  144.     case DRAW_ARC:
  145. OnDrawArc(msg, point);
  146. break;
  147. case DRAW_TEXT:
  148. break;
  149. case MODIFY_DELETE:
  150. OnModifyDelete(msg, point);
  151. break;
  152. }
  153. }
  154. UINT CManageGraphObject::OnRButtonDown(UINT ID, CPoint point)
  155. {
  156. ASSERT(pDC != NULL);
  157. UINT msg = MESSAGE_RBUTTONDOWN;
  158. switch(ID)
  159. {
  160. case DRAW_SELECT:
  161. if(UnselectObject())
  162. pView->Invalidate();
  163. break;
  164. case DRAW_LINE:
  165. ID = OnDrawLine(msg, point);
  166. break;
  167. case DRAW_RECT:
  168. ID = OnDrawRect(msg, point);
  169. break;
  170. case DRAW_CIRCLE:
  171. ID = OnDrawCircle(msg, point);
  172. break;
  173. case DRAW_ARC:
  174. ID = OnDrawArc(msg, point);
  175. break;
  176. case MODIFY_DELETE:
  177. ID = OnModifyDelete(msg, point);
  178. break;
  179. }
  180. return ID;
  181. }
  182. void CManageGraphObject::OnMouseMove(UINT ID, CPoint point)
  183. {
  184. ASSERT(pDC != NULL);
  185. UINT msg = MESSAGE_MOUSEMOVE;
  186. switch(ID)
  187. {
  188. case DRAW_SELECT:
  189. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  190. break;
  191. case DRAW_LINE:
  192.         OnDrawLine(msg, point); 
  193. break;
  194. case DRAW_RECT:
  195. OnDrawRect(msg, point);
  196. break;
  197. case DRAW_CIRCLE:
  198. OnDrawCircle(msg, point);
  199. break;
  200. case DRAW_ARC:
  201. OnDrawArc(msg, point);
  202. break;
  203. case MODIFY_DELETE:
  204. OnModifyDelete(msg, point);
  205. break;
  206. }
  207. }
  208. void CManageGraphObject::OnScroll(UINT ID)
  209. {
  210. ASSERT(pDC != NULL);
  211.     
  212. UINT msg = MESSAGE_SCROLL;
  213. switch(ID)
  214. {
  215. case DRAW_LINE:
  216. OnDrawLine(msg, NULL);
  217. break;
  218. case DRAW_RECT:
  219. OnDrawRect(msg, NULL);
  220. break;
  221. case DRAW_CIRCLE:
  222. OnDrawCircle(msg, NULL);
  223. break;
  224. case DRAW_ARC:
  225. OnDrawArc(msg, NULL);
  226. break;
  227. }
  228.  
  229. }
  230. UINT CManageGraphObject::OnDrawLine(UINT msg, CPoint point)
  231. {
  232. int ObjectType;
  233. int ObjetcIndex;
  234.     
  235. UINT command = DRAW_LINE;
  236. switch(msg)
  237. {
  238. case MESSAGE_LBUTTONDOWN:
  239. LRepeatTimes++;
  240. switch(LRepeatTimes)
  241. {
  242. case 1:
  243. StartPoint = point;
  244. m_DrawGrapObject.SetOnceFlag();
  245. break;
  246. case 2:
  247. EndPoint = point;
  248.         m_DrawGrapObject.DrawLine(pDC, StartPoint, EndPoint, LineStyle, PenColor);
  249.         AddLine(StartPoint, EndPoint, LineStyle, PenColor);
  250.         ObjectType = OBJECT_LINE;
  251.         ObjetcIndex = m_LineArray.GetUpperBound();
  252.         //因为执行一项新操作后,将不再有可以Redo的命令
  253.         pCommandStack->PushCommand(DRAW_LINE, 1, &ObjectType, &ObjetcIndex);
  254.         
  255.         LRepeatTimes--;
  256.         StartPoint = EndPoint;
  257. oldEndPoint = StartPoint;
  258.         break;
  259. }
  260. break;
  261. case MESSAGE_RBUTTONDOWN:
  262. if(LRepeatTimes == 1)
  263. {
  264. m_DrawGrapObject.CancelDrawLine(pDC, StartPoint, EndPoint);
  265. LRepeatTimes = 0;
  266. }
  267. else 
  268. {
  269. command = DRAW_SELECT;
  270. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  271. LRepeatTimes = 0;
  272. }
  273. break;
  274. case MESSAGE_MOUSEMOVE:
  275. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  276. if(LRepeatTimes == 1)
  277. {
  278. EndPoint = point;
  279. m_DrawGrapObject.DrawLineXOR(pDC, StartPoint, EndPoint, oldEndPoint);
  280. oldEndPoint = EndPoint;
  281. }
  282. break;
  283. case MESSAGE_SCROLL:
  284. if(LRepeatTimes == 1)
  285. {
  286. m_DrawGrapObject.CancelDrawLine(pDC, StartPoint, EndPoint);
  287.             oldEndPoint = StartPoint;
  288.     EndPoint = StartPoint;
  289. m_DrawGrapObject.SetOnceFlag();
  290. }
  291. break;
  292. }
  293. return command;
  294. }
  295. UINT CManageGraphObject::OnDrawRect(UINT msg, CPoint point)
  296. {
  297. int ObjectType;
  298. int ObjetcIndex;
  299. UINT command = DRAW_RECT;
  300. switch(msg)
  301. {
  302. case MESSAGE_LBUTTONDOWN:
  303. LRepeatTimes++;
  304. switch(LRepeatTimes)
  305. {
  306. case 1:
  307. StartPoint = point;
  308. m_DrawGrapObject.SetOnceFlag();
  309. break;
  310. case 2:
  311. EndPoint = point;
  312. m_DrawGrapObject.DrawRect(pDC, StartPoint, EndPoint, LineStyle, PenColor);
  313. AddRect(StartPoint, EndPoint, LineStyle, PenColor);
  314.     ObjectType = OBJECT_RECT;
  315. ObjetcIndex = m_RectArray.GetUpperBound();
  316. pCommandStack->PushCommand(DRAW_RECT, 1, &ObjectType, &ObjetcIndex);
  317.     LRepeatTimes = 0;
  318. break;
  319. }
  320. break;
  321. case MESSAGE_RBUTTONDOWN:
  322. if(LRepeatTimes == 1)
  323. {
  324. m_DrawGrapObject.CancelDrawRect(pDC, StartPoint, EndPoint);
  325. LRepeatTimes = 0;
  326. }
  327. else
  328. {
  329. command = DRAW_SELECT;
  330. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  331. LRepeatTimes = 0;
  332. }
  333. break;
  334. case MESSAGE_MOUSEMOVE:
  335. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  336. if(LRepeatTimes == 1)
  337. {
  338. EndPoint = point;
  339. m_DrawGrapObject.DrawRectXOR(pDC, StartPoint, EndPoint, oldEndPoint);
  340. oldEndPoint = EndPoint;
  341. }
  342. break;
  343. case MESSAGE_SCROLL:
  344. if(LRepeatTimes == 1)
  345. {
  346. m_DrawGrapObject.CancelDrawRect(pDC, StartPoint, EndPoint);
  347. oldEndPoint = StartPoint;
  348.     EndPoint = StartPoint;
  349. m_DrawGrapObject.SetOnceFlag();
  350. }
  351. break;
  352. }
  353. return command;
  354. }
  355. UINT CManageGraphObject::OnDrawCircle(UINT msg, CPoint point)
  356. {
  357. int ObjectType;
  358. int ObjetcIndex;
  359. UINT command = DRAW_CIRCLE;
  360. switch(msg)
  361. {
  362. case MESSAGE_LBUTTONDOWN:
  363. LRepeatTimes++;
  364.     switch(LRepeatTimes)
  365. {
  366. case 1:
  367. StartPoint = point;
  368.     m_DrawGrapObject.SetOnceFlag();
  369. break;
  370. case 2:
  371. EndPoint = point;
  372.     m_DrawGrapObject.DrawCircle(pDC, StartPoint, EndPoint, LineStyle, PenColor);
  373. AddCircle(StartPoint, EndPoint, LineStyle, PenColor);
  374. ObjectType = OBJECT_CIRCLE;
  375. ObjetcIndex = m_CircleArray.GetUpperBound();
  376. pCommandStack->PushCommand(DRAW_CIRCLE, 1, &ObjectType, &ObjetcIndex);
  377. LRepeatTimes = 0;
  378. break;
  379. }
  380. break;
  381. case MESSAGE_RBUTTONDOWN:
  382. if(LRepeatTimes == 1)
  383. {
  384. m_DrawGrapObject.CancelDrawCircle(pDC, StartPoint, EndPoint);
  385. LRepeatTimes = 0;
  386. }
  387. else
  388. {
  389. command = DRAW_SELECT;
  390. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  391. LRepeatTimes = 0;
  392. }
  393. break;
  394. case MESSAGE_MOUSEMOVE:
  395. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  396. if(LRepeatTimes == 1)
  397. {
  398. EndPoint = point;
  399. m_DrawGrapObject.DrawCircleXOR(pDC, StartPoint, EndPoint, oldEndPoint);
  400.             oldEndPoint = EndPoint;
  401. }
  402. break;
  403. case MESSAGE_SCROLL:
  404. if(LRepeatTimes == 1)
  405. {
  406. m_DrawGrapObject.CancelDrawCircle(pDC, StartPoint, EndPoint);
  407. oldEndPoint = StartPoint;
  408. EndPoint = StartPoint;
  409. m_DrawGrapObject.SetOnceFlag();
  410. }
  411. break;
  412. }
  413. return command;
  414. }
  415. UINT CManageGraphObject::OnDrawArc(UINT msg, CPoint point)
  416. {
  417. int ObjectType;
  418. int ObjetcIndex;
  419. UINT command = DRAW_ARC;
  420. switch(msg)
  421. {
  422. case MESSAGE_LBUTTONDOWN:
  423. LRepeatTimes++;
  424. switch(LRepeatTimes)
  425. {
  426. case 1:
  427. StartPoint = point;
  428. m_DrawGrapObject.SetOnceFlag();
  429.     break;
  430. case 2:
  431. MidPoint = point;
  432. m_DrawGrapObject.SetOnceFlag();
  433. break;
  434. case 3:
  435. EndPoint = point;
  436. m_DrawGrapObject.DrawArc(pDC, StartPoint, MidPoint, EndPoint, 
  437.                      LineStyle, PenColor);
  438. AddArc(StartPoint, MidPoint, EndPoint, LineStyle, PenColor); 
  439.      ObjectType = OBJECT_ARC;
  440. ObjetcIndex = m_ArcArray.GetUpperBound();
  441. pCommandStack->PushCommand(DRAW_ARC, 1, &ObjectType, &ObjetcIndex);
  442. LRepeatTimes = 0;
  443. break;
  444. }
  445. break;
  446. case MESSAGE_RBUTTONDOWN:
  447. if(LRepeatTimes > 0)
  448. {
  449. m_DrawGrapObject.CancelDrawArc(pDC, StartPoint, MidPoint, EndPoint, LRepeatTimes);
  450. LRepeatTimes = 0;
  451. }
  452. else
  453. {
  454. command = DRAW_SELECT;
  455. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR));
  456. LRepeatTimes = 0;
  457. }
  458. break;
  459. case MESSAGE_MOUSEMOVE:
  460. SetCursor(AfxGetApp()->LoadCursor(IDC_DRAWCUR));
  461. switch(LRepeatTimes)
  462. {
  463. case 1:
  464. MidPoint = point;
  465. m_DrawGrapObject.DrawArcXOR(pDC, StartPoint, MidPoint, oldMidPoint,
  466.                         EndPoint, oldEndPoint, LRepeatTimes);
  467. oldMidPoint = MidPoint;
  468. break;
  469. case 2:
  470. EndPoint = point;
  471. m_DrawGrapObject.DrawArcXOR(pDC, StartPoint, MidPoint, oldMidPoint,
  472.                         EndPoint, oldEndPoint, LRepeatTimes);
  473. oldEndPoint = EndPoint;
  474. break;
  475. }
  476. break;
  477. case MESSAGE_SCROLL:
  478. m_DrawGrapObject.CancelDrawArc(pDC, StartPoint, MidPoint, EndPoint, LRepeatTimes);
  479. if(LRepeatTimes == 1)
  480. {
  481. oldMidPoint = StartPoint;
  482. MidPoint = StartPoint;
  483. }
  484. if(LRepeatTimes == 2)
  485. {
  486. oldEndPoint = StartPoint;
  487. EndPoint = StartPoint;
  488. }
  489.         m_DrawGrapObject.SetOnceFlag();
  490. break;
  491. }
  492. return command;
  493. }
  494. UINT CManageGraphObject::OnModifyDelete(UINT msg, CPoint point)
  495. {
  496. int* pObjectType;
  497. int* pObjetcIndex;
  498. int nIndex;
  499. int num;
  500. CGraphObjectInfo* pLineInfo;
  501. CGraphObjectInfo* pRectInfo;
  502. CGraphObjectInfo* pCircleInfo;
  503. CGraphObjectInfo* pArcInfo;
  504. CSelectedObjectInfo* pSelObjectInfo;
  505. switch(msg)
  506. {
  507. case MESSAGE_LBUTTONDOWN:
  508. SelectObject(pDC, point);
  509. break;
  510.     case MESSAGE_MOUSEMOVE:
  511. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR1));
  512. break;
  513. case NULL:
  514. case MESSAGE_RBUTTONDOWN:
  515. num = m_SelObjInfoArray.GetSize();
  516. //如果没有图形对象被选中则返回
  517. if(num == 0)
  518. {
  519. SetCursor(AfxGetApp()->LoadCursor(IDC_SELECTCUR1));
  520. return MODIFY_DELETE;
  521. }
  522. pObjectType = new int[num];
  523. pObjetcIndex = new int[num];
  524. for(nIndex = 0; nIndex < num; nIndex++)
  525. {
  526. pSelObjectInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  527. switch(pSelObjectInfo->m_type)
  528. {
  529. case OBJECT_LINE:
  530. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pSelObjectInfo->m_index);
  531. pLineInfo->m_del = TRUE;
  532. pObjectType[nIndex] = OBJECT_LINE;
  533. pObjetcIndex[nIndex] = pSelObjectInfo->m_index;
  534. break;
  535. case OBJECT_RECT:
  536. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pSelObjectInfo->m_index);
  537. pRectInfo->m_del = TRUE;
  538. pObjectType[nIndex] = OBJECT_RECT;
  539. pObjetcIndex[nIndex] = pSelObjectInfo->m_index;
  540. break;
  541. case OBJECT_CIRCLE:
  542. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pSelObjectInfo->m_index);
  543. pCircleInfo->m_del = TRUE;
  544. pObjectType[nIndex] = OBJECT_CIRCLE;
  545. pObjetcIndex[nIndex] = pSelObjectInfo->m_index;
  546. break;
  547. case OBJECT_ARC:
  548. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pSelObjectInfo->m_index);
  549. pArcInfo->m_del = TRUE;
  550. pObjectType[nIndex] = OBJECT_ARC;
  551. pObjetcIndex[nIndex] = pSelObjectInfo->m_index;
  552. break;
  553. }
  554. }
  555. pCommandStack->PushCommand(MODIFY_DELETE, num, pObjectType, pObjetcIndex);
  556. UnselectObject();
  557. pView->Invalidate();
  558. delete pObjectType;
  559. delete pObjetcIndex;
  560. return DRAW_SELECT;
  561. }
  562.     return MODIFY_DELETE;
  563. }
  564. //实现“撤销”命令
  565. void CManageGraphObject::OnUndo()
  566. {
  567. CCommandStruct* pCommand;
  568. CGraphObjectInfo* pLineInfo;
  569. CGraphObjectInfo* pRectInfo;
  570. CGraphObjectInfo* pCircleInfo;
  571. CGraphObjectInfo* pArcInfo;
  572. pCommand = (CCommandStruct*)pCommandStack->Undo();
  573.     if(pCommand == NULL)
  574. return;
  575.     switch(pCommand->m_command)
  576. {
  577. case DRAW_LINE:
  578. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[0]);
  579.         pLineInfo->m_del = TRUE;
  580. break;
  581. case DRAW_RECT:
  582. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[0]);
  583.         pRectInfo->m_del = TRUE;
  584. break;
  585. case DRAW_CIRCLE:
  586. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[0]);
  587.         pCircleInfo->m_del = TRUE;
  588. break;
  589. case DRAW_ARC:
  590. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[0]);
  591.         pArcInfo->m_del = TRUE;
  592. break;
  593. case MODIFY_DELETE:
  594. for(int index =0; index < pCommand->m_num; index++)
  595. {
  596. switch(pCommand->m_object[index])
  597. {
  598. case OBJECT_LINE:
  599. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[index]);
  600. pLineInfo->m_del = FALSE;
  601. break;
  602. case OBJECT_RECT:
  603. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[index]);
  604. pRectInfo->m_del = FALSE;
  605. break;
  606. case OBJECT_CIRCLE:
  607. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[index]);
  608. pCircleInfo->m_del = FALSE;
  609. break;
  610. case OBJECT_ARC:
  611. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[index]);
  612. pArcInfo->m_del = FALSE;
  613. break;
  614. }
  615. }
  616. break;
  617. }
  618. }
  619. //实现“重做”命令
  620. void CManageGraphObject::OnRedo()
  621. {
  622. CCommandStruct* pCommand;
  623. CGraphObjectInfo* pLineInfo;
  624. CGraphObjectInfo* pRectInfo;
  625. CGraphObjectInfo* pCircleInfo;
  626. CGraphObjectInfo* pArcInfo;
  627. pCommand = (CCommandStruct*)pCommandStack->Redo();
  628. if(pCommand == NULL)
  629. return;
  630.     switch(pCommand->m_command)
  631. {
  632. case DRAW_LINE:
  633. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[0]);
  634.         pLineInfo->m_del = FALSE;
  635. break;
  636. case DRAW_RECT:
  637. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[0]);
  638.         pRectInfo->m_del = FALSE;
  639. break;
  640. case DRAW_CIRCLE:
  641. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[0]);
  642.         pCircleInfo->m_del = FALSE;
  643. break;
  644. case DRAW_ARC:
  645. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[0]);
  646.         pArcInfo->m_del = FALSE;
  647. break;
  648. case MODIFY_DELETE:
  649. for(int index =0; index < pCommand->m_num; index++)
  650. {
  651. switch(pCommand->m_object[index])
  652. {
  653. case OBJECT_LINE:
  654. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pCommand->m_index[index]);
  655. pLineInfo->m_del = TRUE;
  656. break;
  657. case OBJECT_RECT:
  658. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pCommand->m_index[index]);
  659. pRectInfo->m_del = TRUE;
  660. break;
  661. case OBJECT_CIRCLE:
  662. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pCommand->m_index[index]);
  663. pCircleInfo->m_del = TRUE;
  664. break;
  665. case OBJECT_ARC:
  666. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pCommand->m_index[index]);
  667. pArcInfo->m_del = TRUE;
  668. break;
  669. }
  670. }
  671. break;
  672. }
  673. }
  674. //////////////////////////////////////////////////////////////////////////////////////////////
  675. POINT CManageGraphObject::DrawingToLogic(FPOINT fpoint)
  676. {
  677. POINT point;
  678. point.x = long(fpoint.x * Proportion) + EXTRA_WIDTH;
  679. point.y = LogicViewSize.cy - (long)(fpoint.y * Proportion) - EXTRA_HIGTH;
  680. return point;
  681. }
  682. long CManageGraphObject::DrawingToLogic(float fdistance)
  683. {
  684. long distance;
  685. distance = (long)(fdistance * Proportion);
  686. return distance;
  687. }
  688. FPOINT CManageGraphObject::LogicToDrawing(POINT point)
  689. {
  690. FPOINT fpoint;
  691. fpoint.x = (float)(point.x - EXTRA_WIDTH) / Proportion;
  692. fpoint.y = (float)(LogicViewSize.cy - point.y  - EXTRA_HIGTH) / Proportion;
  693. return fpoint;
  694. }
  695. float CManageGraphObject::LogicToDrawing(long distance)
  696. {
  697. float fdistance;
  698. fdistance = (float)distance / Proportion;
  699. return fdistance;
  700. }
  701. void CManageGraphObject::GetObjectRange()
  702. {
  703. int index;
  704. // int n;
  705. CMyLine*      pLine;
  706. CMyRect*      pRect;
  707. CMyCircle*    pCircle;
  708. CMyArc*       pArc;
  709. CGraphObjectInfo* pLineInfo;
  710. CGraphObjectInfo* pRectInfo;
  711. CGraphObjectInfo* pCircleInfo;
  712. CGraphObjectInfo* pArcInfo;
  713. // n = m_LineArray.GetSize();
  714. // n = m_LineInfoArray.GetSize();
  715. for(index = 0; index < m_LineInfoArray.GetSize(); index++)
  716. {
  717. pLine = GetLine(index);
  718. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(index);
  719. pLineInfo->Initialize(GetLineRange(2, pLine), FALSE, FALSE);
  720. }
  721. for(index = 0; index < m_RectInfoArray.GetSize(); index++)
  722. {
  723. pRect = GetRect(index);
  724. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(index);
  725. pRectInfo->Initialize(GetRectRange(2, pRect), FALSE, FALSE);
  726. }
  727. for(index = 0; index < m_CircleInfoArray.GetSize(); index++)
  728. {
  729. pCircle = GetCircle(index);
  730. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(index);
  731. pCircleInfo->Initialize(GetCircleRange(2, pCircle), FALSE, FALSE);
  732. }
  733. for(index = 0; index < m_ArcInfoArray.GetSize(); index++)
  734. {
  735. pArc = GetArc(index);
  736. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(index);
  737. pArcInfo->Initialize(GetArcRange(2, pArc), FALSE, FALSE);
  738. }
  739. }
  740. void CManageGraphObject::SetEffectRect(CRect rect)
  741. {
  742. EffectRect = rect;
  743. }
  744. CRect CManageGraphObject::GetLogicClientRect()
  745. {
  746. CRect rect;
  747. ASSERT(pView != NULL);
  748. ASSERT(pDC != NULL);
  749. pView->GetClientRect(&rect);
  750. pDC->DPtoLP(&rect);
  751. return rect;
  752. }
  753. BOOL CManageGraphObject::IsInLogicClientRect(CRect rect)
  754. {
  755. CRect clientrect = GetLogicClientRect();
  756. CRect temp = clientrect & rect;
  757. if(temp.IsRectEmpty())
  758. return FALSE;
  759. else 
  760. return TRUE;
  761. }
  762. CRect CManageGraphObject::GetRedrawRect(CRect effectRect, CRect clientRect)
  763. {
  764. CRect rect;
  765.     rect = effectRect & clientRect;
  766. return rect;
  767. }
  768. BOOL CManageGraphObject::IsRedraw(CRect redrawRect, CRect objRect)
  769. {
  770. CRect rect;
  771.     
  772. rect = redrawRect & objRect;
  773. if(rect.IsRectEmpty())
  774. return FALSE;
  775. else 
  776. return TRUE;
  777. }
  778. ////////////////////////////////////////////////////////////////////////////////////////////////
  779. void CManageGraphObject::RedrawAllObject(CDC* pDC)
  780. {
  781. LogicClientRect = GetLogicClientRect();
  782. RedrawLine(pDC);
  783. RedrawRect(pDC);
  784. RedrawCircle(pDC);
  785. RedrawArc(pDC);
  786. }
  787. void CManageGraphObject::SelectObject(CDC* pDC, CPoint point)
  788. {
  789. SelectLine(pDC, point);   
  790. SelectRect(pDC, point);
  791. SelectCircle(pDC, point);
  792. SelectArc(pDC, point);
  793. }
  794. BOOL CManageGraphObject::UnselectObject()
  795. {
  796. BOOL flag = FALSE;
  797. int nIndex;
  798. if(UnselectLine())
  799. flag = TRUE;
  800. if(UnselectRect())
  801. flag = TRUE;
  802. if(UnselectCircle())
  803. flag = TRUE;
  804.     if(UnselectArc())
  805. flag = TRUE;
  806.     
  807. nIndex = m_SelObjInfoArray.GetSize();
  808. while(nIndex--)
  809. delete (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  810.     m_SelObjInfoArray.RemoveAll();
  811. return flag;
  812. }
  813. //////////////////////////////////////////////////////////////////////////////////////////////
  814. CRect CManageGraphObject::GetLineRange(long ExtraRange, CMyLine* pLine)
  815. {
  816. CRect rect;
  817. POINT start;
  818. POINT end;
  819. start = DrawingToLogic(pLine->m_start);
  820. end = DrawingToLogic(pLine->m_end);
  821. rect.left = start.x;
  822. rect.top = start.y;
  823. rect.right = end.x;
  824. rect.bottom = end.y;
  825. //注意必须对矩形规格化
  826. rect.NormalizeRect();
  827. rect.left -= ExtraRange;
  828. rect.top -= ExtraRange;
  829. rect.right += ExtraRange;
  830. rect.bottom += ExtraRange;
  831. return rect;
  832. }
  833. void CManageGraphObject::RedrawLine(CDC* pDC)
  834. {
  835. int index;
  836. CRect rect;
  837. CMyLine* pLine;
  838. CGraphObjectInfo* pLineInfo;
  839. index = m_LineArray.GetSize();
  840. while(index--)
  841. {
  842. pLine = (CMyLine*)m_LineArray.GetAt(index);
  843. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(index);
  844. //判断图形对象所在层是否被隐藏
  845. CFileLayer* pLayer = pManageLayer->GetLayer(pLine->m_layer);
  846. if(!pLayer->IsShow())
  847. continue;
  848. //判断直线对象是否需要重画
  849. if(!IsRedraw(GetRedrawRect(EffectRect, LogicClientRect), pLineInfo->m_rect))
  850. continue;
  851. if(pLineInfo->m_del == TRUE)
  852. continue;
  853. if(pLineInfo->m_selected == TRUE)
  854. {
  855. m_DrawGrapObject.DrawSelLine(pDC, DrawingToLogic(pLine->m_start), DrawingToLogic(pLine->m_end),
  856.                          pLine->m_style, pLine->m_color);
  857. continue;
  858. }
  859.         
  860. m_DrawGrapObject.DrawLine(pDC, DrawingToLogic(pLine->m_start), DrawingToLogic(pLine->m_end),
  861.                    pLine->m_style, pLine->m_color);
  862.      }
  863. }
  864. void CManageGraphObject::SelectLine(CDC* pDC, CPoint point)
  865. {
  866. int nIndex;
  867. long distance;
  868. CRect rect;
  869. CGraphObjectInfo* pLineInfo;
  870. CSelectedObjectInfo* pSelObjInfo;
  871. CMyLine* pLine; 
  872. for(nIndex = 0; nIndex < m_LineInfoArray.GetSize(); nIndex++)
  873. {
  874. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(nIndex);
  875. pLine = (CMyLine*)m_LineArray.GetAt(nIndex);
  876. //判断图形对象所在层是否被隐藏
  877. CFileLayer* pLayer = pManageLayer->GetLayer(pLine->m_layer);
  878. if(!pLayer->IsShow())
  879. continue;
  880. if(pLineInfo ->m_del == TRUE || pLineInfo ->m_selected ==TRUE)
  881. continue;
  882.         
  883. //判断选取点是否在图形对象所占据的选取逻辑区域内
  884. CRect region = pLineInfo->m_rect;
  885. region.InflateRect(SELECT_RANGE, SELECT_RANGE);
  886.         if(!region.PtInRect(point))
  887. continue;
  888. rect.left = DrawingToLogic(pLine->m_start).x;
  889. rect.top = DrawingToLogic(pLine->m_start).y;
  890. rect.right = DrawingToLogic(pLine->m_end).x;
  891. rect.bottom = DrawingToLogic(pLine->m_end).y;
  892. if( (point.x >= min(rect.left, rect.right) - SELECT_RANGE) && (point.x <= max(rect.left, rect.right) + SELECT_RANGE) &&
  893.     (point.y >= min(rect.top, rect.bottom) - SELECT_RANGE) && (point.y <= max(rect.top, rect.bottom) + SELECT_RANGE) )
  894. {
  895. 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) )
  896.        / sqrt( (rect.right - rect.left) * (rect.right - rect.left) + (rect.bottom - rect.top) * (rect.bottom - rect.top) ) );
  897. if(distance <= SELECT_RANGE)
  898. {
  899.            m_DrawGrapObject.DrawSelLine(pDC, DrawingToLogic(pLine->m_start), DrawingToLogic(pLine->m_end),
  900.                          pLine->m_style, pLine->m_color);
  901. pLineInfo->m_selected = TRUE;
  902. pSelObjInfo = new CSelectedObjectInfo(OBJECT_LINE, nIndex);
  903. m_SelObjInfoArray.Add(pSelObjInfo);
  904. }
  905. }
  906. }
  907. }
  908. BOOL CManageGraphObject::UnselectLine()
  909. {
  910. BOOL flag = FALSE;
  911. int nIndex;
  912. CSelectedObjectInfo* pSelObjInfo;
  913. CGraphObjectInfo* pLineInfo;
  914. for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
  915. {
  916. pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  917. if(pSelObjInfo->m_type ==OBJECT_LINE)
  918. {
  919. pLineInfo = (CGraphObjectInfo*)m_LineInfoArray.GetAt(pSelObjInfo->m_index);
  920. pLineInfo->m_selected = FALSE;
  921. flag = TRUE;
  922.         }
  923.     }
  924. return flag;
  925. }
  926. void CManageGraphObject::AddLine(POINT SPoint, POINT EPoint, int Style, COLORREF Color)
  927. {
  928. FPOINT start;
  929. FPOINT end;
  930. start = LogicToDrawing(SPoint);
  931. end = LogicToDrawing(EPoint);
  932. CMyLine* pLine = new CMyLine(CurrentLayerName, start, end, Style, Color);
  933. m_LineArray.Add(pLine);
  934. CGraphObjectInfo* pLineInfo = new CGraphObjectInfo(GetLineRange(0, pLine), FALSE, FALSE);
  935. m_LineInfoArray.Add(pLineInfo);
  936. }
  937. CMyLine* CManageGraphObject::GetLine(int index)
  938. {
  939. ASSERT(index >= 0 && index <= m_LineArray.GetUpperBound());
  940. return (CMyLine*)m_LineArray.GetAt(index);
  941. }
  942. void CManageGraphObject::DelLine(int index)
  943. {
  944. ASSERT(index >= 0 && index <= m_LineArray.GetUpperBound());
  945. delete (CMyLine*)m_LineArray.GetAt(index);
  946. m_LineArray.RemoveAt(index);
  947. delete (CGraphObjectInfo*)m_LineInfoArray.GetAt(index);
  948. m_LineInfoArray.RemoveAt(index);
  949. }
  950. void CManageGraphObject::DelAllLine()
  951. {
  952. int index;
  953. index = m_LineArray.GetSize();
  954. while(index--)
  955. delete (CMyLine*)m_LineArray.GetAt(index);
  956. m_LineArray.RemoveAll();
  957.      
  958. index = m_LineInfoArray.GetSize();
  959. while(index--)
  960. delete (CGraphObjectInfo*)m_LineInfoArray.GetAt(index);
  961. m_LineInfoArray.RemoveAll();
  962. }
  963. //////////////////////////////////////////////////////////////////////////////////////////////
  964. CRect CManageGraphObject::GetRectRange(long ExtraRange, CMyRect* pRect)
  965. {
  966. CRect rect;
  967. POINT start;
  968. POINT end;
  969. start = DrawingToLogic(pRect->m_start);
  970. end = DrawingToLogic(pRect->m_end);
  971. rect.left = start.x;
  972. rect.top = start.y;
  973. rect.right = end.x;
  974. rect.bottom = end.y;
  975. //注意必须对矩形规格化
  976. rect.NormalizeRect();
  977. rect.left -= ExtraRange;
  978. rect.top -= ExtraRange;
  979. rect.right += ExtraRange;
  980. rect.bottom += ExtraRange;
  981. return rect;
  982. }
  983. void CManageGraphObject::RedrawRect(CDC* pDC)
  984. {
  985. int index;
  986. CMyRect* pRect;
  987. CGraphObjectInfo* pRectInfo;
  988. index = m_RectArray.GetSize();
  989. //设置不填充状态
  990. pDC->SelectStockObject(NULL_BRUSH);
  991. while(index--)
  992. {
  993. pRect = (CMyRect*)m_RectArray.GetAt(index);
  994. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(index);
  995. //判断图形对象所在层是否被隐藏
  996. CFileLayer* pLayer = pManageLayer->GetLayer(pRect->m_layer);
  997. if(!pLayer->IsShow())
  998. continue;
  999. //判断矩形对象是否需要重画
  1000. if(!IsRedraw(GetRedrawRect(EffectRect, LogicClientRect), pRectInfo->m_rect))
  1001. continue;
  1002. if(pRectInfo->m_del == TRUE)
  1003. continue;
  1004. if(pRectInfo->m_selected == TRUE)
  1005. {
  1006. m_DrawGrapObject.DrawSelRect(pDC, DrawingToLogic(pRect->m_start), DrawingToLogic(pRect->m_end),
  1007.                          pRect->m_style, pRect->m_color);
  1008. continue;
  1009. }
  1010. m_DrawGrapObject.DrawRect(pDC, DrawingToLogic(pRect->m_start), DrawingToLogic(pRect->m_end),
  1011.                    pRect->m_style, pRect->m_color);
  1012.       
  1013. }
  1014. }
  1015. void CManageGraphObject::SelectRect(CDC* pDC, CPoint point)
  1016. {
  1017. int nIndex;
  1018. CRect rect;
  1019. CGraphObjectInfo* pRectInfo;
  1020. CSelectedObjectInfo* pSelObjInfo;
  1021. CMyRect* pRect; 
  1022. for(nIndex = 0; nIndex < m_RectInfoArray.GetSize(); nIndex++)
  1023. {
  1024. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(nIndex);
  1025. pRect = (CMyRect*)m_RectArray.GetAt(nIndex);
  1026. //判断图形对象所在层是否被隐藏
  1027. CFileLayer* pLayer = pManageLayer->GetLayer(pRect->m_layer);
  1028. if(!pLayer->IsShow())
  1029. continue;
  1030. if(pRectInfo ->m_del == TRUE || pRectInfo ->m_selected ==TRUE)
  1031. continue;
  1032. //判断选取点是否在图形对象所占据的选取逻辑区域内
  1033. CRect region = pRectInfo->m_rect;
  1034. region.InflateRect(SELECT_RANGE, SELECT_RANGE);
  1035.         if(!region.PtInRect(point))
  1036. continue;
  1037. rect.left = DrawingToLogic(pRect->m_start).x;
  1038. rect.top = DrawingToLogic(pRect->m_start).y;
  1039. rect.right = DrawingToLogic(pRect->m_end).x;
  1040. rect.bottom = DrawingToLogic(pRect->m_end).y;
  1041. if( ( (point.x >= min(rect.left, rect.right) - SELECT_RANGE) && (point.x <= max(rect.left, rect.right) + SELECT_RANGE) &&
  1042.     (point.y >= min(rect.top, rect.bottom) - SELECT_RANGE) && (point.y <= max(rect.top, rect.bottom) + SELECT_RANGE) ) &&
  1043. ( (point.x <= min(rect.left, rect.right) + SELECT_RANGE) || (point.x >= max(rect.left, rect.right) - SELECT_RANGE) ||
  1044.             (point.y <= min(rect.top, rect.bottom) + SELECT_RANGE) || (point.y >= max(rect.top, rect.bottom) - SELECT_RANGE) ) ) 
  1045. {
  1046. m_DrawGrapObject.DrawSelRect(pDC, rect.TopLeft(), rect.BottomRight(), 
  1047.                          pRect->m_style, pRect->m_color);
  1048. pRectInfo->m_selected = TRUE;
  1049. pSelObjInfo = new CSelectedObjectInfo(OBJECT_RECT, nIndex);
  1050. m_SelObjInfoArray.Add(pSelObjInfo);
  1051. }
  1052. }
  1053. }
  1054. BOOL CManageGraphObject::UnselectRect()
  1055. {
  1056. BOOL flag = FALSE;
  1057. int nIndex;
  1058. CSelectedObjectInfo* pSelObjInfo;
  1059. CGraphObjectInfo* pRectInfo;
  1060. for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
  1061. {
  1062. pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  1063. if(pSelObjInfo->m_type ==OBJECT_RECT)
  1064. {
  1065. pRectInfo = (CGraphObjectInfo*)m_RectInfoArray.GetAt(pSelObjInfo->m_index);
  1066. pRectInfo->m_selected = FALSE;
  1067. flag = TRUE;
  1068.         }
  1069.     }
  1070. return flag;
  1071. }
  1072. void CManageGraphObject::AddRect(POINT SPoint, POINT EPoint, int Style, COLORREF Color)
  1073. {
  1074. FPOINT start;
  1075. FPOINT end;
  1076. start = LogicToDrawing(SPoint);
  1077. end = LogicToDrawing(EPoint);
  1078.     
  1079. CMyRect* pRect = new CMyRect(CurrentLayerName, start, end, Style, Color);
  1080. m_RectArray.Add(pRect);
  1081. CGraphObjectInfo * pRectInfo = new CGraphObjectInfo(GetRectRange(0, pRect), FALSE, FALSE);
  1082. m_RectInfoArray.Add(pRectInfo);
  1083. }
  1084. CMyRect* CManageGraphObject::GetRect(int index)
  1085. {
  1086. ASSERT(index >= 0 && index <= m_RectArray.GetUpperBound());
  1087. return (CMyRect*)m_RectArray.GetAt(index);
  1088. }
  1089. void CManageGraphObject::DelRect(int index)
  1090. {
  1091. ASSERT(index >= 0 && index <= m_RectArray.GetUpperBound());
  1092. delete (CMyRect*)m_RectArray.GetAt(index);
  1093. m_RectArray.RemoveAt(index);
  1094. delete (CGraphObjectInfo*)m_RectInfoArray.GetAt(index);
  1095. m_RectInfoArray.RemoveAt(index);
  1096. }
  1097. void CManageGraphObject::DelAllRect()
  1098. {
  1099. int index;
  1100. index = m_RectArray.GetSize();
  1101. while(index--)
  1102. delete (CMyRect*)m_RectArray.GetAt(index);
  1103. m_RectArray.RemoveAll();
  1104. index = m_RectInfoArray.GetSize();
  1105. while(index--)
  1106. delete (CGraphObjectInfo*)m_RectInfoArray.GetAt(index);
  1107. m_RectInfoArray.RemoveAll();
  1108. }
  1109. //////////////////////////////////////////////////////////////////////////////////////////////
  1110. CRect CManageGraphObject::GetCircleRange(long ExtraRange, CMyCircle* pCircle)
  1111. {
  1112. CRect rect;
  1113. POINT origin;
  1114. long Radius;
  1115. origin = DrawingToLogic(pCircle->m_origin);
  1116. Radius = DrawingToLogic(pCircle->m_radius);
  1117. rect.left = origin.x - Radius - ExtraRange;
  1118. rect.top = origin.y - Radius - ExtraRange;
  1119. rect.right = origin.x + Radius + ExtraRange;
  1120. rect.bottom = origin.y + Radius + ExtraRange;
  1121. rect.NormalizeRect();
  1122. return rect;
  1123. }
  1124. void CManageGraphObject::RedrawCircle(CDC* pDC)
  1125. {
  1126. int index;
  1127. CMyCircle* pCircle;
  1128. CGraphObjectInfo* pCircleInfo;
  1129. index = m_CircleArray.GetSize();
  1130. //设置不填充状态
  1131. pDC->SelectStockObject(NULL_BRUSH);
  1132. while(index--)
  1133. {
  1134. pCircle = (CMyCircle*)m_CircleArray.GetAt(index);
  1135. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(index);
  1136. //判断图形对象所在层是否被隐藏
  1137. CFileLayer* pLayer = pManageLayer->GetLayer(pCircle->m_layer);
  1138. if(!pLayer->IsShow())
  1139. continue;
  1140. //判断圆对象是否需要重画
  1141. if(!IsRedraw(GetRedrawRect(EffectRect, LogicClientRect), pCircleInfo->m_rect))
  1142. continue;
  1143. if(pCircleInfo->m_del == TRUE)
  1144. continue;
  1145. if(pCircleInfo->m_selected == TRUE)
  1146. {
  1147. m_DrawGrapObject.DrawSelCircle(pDC, DrawingToLogic(pCircle->m_origin), DrawingToLogic(pCircle->m_radius), 
  1148.                            pCircle->m_style, pCircle->m_color);
  1149. continue;
  1150. }
  1151. m_DrawGrapObject.DrawCircle(pDC, DrawingToLogic(pCircle->m_origin), DrawingToLogic(pCircle->m_radius),
  1152.                          pCircle->m_style, pCircle->m_color);
  1153.    }
  1154. }
  1155. void CManageGraphObject::SelectCircle(CDC* pDC, CPoint point)
  1156. {
  1157. int nIndex;
  1158. POINT origin;
  1159. long r;
  1160. long distance;
  1161. CGraphObjectInfo* pCircleInfo;
  1162. CSelectedObjectInfo* pSelObjInfo;
  1163. CMyCircle* pCircle; 
  1164. for(nIndex = 0; nIndex < m_CircleInfoArray.GetSize(); nIndex++)
  1165. {
  1166. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(nIndex);
  1167. pCircle = (CMyCircle*)m_CircleArray.GetAt(nIndex);
  1168. //判断图形对象所在层是否被隐藏
  1169. CFileLayer* pLayer = pManageLayer->GetLayer(pCircle->m_layer);
  1170. if(!pLayer->IsShow())
  1171. continue;
  1172. if(pCircleInfo ->m_del == TRUE || pCircleInfo ->m_selected ==TRUE)
  1173. continue;
  1174. //判断选取点是否在图形对象所占据的选取逻辑区域内
  1175. CRect region = pCircleInfo->m_rect;
  1176. region.InflateRect(SELECT_RANGE, SELECT_RANGE);
  1177.         if(!region.PtInRect(point))
  1178. continue;
  1179. origin = DrawingToLogic(pCircle->m_origin);
  1180. r = DrawingToLogic(pCircle->m_radius);
  1181. distance = (long)sqrt( (origin.x - point.x) * (origin.x - point.x) + (origin.y - point.y) * (origin.y - point.y) );
  1182. if(distance <= r + SELECT_RANGE && distance >= r - SELECT_RANGE)
  1183. {
  1184. m_DrawGrapObject.DrawSelCircle(pDC, DrawingToLogic(pCircle->m_origin), DrawingToLogic(pCircle->m_radius),
  1185.                            pCircle->m_style, pCircle->m_color);
  1186. pCircleInfo->m_selected = TRUE;
  1187. pSelObjInfo = new CSelectedObjectInfo(OBJECT_CIRCLE, nIndex);
  1188. m_SelObjInfoArray.Add(pSelObjInfo);
  1189. }
  1190. }
  1191. }
  1192. BOOL CManageGraphObject::UnselectCircle()
  1193. {
  1194. BOOL flag = FALSE;
  1195. int nIndex;
  1196. CSelectedObjectInfo* pSelObjInfo;
  1197. CGraphObjectInfo* pCircleInfo;
  1198. for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
  1199. {
  1200. pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  1201. if(pSelObjInfo->m_type ==OBJECT_CIRCLE)
  1202. {
  1203. pCircleInfo = (CGraphObjectInfo*)m_CircleInfoArray.GetAt(pSelObjInfo->m_index);
  1204. pCircleInfo->m_selected = FALSE;
  1205. flag = TRUE;
  1206.         }
  1207.     }
  1208. return flag;
  1209. }
  1210. void CManageGraphObject::AddCircle(POINT SPoint, POINT EPoint, int Style, COLORREF Color)
  1211. {
  1212. FPOINT start;
  1213. FPOINT end;
  1214. float r;
  1215. start = LogicToDrawing(SPoint);
  1216. end = LogicToDrawing(EPoint);
  1217. r = (float)sqrt(pow(start.x - end.x, 2) + pow(start.y - end.y, 2));
  1218. CMyCircle* pCircle = new CMyCircle(CurrentLayerName, start, r, Style, Color);
  1219. m_CircleArray.Add(pCircle);
  1220. CGraphObjectInfo* pCircleInfo = new CGraphObjectInfo(GetCircleRange(0, pCircle), FALSE, FALSE);
  1221. m_CircleInfoArray.Add(pCircleInfo);
  1222. }
  1223. CMyCircle* CManageGraphObject::GetCircle(int index)
  1224. {
  1225. ASSERT(index >= 0 && index <= m_CircleArray.GetUpperBound());
  1226. return (CMyCircle*)m_CircleArray.GetAt(index);
  1227. }
  1228. void CManageGraphObject::DelCircle(int index)
  1229. {
  1230. ASSERT(index >= 0 && index <= m_CircleArray.GetUpperBound());
  1231. delete (CMyCircle*)m_CircleArray.GetAt(index);
  1232. m_CircleArray.RemoveAt(index);
  1233. delete (CGraphObjectInfo*)m_CircleInfoArray.GetAt(index);
  1234. m_CircleInfoArray.RemoveAt(index);
  1235. }
  1236. void CManageGraphObject::DelAllCircle()
  1237. {
  1238. int index;
  1239. index = m_CircleArray.GetSize();
  1240. while(index--)
  1241. delete (CMyCircle*)m_CircleArray.GetAt(index);
  1242. m_CircleArray.RemoveAll();
  1243. index = m_CircleInfoArray.GetSize();
  1244. while(index--)
  1245. delete (CGraphObjectInfo*)m_CircleInfoArray.GetAt(index);
  1246. m_CircleInfoArray.RemoveAll();
  1247. }
  1248. //////////////////////////////////////////////////////////////////////////////////////////////
  1249. CRect CManageGraphObject::GetArcRange(long ExtraRange, CMyArc* pArc)
  1250. {
  1251. CRect rect;
  1252. POINT origin;
  1253. long Radius;
  1254. origin = DrawingToLogic(pArc->m_origin);
  1255. Radius = DrawingToLogic(pArc->m_radius);
  1256. rect.left = origin.x - Radius - ExtraRange;
  1257. rect.top = origin.y - Radius - ExtraRange;
  1258. rect.right = origin.x + Radius + ExtraRange;
  1259. rect.bottom = origin.y + Radius + ExtraRange;
  1260. rect.NormalizeRect();
  1261. return rect;
  1262. }
  1263. void CManageGraphObject::RedrawArc(CDC* pDC)
  1264. {
  1265. int index;
  1266. CMyArc* pArc;
  1267. CGraphObjectInfo* pArcInfo;
  1268. index = m_ArcArray.GetSize();
  1269. //设置不填充状态
  1270. pDC->SelectStockObject(NULL_BRUSH);
  1271. while(index--)
  1272. {
  1273. pArc = (CMyArc*)m_ArcArray.GetAt(index);
  1274. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(index);
  1275. //判断图形对象所在层是否被隐藏
  1276. CFileLayer* pLayer = pManageLayer->GetLayer(pArc->m_layer);
  1277. if(!pLayer->IsShow())
  1278. continue;
  1279. //判断圆弧对象是否需要重画
  1280. if(!IsRedraw(GetRedrawRect(EffectRect, LogicClientRect), pArcInfo->m_rect))
  1281. continue;
  1282. if(pArcInfo->m_del == TRUE)
  1283. continue;
  1284. if(pArcInfo->m_selected == TRUE)
  1285. {
  1286. m_DrawGrapObject.DrawSelArc(pDC, DrawingToLogic(pArc->m_origin), DrawingToLogic(pArc->m_start), 
  1287.                         DrawingToLogic(pArc->m_end),
  1288. DrawingToLogic(pArc->m_radius), LineStyle, PenColor);
  1289. continue;
  1290. }
  1291.          
  1292. m_DrawGrapObject.DrawArc(pDC, DrawingToLogic(pArc->m_origin),
  1293.                      DrawingToLogic(pArc->m_start), DrawingToLogic(pArc->m_end),
  1294.                                  DrawingToLogic(pArc->m_radius), pArc->m_style, pArc->m_color);
  1295. }
  1296. }
  1297. void CManageGraphObject::SelectArc(CDC* pDC, CPoint point)
  1298. {
  1299. int nIndex;
  1300. POINT origin;
  1301. long r;
  1302. long distance;
  1303. CPoint start;
  1304. CPoint end;
  1305. CGraphObjectInfo* pArcInfo;
  1306. CSelectedObjectInfo* pSelObjInfo;
  1307. CMyArc* pArc; 
  1308. for(nIndex = 0; nIndex < m_ArcInfoArray.GetSize(); nIndex++)
  1309. {
  1310. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(nIndex);
  1311. pArc = (CMyArc*)m_ArcArray.GetAt(nIndex);
  1312. //判断图形对象所在层是否被隐藏
  1313. CFileLayer* pLayer = pManageLayer->GetLayer(pArc->m_layer);
  1314. if(!pLayer->IsShow())
  1315. continue;
  1316. if(pArcInfo ->m_del == TRUE || pArcInfo ->m_selected ==TRUE)
  1317. continue;
  1318. //判断选取点是否在图形对象所占据的选取逻辑区域内
  1319. CRect region = pArcInfo->m_rect;
  1320. region.InflateRect(SELECT_RANGE, SELECT_RANGE);
  1321.         if(!region.PtInRect(point))
  1322. continue;
  1323. origin = DrawingToLogic(pArc->m_origin);
  1324. start = DrawingToLogic(pArc->m_start);
  1325. end = DrawingToLogic(pArc->m_end);
  1326. r = DrawingToLogic(pArc->m_radius);
  1327. distance = (long)sqrt( (origin.x - point.x) * (origin.x - point.x) + (origin.y - point.y) * (origin.y - point.y) );
  1328. if(distance <= r + SELECT_RANGE && distance >= r - SELECT_RANGE)
  1329. {
  1330. m_DrawGrapObject.DrawSelArc(pDC, origin, start, end, r, pArc->m_style, pArc->m_color);
  1331. pArcInfo->m_selected = TRUE;
  1332. pSelObjInfo = new CSelectedObjectInfo(OBJECT_ARC, nIndex);
  1333. m_SelObjInfoArray.Add(pSelObjInfo);
  1334. }
  1335. }
  1336. }
  1337. BOOL CManageGraphObject::UnselectArc()
  1338. {
  1339. BOOL flag = FALSE;
  1340. int nIndex;
  1341. CSelectedObjectInfo* pSelObjInfo;
  1342. CGraphObjectInfo* pArcInfo;
  1343. for(nIndex = 0; nIndex < m_SelObjInfoArray.GetSize(); nIndex++)
  1344. {
  1345. pSelObjInfo = (CSelectedObjectInfo*)m_SelObjInfoArray.GetAt(nIndex);
  1346. if(pSelObjInfo->m_type ==OBJECT_ARC)
  1347. {
  1348. pArcInfo = (CGraphObjectInfo*)m_ArcInfoArray.GetAt(pSelObjInfo->m_index);
  1349. pArcInfo->m_selected = FALSE;
  1350. flag = TRUE;
  1351.         }
  1352.     }
  1353. return flag;
  1354. }
  1355. void CManageGraphObject::AddArc(POINT SPoint, POINT MPoint, POINT EPoint, int Style, COLORREF Color)
  1356. {
  1357. long x1, y1, x2, y2;
  1358. double originX;
  1359. double originY;
  1360. double k1;
  1361. double k2;
  1362. double mid1X;
  1363. double mid1Y;
  1364. double mid2X;
  1365. double mid2Y;
  1366. POINT OPoint;
  1367. long Radius;
  1368. CMyArc* pArc;
  1369. if(SPoint.y == MPoint.y)
  1370. k1 = 1000000.0;
  1371. else 
  1372. k1 = (- (double)(SPoint.x - MPoint.x) / (double)(SPoint.y - MPoint.y));
  1373. if(EPoint.y == MPoint.y)
  1374. k2 = 1000000.0;
  1375. else 
  1376. k2 =  (- (double)(EPoint.x - MPoint.x) / (double)(EPoint.y - MPoint.y));
  1377. mid1X = (double)(SPoint.x + MPoint.x) / 2.0;
  1378. mid1Y = (double)(SPoint.y + MPoint.y) / 2.0;
  1379. mid2X = (double)(EPoint.x + MPoint.x) / 2.0;
  1380. mid2Y = (double)(EPoint.y + MPoint.y) / 2.0;
  1381. if(k1 == k2)
  1382. return;
  1383. if(k1 == 1000000.0)
  1384. {
  1385. originX = mid1X;
  1386. originY = k2 * (originX - mid2X) + mid2Y;
  1387. }
  1388. else
  1389. if(k2 == 1000000.0)
  1390. {
  1391. originX = mid2X;
  1392. originY = k1 * (originX - mid1X) + mid1Y;
  1393. }
  1394. else
  1395. {
  1396. originX = ((k2 * mid2X - k1 * mid1X) - (mid2Y - mid1Y)) / (k2 - k1);
  1397. originY = k2 * (originX - mid2X) + mid2Y;
  1398. }
  1399. Radius = (long)sqrt(pow((double)(originX - SPoint.x), 2) + pow((double)(originY - SPoint.y), 2));
  1400. OPoint.x = (long)originX;
  1401. OPoint.y = (long)originY ;
  1402. x1 = MidPoint.x - SPoint.x;
  1403. y1 = MPoint.y - SPoint.y;
  1404. x2 = EPoint.x - SPoint.x;
  1405. y2 = EPoint.y - SPoint.y;
  1406. if(x1*y2 - x2*y1 < 0)
  1407. pArc = new CMyArc(CurrentLayerName, LogicToDrawing(OPoint),
  1408.                   LogicToDrawing(SPoint), LogicToDrawing(EPoint), 
  1409.   LogicToDrawing(Radius), LineStyle, PenColor);
  1410. else
  1411. pArc = new CMyArc(CurrentLayerName, LogicToDrawing(OPoint), 
  1412.                   LogicToDrawing(EPoint), LogicToDrawing(SPoint), 
  1413.   LogicToDrawing(Radius), LineStyle, PenColor);
  1414. m_ArcArray.Add(pArc);
  1415. CGraphObjectInfo* pArcInfo = new CGraphObjectInfo(GetArcRange(0, pArc), FALSE, FALSE);
  1416. m_ArcInfoArray.Add(pArcInfo);
  1417. }
  1418. CMyArc* CManageGraphObject::GetArc(int index)
  1419. {
  1420. ASSERT(index >= 0 && index <= m_ArcArray.GetUpperBound());
  1421. return (CMyArc*)m_ArcArray.GetAt(index);
  1422. }
  1423. void CManageGraphObject::DelArc(int index)
  1424. {
  1425. ASSERT(index >= 0 && index <= m_ArcArray.GetUpperBound());
  1426. delete (CMyArc*)m_ArcArray.GetAt(index);
  1427. m_ArcArray.RemoveAt(index);
  1428. delete (CGraphObjectInfo*)m_ArcInfoArray.GetAt(index);
  1429. m_ArcInfoArray.RemoveAt(index);
  1430. }
  1431. void CManageGraphObject::DelAllArc()
  1432. {
  1433. int index;
  1434. index = m_ArcArray.GetSize();
  1435. while(index--)
  1436. delete (CMyArc*)m_ArcArray.GetAt(index);
  1437. m_ArcArray.RemoveAll();
  1438. index = m_ArcInfoArray.GetSize();
  1439. while(index--)
  1440. delete (CGraphObjectInfo*)m_ArcInfoArray.GetAt(index);
  1441. m_ArcInfoArray.RemoveAll();
  1442. }