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