DrawSysView.cpp
Upload User: taoguanmin
Upload Date: 2007-05-11
Package Size: 180k
Code Size: 38k
Category:

2D Graphic

Development Platform:

Visual C++

  1. // DrawSysView.cpp : implementation of the CDrawSysView class
  2. //
  3. #include "stdafx.h"
  4. #include "DrawSys.h"
  5. #include "math.h"
  6. #include "MainFrm.h"
  7. #include "DrawSysDoc.h"
  8. #include "DrawSysView.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CDrawSysView
  16. IMPLEMENT_DYNCREATE(CDrawSysView, CView)
  17. BEGIN_MESSAGE_MAP(CDrawSysView, CView)
  18. //{{AFX_MSG_MAP(CDrawSysView)
  19. ON_COMMAND(IDM_LINE, OnLine)
  20. ON_COMMAND(IDM_CIRCLE, OnCircle)
  21. ON_UPDATE_COMMAND_UI(IDM_LINE, OnUpdateLine)
  22. ON_UPDATE_COMMAND_UI(IDM_CIRCLE, OnUpdateCircle)
  23. ON_WM_LBUTTONDOWN()
  24. ON_WM_LBUTTONUP()
  25. ON_WM_MOUSEMOVE()
  26. ON_COMMAND(IDM_DDA, OnDda)
  27. ON_COMMAND(IDM_BRESENHAM, OnBresenham)
  28. ON_COMMAND(IDM_SET, OnSet)
  29. ON_COMMAND(IDM_MANYBD, OnManybd)
  30. ON_UPDATE_COMMAND_UI(IDM_MANYBD, OnUpdateManybd)
  31. ON_WM_LBUTTONDBLCLK()
  32. ON_COMMAND(IDM_CUT, OnCut)
  33. ON_UPDATE_COMMAND_UI(IDM_CUT, OnUpdateCut)
  34. ON_WM_RBUTTONDOWN()
  35. ON_WM_RBUTTONUP()
  36. ON_WM_RBUTTONDBLCLK()
  37. ON_COMMAND(IDM_BEZIER, OnBezier)
  38. ON_UPDATE_COMMAND_UI(IDM_BEZIER, OnUpdateBezier)
  39. ON_COMMAND(IDM_TEXT, OnText)
  40. ON_COMMAND(IDM_CLEAR, OnClear)
  41. ON_COMMAND(IDM_STROKE, OnStroke)
  42. ON_UPDATE_COMMAND_UI(IDM_STROKE, OnUpdateStroke)
  43. ON_UPDATE_COMMAND_UI(IDM_TEXT, OnUpdateText)
  44. ON_COMMAND(IDM_TEST, OnTest)
  45. ON_COMMAND(IDM_RANDLINE, OnRandline)
  46. ON_COMMAND(IDM_REFRESH, OnRefresh)
  47. //}}AFX_MSG_MAP
  48. // Standard printing commands
  49. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  50. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  51. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CDrawSysView construction/destruction
  55. CDrawSysView::CDrawSysView()
  56. {
  57. // TODO: add construction code here
  58. m_dsDrawSort=DsNull;
  59. m_dlsDrawLSort=DlsBresenham;
  60. m_startPoint.x=0;
  61. m_startPoint.y=0;
  62. m_endPoint.x=0;
  63. m_endPoint.y=0;
  64. m_bIsMouseDown=0;
  65. m_bIsFirstDone=0;
  66. m_bIsDel=0;
  67. m_lPenColor=RGB(0,0,255);
  68. m_lBkColor=RGB(255,255,255);
  69. m_lFillColor=RGB(255,0,0);
  70. m_iPenWidth=1;
  71. m_lPenColor=RGB(0,0,255);
  72. m_dlsDrawLSort=DlsBresenham;
  73. m_hDLCursor=AfxGetApp()->LoadCursor(IDC_DRAWLINE);
  74. m_hDCCursor=AfxGetApp()->LoadCursor(IDC_DRAWCIRCLE);
  75. m_hCLCursor=AfxGetApp()->LoadCursor(IDC_CUTLINE);
  76. m_hDBCursor=AfxGetApp()->LoadCursor(IDC_DRAWBEZIER);
  77. m_hDTCursor=AfxGetApp()->LoadCursor(IDC_DRAWTEXT);
  78. m_hDSCursor=AfxGetApp()->LoadCursor(IDC_DRAWSTROKE);
  79. m_pwndStatusBar=0;
  80. }
  81. CDrawSysView::~CDrawSysView()
  82. {
  83. }
  84. BOOL CDrawSysView::PreCreateWindow(CREATESTRUCT& cs)
  85. {
  86. // TODO: Modify the Window class or styles here by modifying
  87. //  the CREATESTRUCT cs
  88. return CView::PreCreateWindow(cs);
  89. }
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CDrawSysView drawing
  92. void CDrawSysView::OnDraw(CDC* pDC)
  93. {
  94. CDrawSysDoc* pDoc = GetDocument();
  95. ASSERT_VALID(pDoc);
  96. // TODO: add draw code for native data here
  97. list<LINE>::iterator pLine;
  98. list<CIRCLE>::iterator pCircle;
  99. list<BEZIER>::iterator pBezier;
  100. vector<TEXT>::iterator pText;
  101. //画直线
  102. for(pLine=m_listAllLine.begin();pLine!=m_listAllLine.end();pLine++)
  103. Line(*pLine,pDC);
  104. //画圆
  105. for(pCircle=m_listAllCircle.begin();pCircle!=m_listAllCircle.end();pCircle++)
  106. Circle(*pCircle,pDC);
  107. //画Bezier曲线
  108. for(pBezier=m_listBezier.begin();pBezier!=m_listBezier.end();pBezier++)
  109. Bezier(*pBezier,pDC);
  110. //画文本
  111. for(pText=m_vectorText.begin();pText!=m_vectorText.end();pText++)
  112. pDC->TextOut((*pText).p.x,(*pText).p.y,(*pText).str);
  113. CRect rectText;
  114. GetClientRect(&rectText);
  115. rectText.left=rectText.right-290;
  116. rectText.top=rectText.bottom-50;
  117. }
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CDrawSysView printing
  120. BOOL CDrawSysView::OnPreparePrinting(CPrintInfo* pInfo)
  121. {
  122. // default preparation
  123. return DoPreparePrinting(pInfo);
  124. }
  125. void CDrawSysView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  126. {
  127. // TODO: add extra initialization before printing
  128. }
  129. void CDrawSysView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  130. {
  131. // TODO: add cleanup after printing
  132. }
  133. /////////////////////////////////////////////////////////////////////////////
  134. // CDrawSysView diagnostics
  135. #ifdef _DEBUG
  136. void CDrawSysView::AssertValid() const
  137. {
  138. CView::AssertValid();
  139. }
  140. void CDrawSysView::Dump(CDumpContext& dc) const
  141. {
  142. CView::Dump(dc);
  143. }
  144. CDrawSysDoc* CDrawSysView::GetDocument() // non-debug version is inline
  145. {
  146. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawSysDoc)));
  147. return (CDrawSysDoc*)m_pDocument;
  148. }
  149. #endif //_DEBUG
  150. /////////////////////////////////////////////////////////////////////////////
  151. // CDrawSysView message handlers
  152. void CDrawSysView::OnLine()
  153. {
  154. // TODO: Add your command handler code here
  155. if(m_dsDrawSort!=DsLine)
  156. {
  157. TEXT tTemp;
  158. int nSize;
  159. CDC *pDC=GetDC();
  160. switch(m_dsDrawSort)
  161. {
  162. case DsManyBd:
  163. if(m_bIsFirstDone)
  164. {
  165. CDC *pDC=GetDC();
  166. LINE lTemp;
  167. Line(m_startPoint,m_spManyBd,pDC);
  168. lTemp.pStart=m_startPoint;
  169. lTemp.pEnd=m_spManyBd;
  170. m_listAllLine.push_back(lTemp);
  171. }
  172. break;
  173. case DsBezier:
  174. m_listBezier.push_back(m_vectorCurBezier);
  175. m_vectorCurBezier.clear();
  176. break;
  177. case DsText:
  178. m_edtTextIn.GetWindowText(tTemp.str);
  179. if(tTemp.str!="")
  180. {
  181. m_startPoint.y-=7;
  182. tTemp.p=m_startPoint;
  183. m_vectorText.push_back(tTemp);
  184. }
  185. m_edtTextIn.ShowWindow(0);
  186. m_edtTextIn.SetWindowText("");
  187. nSize=m_vectorText.size();
  188. if(nSize>0)
  189. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  190. break;
  191. }
  192. m_dsDrawSort=DsLine;
  193. }
  194. else
  195. m_dsDrawSort=DsNull;
  196. m_bIsFirstDone=0;
  197. m_bIsMouseDown=0;
  198. }
  199. void CDrawSysView::OnCircle()
  200. {
  201. // TODO: Add your command handler code here
  202. if(m_dsDrawSort!=DsCircle)
  203. {
  204. TEXT tTemp;
  205. int nSize;
  206. CDC *pDC=GetDC();
  207. switch(m_dsDrawSort)
  208. {
  209. case DsManyBd:
  210. if(m_bIsFirstDone)
  211. {
  212. CDC *pDC=GetDC();
  213. LINE lTemp;
  214. Line(m_startPoint,m_spManyBd,pDC);
  215. lTemp.pStart=m_startPoint;
  216. lTemp.pEnd=m_spManyBd;
  217. m_listAllLine.push_back(lTemp);
  218. }
  219. break;
  220. case DsBezier:
  221. m_listBezier.push_back(m_vectorCurBezier);
  222. m_vectorCurBezier.clear();
  223. break;
  224. case DsText:
  225. m_edtTextIn.GetWindowText(tTemp.str);
  226. if(tTemp.str!="")
  227. {
  228. m_startPoint.y-=7;
  229. tTemp.p=m_startPoint;
  230. m_vectorText.push_back(tTemp);
  231. }
  232. m_edtTextIn.ShowWindow(0);
  233. m_edtTextIn.SetWindowText("");
  234. nSize=m_vectorText.size();
  235. if(nSize>0)
  236. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  237. break;
  238. }
  239. m_dsDrawSort=DsCircle;
  240. }
  241. else
  242. m_dsDrawSort=DsNull;
  243. m_bIsFirstDone=0;
  244. m_bIsMouseDown=0;
  245. }
  246. void CDrawSysView::OnUpdateLine(CCmdUI* pCmdUI) 
  247. {
  248. // TODO: Add your command update UI handler code here
  249. pCmdUI->SetCheck(m_dsDrawSort==DsLine);
  250. }
  251. void CDrawSysView::OnUpdateCircle(CCmdUI* pCmdUI) 
  252. {
  253. // TODO: Add your command update UI handler code here
  254. pCmdUI->SetCheck(m_dsDrawSort==DsCircle);
  255. }
  256. void CDrawSysView::OnLButtonDown(UINT nFlags, CPoint point) 
  257. {
  258. // TODO: Add your message handler code here and/or call default
  259. m_bIsMouseDown=1;
  260. if(m_dsDrawSort==DsNull)
  261. return;
  262. CDC *pDC=GetDC();
  263. LINE lTemp;
  264. CRect rect;
  265. TEXT tTemp;
  266. int nSize;
  267. DWORD dwStyle=WS_CHILD | ES_WANTRETURN | WS_TABSTOP;;
  268. pDC->SetROP2(R2_NOTXORPEN);
  269. switch(m_dsDrawSort)
  270. {
  271. case DsBezier:
  272. Bezier(m_vectorCurBezier,pDC);
  273. m_vectorCurBezier.push_back(point);
  274. Bezier(m_vectorCurBezier,pDC);
  275. break;
  276. case DsText:
  277. if(!m_edtTextIn)
  278. {
  279. if (!m_edtTextIn.CreateEx(WS_EX_CLIENTEDGE,_T("Edit"),NULL, 
  280. dwStyle,CRect(0,0,0,0), this,IDC_EDIT_TEXTIN))
  281. {
  282. TRACE(_T("Failed to create Edit TextIn.n"));
  283. OnClose();
  284. }
  285. CFont *font = CFont::FromHandle((HFONT)::GetStockObject(DEFAULT_GUI_FONT));
  286. m_edtTextIn.SetFont(font);
  287. }
  288. rect.left=point.x;
  289. rect.bottom=point.y+10;
  290. rect.right=rect.left+90;
  291. rect.top=rect.bottom-20;
  292. m_edtTextIn.MoveWindow(&rect,1);
  293. m_edtTextIn.UpdateWindow();
  294. m_edtTextIn.ShowWindow(1);
  295. m_edtTextIn.GetWindowText(tTemp.str);
  296. if(tTemp.str!="")
  297. {
  298. m_startPoint.y-=7;
  299. tTemp.p=m_startPoint;
  300. m_vectorText.push_back(tTemp);
  301. }
  302. m_edtTextIn.SetWindowText("");
  303. nSize=m_vectorText.size();
  304. if(nSize>0)
  305. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  306. break;
  307. }
  308. if(m_dsDrawSort==DsManyBd && m_bIsFirstDone)
  309. {
  310. Line(m_startPoint,point,pDC);
  311. lTemp.pStart=m_startPoint;
  312. lTemp.pEnd=point;
  313. m_listAllLine.push_back(lTemp);
  314. }
  315. if(m_dsDrawSort!=DsManyBd || !m_bIsFirstDone)
  316. {
  317. m_endPoint.x=m_startPoint.x=point.x;
  318. m_endPoint.y=m_startPoint.y=point.y;
  319. }
  320. m_endPoint.x=point.x;
  321. m_endPoint.y=point.y;
  322. switch(m_dsDrawSort)
  323. {
  324. case DsLine:
  325. case DsManyBd:
  326. SetCursor(m_hDLCursor);
  327. break;
  328. case DsCircle:
  329. SetCursor(m_hDCCursor);
  330. break;
  331. case DsCutLine:
  332. SetCursor(m_hCLCursor);
  333. break;
  334. case DsBezier:
  335. SetCursor(m_hDBCursor);
  336. break;
  337. case DsText:
  338. SetCursor(m_hDTCursor);
  339. break;
  340. case DsStroke:
  341. SetCursor(m_hDSCursor);
  342. break;
  343. }
  344. CView::OnLButtonDown(nFlags, point);
  345. }
  346. void CDrawSysView::Line_DDA(POINT p1, POINT p2,CDC *pDC)
  347. {
  348. int i;
  349. if(p1.x==p2.x)
  350. {
  351. //为竖线
  352. if(p1.y<=p2.y)
  353. {
  354. for(i=p1.y;i<=p2.y;i++)
  355. pDC->SetPixel(p1.x,i,m_lPenColor);
  356. }
  357. else
  358. {
  359. for(i=p2.y;i<=p1.y;i++)
  360. pDC->SetPixel(p1.x,i,m_lPenColor);
  361. }
  362. return;
  363. }
  364. //为横线
  365. if(p1.y==p2.y)
  366. {
  367. if(p1.x<=p2.x)
  368. {
  369. for(i=p1.x;i<=p2.x;i++)
  370. pDC->SetPixel(i,p1.y,m_lPenColor);
  371. }
  372. else
  373. {
  374. for(i=p2.x;i<=p1.x;i++)
  375. pDC->SetPixel(i,p1.y,m_lPenColor);
  376. }
  377. return;
  378. }
  379. //为斜线
  380. float m=(p2.y-p1.y)*1.0/(p2.x-p1.x);
  381. float fTemp;
  382. if(abs(m)<=1)
  383. {
  384. if(p1.x<p2.x)
  385. {
  386. fTemp=p1.y-m;
  387. for(i=p1.x;i<=p2.x;i++)
  388. pDC->SetPixel(i,fTemp+=m,m_lPenColor);
  389. }
  390. else
  391. {
  392. fTemp=p2.y-m;
  393. for(i=p2.x;i<=p1.x;i++)
  394. pDC->SetPixel(i,fTemp+=m,m_lPenColor);
  395. }
  396. return;
  397. }
  398. if(p1.y<p2.y)
  399. {
  400. fTemp=p1.x-1/m;
  401. for(i=p1.y;i<=p2.y;i++)
  402. pDC->SetPixel(fTemp+=1/m,i,m_lPenColor);
  403. }
  404. else
  405. {
  406. fTemp=p2.x-1/m;
  407. for(i=p2.y;i<=p1.y;i++)
  408. pDC->SetPixel(fTemp+=1/m,i,m_lPenColor);
  409. }
  410. }
  411. void CDrawSysView::Line_Bresenham(POINT p1, POINT p2,CDC *pDC)
  412. {
  413. int i;
  414. if(p1.x==p2.x)
  415. {
  416. //为竖线
  417. if(p1.y<=p2.y)
  418. {
  419. for(i=p1.y;i<=p2.y;i++)
  420. pDC->SetPixel(p1.x,i,m_lPenColor);
  421. }
  422. else
  423. {
  424. for(i=p2.y;i<=p1.y;i++)
  425. pDC->SetPixel(p1.x,i,m_lPenColor);
  426. }
  427. return;
  428. }
  429. //为横线
  430. if(p1.y==p2.y)
  431. {
  432. if(p1.x<=p2.x)
  433. {
  434. for(i=p1.x;i<=p2.x;i++)
  435. pDC->SetPixel(i,p1.y,m_lPenColor);
  436. }
  437. else
  438. {
  439. for(i=p2.x;i<=p1.x;i++)
  440. pDC->SetPixel(i,p1.y,m_lPenColor);
  441. }
  442. return;
  443. }
  444. //为斜线
  445. float m=(p2.y-p1.y)*1.0/(p2.x-p1.x);
  446. float p;
  447. p=2*m-1;
  448. if(m>0 && m<=1)
  449. {
  450. if(p1.x<p2.x)
  451. {
  452. while(p1.x<=p2.x)
  453. {
  454. pDC->SetPixel(p1.x++,p1.y,m_lPenColor);
  455. if(p>=0)
  456. {
  457. p+=2*m-2;
  458. p1.y++;
  459. }
  460. else
  461. p+=2*m;
  462. }
  463. }
  464. else
  465. {
  466. while(p2.x<=p1.x)
  467. {
  468. pDC->SetPixel(p2.x++,p2.y,m_lPenColor);
  469. if(p>=0)
  470. {
  471. p+=2*m-2;
  472. p2.y++;
  473. }
  474. else
  475. p+=2*m;
  476. }
  477. }
  478. return;
  479. }
  480. p=-2*m-1;
  481. if(m<0 && m>=-1)
  482. {
  483. if(p1.x<p2.x)
  484. {
  485. while(p1.x<=p2.x)
  486. {
  487. pDC->SetPixel(p1.x++,p1.y,m_lPenColor);
  488. if(p>=0)
  489. {
  490. p+=-2*m-2;
  491. p1.y--;
  492. }
  493. else
  494. p+=-2*m;
  495. }
  496. }
  497. else
  498. {
  499. while(p2.x<=p1.x)
  500. {
  501. pDC->SetPixel(p2.x++,p2.y,m_lPenColor);
  502. if(p>=0)
  503. {
  504. p+=-2*m-2;
  505. p2.y--;
  506. }
  507. else
  508. p+=-2*m;
  509. }
  510. }
  511. return;
  512. }
  513. p=2/m-1;
  514. if(m>1)
  515. {
  516. if(p1.y<p2.y)
  517. {
  518. while(p1.y<=p2.y)
  519. {
  520. pDC->SetPixel(p1.x,p1.y++,m_lPenColor);
  521. if(p>=0)
  522. {
  523. p+=2/m-2;
  524. p1.x++;
  525. }
  526. else
  527. p+=2/m;
  528. }
  529. }
  530. else
  531. {
  532. while(p2.y<=p1.y)
  533. {
  534. pDC->SetPixel(p2.x,p2.y++,m_lPenColor);
  535. if(p>=0)
  536. {
  537. p+=2/m-2;
  538. p2.x++;
  539. }
  540. else
  541. p+=2/m;
  542. }
  543. }
  544. return;
  545. }
  546. p=-2/m-1;
  547. if(p1.y<p2.y)
  548. {
  549. while(p1.y<=p2.y)
  550. {
  551. pDC->SetPixel(p1.x,p1.y++,m_lPenColor);
  552. if(p>=0)
  553. {
  554. p+=-2/m-2;
  555. p1.x--;
  556. }
  557. else
  558. p+=-2/m;
  559. }
  560. }
  561. else
  562. {
  563. while(p2.y<=p1.y)
  564. {
  565. pDC->SetPixel(p2.x,p2.y++,m_lPenColor);
  566. if(p>=0)
  567. {
  568. p+=-2/m-2;
  569. p2.x--;
  570. }
  571. else
  572. p+=-2/m;
  573. }
  574. }
  575. }
  576. void CDrawSysView::OnLButtonUp(UINT nFlags, CPoint point) 
  577. {
  578. // TODO: Add your message handler code here and/or call default
  579. m_bIsMouseDown=0;
  580. if(m_dsDrawSort==DsNull)
  581. return;
  582. CDC *pDC=GetDC();
  583. CPen Pen;
  584. LINE lTemp;
  585. CIRCLE cTemp;
  586. int r;
  587. RECT rect;
  588. list<LINE>::iterator pLine=m_listAllLine.begin();
  589. list<LINE>::iterator pTemp;
  590. switch(m_dsDrawSort)
  591. {
  592. case DsManyBd:
  593. case DsLine:
  594. Line(m_startPoint,point,GetDC());
  595. lTemp.pStart=m_startPoint;
  596. lTemp.pEnd=point;
  597. m_listAllLine.push_back(lTemp);
  598. if(m_dsDrawSort==DsManyBd)
  599. m_startPoint=point;
  600. SetCursor(m_hDLCursor);
  601. break;
  602. case DsCircle:
  603. r=GetDist(m_startPoint,point);
  604. Circle(m_startPoint,r,pDC);
  605. cTemp.pMid=m_startPoint;
  606. cTemp.r=r;
  607. m_listAllCircle.push_back(cTemp);
  608. pDC->SetROP2(R2_NOTXORPEN);
  609. Line(m_startPoint,point,pDC);
  610. SetCursor(m_hDCCursor);
  611. break;
  612. case DsCutLine:
  613. rect.left=m_startPoint.x;
  614. rect.top=m_startPoint.y;
  615. rect.right=point.x;
  616. rect.bottom=point.y;
  617. while(pLine!=m_listAllLine.end())
  618. {
  619. CutLine(rect,*pLine);
  620. if(!(*pLine).pStart.x && !(*pLine).pStart.y && !(*pLine).pEnd.x && !(*pLine).pEnd.y)
  621. {
  622. pTemp=pLine;
  623. pTemp++;
  624. m_listAllLine.remove(*pLine);
  625. pLine=pTemp;
  626. }
  627. else
  628. pLine++;
  629. }
  630. GetClientRect(&rect);
  631. InvalidateRect(&rect,1);
  632. SetCursor(m_hCLCursor);
  633. break;
  634. case DsBezier:
  635. Bezier(m_vectorCurBezier,pDC);
  636. m_vectorCurBezier.pop_back();
  637. m_vectorCurBezier.push_back(point);
  638. Bezier(m_vectorCurBezier,pDC);
  639. SetCursor(m_hDBCursor);
  640. break;
  641. case DsText:
  642. SetCursor(m_hDTCursor);
  643. break;
  644. case DsStroke:
  645. SetCursor(m_hDSCursor);
  646. break;
  647. }
  648. CView::OnLButtonUp(nFlags, point);
  649. }
  650. void CDrawSysView::OnMouseMove(UINT nFlags, CPoint point) 
  651. {
  652. // TODO: Add your message handler code here and/or call default
  653. CString str;
  654. CDC *pDC=GetDC();
  655. str.Format("X:%d,Y:%d",point.x,point.y);
  656. if(!m_pwndStatusBar)
  657. m_pwndStatusBar=((CMainFrame*)AfxGetMainWnd())->GetStatusBar();
  658. m_pwndStatusBar->SetPaneText(1,str,1);
  659. if(m_dsDrawSort==DsNull)
  660. return;
  661. switch(m_dsDrawSort)
  662. {
  663. case DsLine:
  664. case DsManyBd:
  665. SetCursor(m_hDLCursor);
  666. break;
  667. case DsCircle:
  668. SetCursor(m_hDCCursor);
  669. break;
  670. case DsCutLine:
  671. SetCursor(m_hCLCursor);
  672. break;
  673. case DsBezier:
  674. SetCursor(m_hDBCursor);
  675. break;
  676. case DsText:
  677. SetCursor(m_hDTCursor);
  678. break;
  679. case DsStroke:
  680. SetCursor(m_hDSCursor);
  681. break;
  682. }
  683. if(!m_bIsMouseDown)
  684. return;
  685. CPen pen;
  686. CRect rect;
  687. list<LINE>::iterator p=m_listAllLine.end();
  688. vector<POINT>::iterator pVector=m_vectorCurBezier.end();
  689. pDC->SetROP2(R2_NOTXORPEN);
  690. switch(m_dsDrawSort)
  691. {
  692. case DsManyBd:
  693. p--;
  694. if((*p).pStart.x==m_startPoint.x && (*p).pStart.y==m_startPoint.y)
  695. m_listAllLine.pop_back();
  696. if(!m_bIsFirstDone)
  697. m_spManyBd=m_startPoint;
  698. m_bIsFirstDone=1;
  699. case DsLine:
  700. Line(m_startPoint,m_endPoint,pDC);
  701. Line(m_startPoint,point,pDC);
  702. break;
  703. case DsCircle:
  704. Line(m_startPoint,m_endPoint,pDC);
  705. Line(m_startPoint,point,pDC);
  706. Circle(m_startPoint,GetDist(m_startPoint,m_endPoint),pDC);
  707. Circle(m_startPoint,GetDist(m_startPoint,point),pDC);
  708. break;
  709. case DsCutLine:
  710. pen.CreatePen(PS_DOT,m_iPenWidth,m_lPenColor);
  711. pDC->SelectObject(&pen);
  712. pDC->Rectangle(&CRect(m_startPoint,m_endPoint));
  713. pDC->Rectangle(&CRect(m_startPoint,point));
  714. break;
  715. case DsBezier:
  716. Bezier(m_vectorCurBezier,pDC);
  717. m_vectorCurBezier.pop_back();
  718. m_vectorCurBezier.push_back(point);
  719. Bezier(m_vectorCurBezier,pDC);
  720. break;
  721. case DsStroke:
  722. pen.CreatePen(PS_SOLID,m_iPenWidth,m_lPenColor);
  723. pDC->SelectObject(&pen);
  724. pDC->MoveTo(m_endPoint);
  725. pDC->LineTo(point);
  726. break;
  727. }
  728. m_endPoint=point;
  729. CView::OnMouseMove(nFlags, point);
  730. }
  731. void CDrawSysView::Circle(POINT pc, int r,CDC *pDC)
  732. {
  733. int x,y;
  734. float p;
  735. pDC->SetViewportOrg(pc);
  736. x=0;
  737. y=r;
  738. p=1.25-r;
  739. while(x<=y+1)
  740. {
  741. //第1象限
  742. pDC->SetPixel(x,y,m_lPenColor);
  743. pDC->SetPixel(y,x,m_lPenColor);
  744. //第2象限
  745. pDC->SetPixel(-x,y,m_lPenColor);
  746. pDC->SetPixel(-y,x,m_lPenColor);
  747. //第3象限
  748. pDC->SetPixel(-y,-x,m_lPenColor);
  749. pDC->SetPixel(-x,-y,m_lPenColor);
  750. //第4象限
  751. pDC->SetPixel(x,-y,m_lPenColor);
  752. pDC->SetPixel(y,-x,m_lPenColor);
  753. x++;
  754. if(p>=0)
  755. {
  756. y--;
  757. p+=2*x+1-2*y;
  758. }
  759. else
  760. p+=2*x+1;
  761. }
  762. pDC->SetViewportOrg(0,0);
  763. }
  764. int CDrawSysView::GetDist(POINT p1, POINT p2)
  765. {
  766. return pow(pow(p1.y-p2.y,2)+pow(p1.x-p2.x,2),1.0/2);
  767. }
  768. void CDrawSysView::OnDda() 
  769. {
  770. // TODO: Add your command handler code here
  771. m_dlsDrawLSort=DlsDDA;
  772. }
  773. void CDrawSysView::OnBresenham() 
  774. {
  775. // TODO: Add your command handler code here
  776. m_dlsDrawLSort=DlsBresenham;
  777. }
  778. void CDrawSysView::OnSet()
  779. {
  780. // TODO: Add your command handler code here
  781. if(m_SetDlg.DoModal()==IDOK)
  782. {
  783. m_iPenWidth=m_SetDlg.GetPenWidth();
  784. m_lPenColor=m_SetDlg.GetPenColor();
  785. m_lBkColor=m_SetDlg.GetBkColor();
  786. m_lFillColor=m_SetDlg.GetFillColor();
  787. m_dlsDrawLSort=m_SetDlg.GetDrawLSort();
  788. CDC *pDC=GetDC();
  789. CPen Pen;
  790. pDC->SetBkColor(m_lBkColor);
  791. Pen.CreatePen(PS_SOLID,m_iPenWidth,m_lPenColor);
  792. pDC->SelectObject(&Pen);
  793. }
  794. }
  795. void CDrawSysView::OnManybd() 
  796. {
  797. // TODO: Add your command handler code here
  798. if(m_dsDrawSort!=DsManyBd)
  799. {
  800. TEXT tTemp;
  801. int nSize;
  802. CDC *pDC=GetDC();
  803. switch(m_dsDrawSort)
  804. {
  805. case DsBezier:
  806. m_listBezier.push_back(m_vectorCurBezier);
  807. m_vectorCurBezier.clear();
  808. break;
  809. case DsText:
  810. m_edtTextIn.GetWindowText(tTemp.str);
  811. if(tTemp.str!="")
  812. {
  813. m_startPoint.y-=7;
  814. tTemp.p=m_startPoint;
  815. m_vectorText.push_back(tTemp);
  816. }
  817. m_edtTextIn.ShowWindow(0);
  818. m_edtTextIn.SetWindowText("");
  819. nSize=m_vectorText.size();
  820. if(nSize>0)
  821. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  822. break;
  823. }
  824. m_dsDrawSort=DsManyBd;
  825. }
  826. else
  827. {
  828. if(m_bIsFirstDone)
  829. {
  830. CDC *pDC=GetDC();
  831. LINE lTemp;
  832. Line(m_startPoint,m_spManyBd,pDC);
  833. lTemp.pStart=m_startPoint;
  834. lTemp.pEnd=m_spManyBd;
  835. m_listAllLine.push_back(lTemp);
  836. }
  837. m_dsDrawSort=DsNull;
  838. }
  839. m_bIsFirstDone=0;
  840. m_bIsMouseDown=0;
  841. }
  842. void CDrawSysView::OnUpdateManybd(CCmdUI* pCmdUI) 
  843. {
  844. // TODO: Add your command update UI handler code here
  845. pCmdUI->SetCheck(m_dsDrawSort==DsManyBd);
  846. }
  847. void CDrawSysView::OnLButtonDblClk(UINT nFlags, CPoint point) 
  848. {
  849. // TODO: Add your message handler code here and/or call default
  850. if(m_dsDrawSort==DsNull)
  851. return;
  852. switch(m_dsDrawSort)
  853. {
  854. case DsLine:
  855. case DsManyBd:
  856. SetCursor(m_hDLCursor);
  857. break;
  858. case DsCircle:
  859. SetCursor(m_hDCCursor);
  860. break;
  861. case DsCutLine:
  862. SetCursor(m_hCLCursor);
  863. break;
  864. case DsBezier:
  865. SetCursor(m_hDBCursor);
  866. break;
  867. case DsText:
  868. SetCursor(m_hDTCursor);
  869. break;
  870. case DsStroke:
  871. SetCursor(m_hDSCursor);
  872. break;
  873. }
  874. CView::OnLButtonDblClk(nFlags, point);
  875. }
  876. void CDrawSysView::OnCut() 
  877. {
  878. // TODO: Add your command handler code here
  879. if(m_dsDrawSort!=DsCutLine)
  880. {
  881. TEXT tTemp;
  882. int nSize;
  883. CDC *pDC=GetDC();
  884. switch(m_dsDrawSort)
  885. {
  886. case DsManyBd:
  887. if(m_bIsFirstDone)
  888. {
  889. CDC *pDC=GetDC();
  890. LINE lTemp;
  891. Line(m_startPoint,m_spManyBd,pDC);
  892. lTemp.pStart=m_startPoint;
  893. lTemp.pEnd=m_spManyBd;
  894. m_listAllLine.push_back(lTemp);
  895. }
  896. break;
  897. case DsBezier:
  898. m_listBezier.push_back(m_vectorCurBezier);
  899. m_vectorCurBezier.clear();
  900. break;
  901. case DsText:
  902. m_edtTextIn.GetWindowText(tTemp.str);
  903. if(tTemp.str!="")
  904. {
  905. m_startPoint.y-=7;
  906. tTemp.p=m_startPoint;
  907. m_vectorText.push_back(tTemp);
  908. }
  909. m_edtTextIn.ShowWindow(0);
  910. m_edtTextIn.SetWindowText("");
  911. nSize=m_vectorText.size();
  912. if(nSize>0)
  913. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  914. break;
  915. }
  916. m_dsDrawSort=DsCutLine;
  917. }
  918. else
  919. m_dsDrawSort=DsNull;
  920. m_bIsFirstDone=0;
  921. m_bIsMouseDown=0;
  922. }
  923. void CDrawSysView::OnUpdateCut(CCmdUI* pCmdUI) 
  924. {
  925. // TODO: Add your command update UI handler code here
  926. pCmdUI->SetCheck(m_dsDrawSort==DsCutLine);
  927. }
  928. void CDrawSysView::CutLine(RECT rect,POINT& p1, POINT& p2)
  929. {
  930. //说明:此坐标为设备坐标
  931. int iRgnCode1=0;
  932. int iRgnCode2=0;
  933. int temp;
  934. POINT ptTemp;
  935. //使p1在前面
  936. if(p1.x>p2.x)
  937. {
  938. ptTemp=p1;
  939. p1=p2;
  940. p2=ptTemp;
  941. }
  942. //使bottom比top大,right比left大
  943. if(rect.bottom<rect.top)
  944. {
  945. temp=rect.bottom;
  946. rect.bottom=rect.top;
  947. rect.top=temp;
  948. }
  949. if(rect.right<rect.left)
  950. {
  951. temp=rect.right;
  952. rect.right=rect.left;
  953. rect.left=temp;
  954. }
  955. //----------求p1和p2的区域码----------
  956. if(p1.x<rect.left)
  957. iRgnCode1+=1;
  958. if(p1.x>rect.right)
  959. iRgnCode1+=1<<1;
  960. if(p1.y>rect.bottom)
  961. iRgnCode1+=1<<2;
  962. if(p1.y<rect.top)
  963. iRgnCode1+=1<<3;
  964. if(p2.x<rect.left)
  965. iRgnCode2+=1;
  966. if(p2.x>rect.right)
  967. iRgnCode2+=1<<1;
  968. if(p2.y>rect.bottom)
  969. iRgnCode2+=1<<2;
  970. if(p2.y<rect.top)
  971. iRgnCode2+=1<<3;
  972. //------------------------------------
  973. //被裁剪的线段在矩形内
  974. if(!iRgnCode1 && !iRgnCode2)
  975. return;
  976. //被裁剪的线段在矩形外
  977. if((iRgnCode1 & iRgnCode2)!=0 && (iRgnCode1 || iRgnCode2))
  978. {
  979. p1.x=p2.x=0;
  980. p1.y=p2.y=0;
  981. return;
  982. }
  983. //竖线
  984. if(p1.x==p2.x)
  985. {
  986. if(p1.y>p2.y)
  987. {
  988. temp=p1.y;
  989. p1.y=p2.y;
  990. p2.y=temp;
  991. }
  992. if(p1.y<rect.top)
  993. {
  994. p1.y=rect.top;
  995. if(p2.y>rect.bottom)
  996. p2.y=rect.bottom;
  997. return;
  998. }
  999. p2.y=rect.bottom;
  1000. return;
  1001. }
  1002. //横线
  1003. if(p1.y==p2.y)
  1004. {
  1005. if(p1.x<rect.left)
  1006. {
  1007. p1.x=rect.left;
  1008. if(p2.x>rect.right)
  1009. p2.x=rect.right;
  1010. return;
  1011. }
  1012. p2.x=rect.right;
  1013. return;
  1014. }
  1015. //斜线
  1016. float m=(p2.y-p1.y)*1.0/(p2.x-p1.x);
  1017. //---------------p1在矩形内,p2在矩形外---------------
  1018. if(!iRgnCode1)
  1019. {
  1020. if(m<0)
  1021. {
  1022. p2.y=p1.y+(rect.right-p1.x)*m;
  1023. if(p2.y>rect.top)
  1024. {
  1025. p2.x=rect.right;
  1026. return;
  1027. }
  1028. p2.y=rect.top;
  1029. p2.x=(p2.y-p1.y)/m+p1.x;
  1030. return;
  1031. }
  1032. p2.y=p1.y+(rect.right-p1.x)*m;
  1033. if(p2.y<rect.bottom)
  1034. {
  1035. p2.x=rect.right;
  1036. return;
  1037. }
  1038. p2.y=rect.bottom;
  1039. p2.x=(p2.y-p1.y)/m+p1.x;
  1040. return;
  1041. }
  1042. //---------------------------------------------------
  1043. //---------------p1在矩形外,p2在矩形内---------------
  1044. if(!iRgnCode2)
  1045. {
  1046. if(m<0)
  1047. {
  1048. p1.y=p2.y-m*(p2.x-rect.left);
  1049. if(p1.y<rect.bottom)
  1050. {
  1051. p1.x=rect.left;
  1052. return;
  1053. }
  1054. p1.y=rect.bottom;
  1055. p1.x=p2.x-(p2.y-p1.y)/m;
  1056. return;
  1057. }
  1058. p1.y=p2.y-m*(p2.x-rect.left);
  1059. if(p1.y>rect.top)
  1060. {
  1061. p1.x=rect.left;
  1062. return;
  1063. }
  1064. p1.y=rect.top;
  1065. p1.x=p2.x-(p2.y-p1.y)/m;
  1066. return;
  1067. }
  1068. //---------------------------------------------------
  1069. //---------------两点都在矩形外---------------
  1070. //iRgnCode1==9 || 1 || 5
  1071. if(p1.x<rect.left)
  1072. {
  1073. if(iRgnCode1==9)
  1074. {
  1075. if(m<0)
  1076. {
  1077. p1.x=p2.x=0;
  1078. p1.y=p2.y=0;
  1079. return;
  1080. }
  1081. temp=p1.x+(rect.top-p1.y)/m;
  1082. if(temp>rect.right)
  1083. {
  1084. p1.x=p2.x=0;
  1085. p1.y=p2.y=0;
  1086. return;
  1087. }
  1088. if(temp>rect.left)
  1089. {
  1090. p1.x=temp;
  1091. p1.y=rect.top;
  1092. p2.x=rect.right;
  1093. p2.y=p1.y+(p2.x-p1.x)*m;
  1094. if(p2.y<rect.bottom)
  1095. return;
  1096. p2.y=rect.bottom;
  1097. p2.x=p1.x+(p2.y-p1.y)/m;
  1098. return;
  1099. }
  1100. p1.y+=(rect.left-p1.x)*m;
  1101. if(p1.y>rect.bottom)
  1102. {
  1103. p1.x=p2.x=0;
  1104. p1.y=p2.y=0;
  1105. return;
  1106. }
  1107. p1.x=rect.left;
  1108. p2.y=rect.bottom;
  1109. p2.x=p1.x+(p2.y-p1.y)/m;
  1110. if(p2.x<rect.right)
  1111. return;
  1112. p2.x=rect.right;
  1113. p2.y=p1.y+(p2.x-p1.x)*m;
  1114. return;
  1115. }
  1116. if(iRgnCode1==1)
  1117. {
  1118. if(m<0)
  1119. {
  1120. p1.y+=(rect.left-p1.x)*m;
  1121. if(p1.y<rect.top)
  1122. {
  1123. p1.x=p2.x=0;
  1124. p1.y=p2.y=0;
  1125. return;
  1126. }
  1127. p1.x=rect.left;
  1128. p2.x=p1.x+(rect.top-p1.y)/m;
  1129. if(p2.x>rect.right)
  1130. {
  1131. p2.x=rect.right;
  1132. p2.y=p1.y+(p2.x-p1.x)*m;
  1133. return;
  1134. }
  1135. p2.y=rect.top;
  1136. return;
  1137. }
  1138. p1.y+=(rect.left-p1.x)*m;
  1139. if(p1.y>rect.bottom)
  1140. {
  1141. p1.x=p2.x=0;
  1142. p1.y=p2.y=0;
  1143. return;
  1144. }
  1145. p1.x=rect.left;
  1146. p2.x=p1.x+(rect.bottom-p1.y)/m;
  1147. if(p2.x>rect.right)
  1148. {
  1149. p2.x=rect.right;
  1150. p2.y=p1.y+(p2.x-p1.x)*m;
  1151. return;
  1152. }
  1153. p2.y=rect.bottom;
  1154. return;
  1155. }
  1156. //iRgnCode1==5
  1157. if(m>0)
  1158. {
  1159. p1.x=p2.x=0;
  1160. p1.y=p2.y=0;
  1161. return;
  1162. }
  1163. temp=p1.x+(rect.bottom-p1.y)/m;
  1164. if(temp>rect.right)
  1165. {
  1166. p1.x=p2.x=0;
  1167. p1.y=p2.y=0;
  1168. return;
  1169. }
  1170. if(temp>rect.left)
  1171. {
  1172. p1.x=temp;
  1173. p1.y=rect.bottom;
  1174. p2.x=rect.right;
  1175. p2.y=p1.y+(p2.x-p1.x)*m;
  1176. if(p2.y>rect.top)
  1177. return;
  1178. p2.y=rect.top;
  1179. p2.x=p1.x+(p2.y-p1.y)/m;
  1180. return;
  1181. }
  1182. p1.y+=(rect.left-p1.x)*m;
  1183. if(p1.y<rect.top)
  1184. {
  1185. p1.x=p2.x=0;
  1186. p1.y=p2.y=0;
  1187. return;
  1188. }
  1189. p1.x=rect.left;
  1190. p2.y=rect.top;
  1191. p2.x=p1.x+(p2.y-p1.y)/m;
  1192. if(p2.x<rect.right)
  1193. return;
  1194. p2.x=rect.right;
  1195. p2.y=p1.y+(p2.x-p1.x)*m;
  1196. return;
  1197. }
  1198. //iRgnCode1==8 || 4
  1199. if(m<0)
  1200. {
  1201. //iRgnCode1==8
  1202. if(p1.y<rect.top)
  1203. {
  1204. p1.x=p2.x=0;
  1205. p1.y=p2.y=0;
  1206. return;
  1207. }
  1208. //iRgnCode1==4
  1209. p1.x+=(rect.bottom-p1.y)/m;
  1210. if(p1.x>rect.right)
  1211. {
  1212. p1.x=p2.x=0;
  1213. p1.y=p2.y=0;
  1214. return;
  1215. }
  1216. p1.y=rect.bottom;
  1217. p2.y=p1.y+(rect.right-p1.x)*m;
  1218. if(p2.y>rect.top)
  1219. {
  1220. p2.x=rect.right;
  1221. return;
  1222. }
  1223. p2.y=rect.top;
  1224. p2.x=p1.x+(p2.y-p1.y)/m;
  1225. return;
  1226. }
  1227. if(p1.y>rect.bottom)
  1228. {
  1229. p1.x=p2.x=0;
  1230. p1.y=p2.y=0;
  1231. return;
  1232. }
  1233. p1.x+=(rect.top-p1.y)/m;
  1234. if(p1.x>rect.right)
  1235. {
  1236. p1.x=p2.x=0;
  1237. p1.y=p2.y=0;
  1238. return;
  1239. }
  1240. p1.y=rect.top;
  1241. p2.y=p1.y+(rect.right-p1.x)*m;
  1242. if(p2.y<rect.bottom)
  1243. {
  1244. p2.x=rect.right;
  1245. return;
  1246. }
  1247. p2.y=rect.bottom;
  1248. p2.x=p1.x+(p2.y-p1.y)/m;
  1249. //--------------------------------------------
  1250. }
  1251. void CDrawSysView::CutLine(RECT rect, LINE &l)
  1252. {
  1253. POINT p1,p2;
  1254. p1=l.pStart;
  1255. p2=l.pEnd;
  1256. CutLine(rect,p1,p2);
  1257. l.pStart=p1;
  1258. l.pEnd=p2;
  1259. }
  1260. void CDrawSysView::Bezier(vector<POINT> vectorPoint,CDC *pDC)
  1261. {
  1262. int n=vectorPoint.size()-1;
  1263. int i,r;
  1264. float u;
  1265. POINT *p=new POINT[n+1];
  1266. vector<POINT> vectorBPoint;
  1267. CPen pen;
  1268. for(u=0;u<=1;u+=0.0005)
  1269. {
  1270. for(i=0;i<=n;i++)
  1271. p[i]=vectorPoint[i];
  1272. for(r=1;r<=n;r++)
  1273. {
  1274. for(i=0;i<=n-r;i++)
  1275. {
  1276. p[i].x=(1-u)*p[i].x+u*p[i+1].x;
  1277. p[i].y=(1-u)*p[i].y+u*p[i+1].y;
  1278. }
  1279. }
  1280. vectorBPoint.push_back(p[0]);
  1281. }
  1282. pen.CreatePen(PS_SOLID,m_iPenWidth,m_lPenColor);
  1283. pDC->SelectObject(&pen);
  1284. n=vectorBPoint.size();
  1285. for(i=1;i<n;i++)
  1286. {
  1287. pDC->MoveTo(vectorBPoint[i-1]);
  1288. pDC->LineTo(vectorBPoint[i]);
  1289. }
  1290. }
  1291. void CDrawSysView::OnRButtonDown(UINT nFlags, CPoint point) 
  1292. {
  1293. // TODO: Add your message handler code here and/or call default
  1294. switch(m_dsDrawSort)
  1295. {
  1296. case DsLine:
  1297. case DsManyBd:
  1298. SetCursor(m_hDLCursor);
  1299. break;
  1300. case DsCircle:
  1301. SetCursor(m_hDCCursor);
  1302. break;
  1303. case DsCutLine:
  1304. SetCursor(m_hCLCursor);
  1305. break;
  1306. case DsBezier:
  1307. SetCursor(m_hDBCursor);
  1308. break;
  1309. case DsText:
  1310. SetCursor(m_hDTCursor);
  1311. break;
  1312. case DsStroke:
  1313. SetCursor(m_hDSCursor);
  1314. break;
  1315. }
  1316. CView::OnRButtonDown(nFlags, point);
  1317. }
  1318. void CDrawSysView::OnRButtonUp(UINT nFlags, CPoint point) 
  1319. {
  1320. // TODO: Add your message handler code here and/or call default
  1321. CDC *pDC=GetDC();
  1322. LINE lTemp;
  1323. switch(m_dsDrawSort)
  1324. {
  1325. case DsManyBd:
  1326. Line(m_startPoint,m_spManyBd,pDC);
  1327. lTemp.pStart=m_startPoint;
  1328. lTemp.pEnd=m_spManyBd;
  1329. m_listAllLine.push_back(lTemp);
  1330. m_bIsFirstDone=0;
  1331. case DsLine:
  1332. SetCursor(m_hDLCursor);
  1333. break;
  1334. case DsCircle:
  1335. SetCursor(m_hDCCursor);
  1336. break;
  1337. case DsCutLine:
  1338. SetCursor(m_hCLCursor);
  1339. break;
  1340. case DsBezier:
  1341. m_listBezier.push_back(m_vectorCurBezier);
  1342. m_vectorCurBezier.clear();
  1343. SetCursor(m_hDBCursor);
  1344. break;
  1345. case DsText:
  1346. SetCursor(m_hDTCursor);
  1347. break;
  1348. case DsStroke:
  1349. SetCursor(m_hDSCursor);
  1350. break;
  1351. }
  1352. CView::OnRButtonUp(nFlags, point);
  1353. }
  1354. void CDrawSysView::OnRButtonDblClk(UINT nFlags, CPoint point) 
  1355. {
  1356. // TODO: Add your message handler code here and/or call default
  1357. switch(m_dsDrawSort)
  1358. {
  1359. case DsLine:
  1360. case DsManyBd:
  1361. SetCursor(m_hDLCursor);
  1362. break;
  1363. case DsCircle:
  1364. SetCursor(m_hDCCursor);
  1365. break;
  1366. case DsCutLine:
  1367. SetCursor(m_hCLCursor);
  1368. break;
  1369. case DsBezier:
  1370. SetCursor(m_hDBCursor);
  1371. break;
  1372. case DsText:
  1373. SetCursor(m_hDTCursor);
  1374. break;
  1375. case DsStroke:
  1376. SetCursor(m_hDSCursor);
  1377. break;
  1378. }
  1379. CView::OnRButtonDblClk(nFlags, point);
  1380. }
  1381. void CDrawSysView::OnBezier() 
  1382. {
  1383. // TODO: Add your command handler code here
  1384. if(m_dsDrawSort!=DsBezier)
  1385. {
  1386. CDC *pDC=GetDC();
  1387. TEXT tTemp;
  1388. int nSize;
  1389. switch(m_dsDrawSort)
  1390. {
  1391. case DsManyBd:
  1392. if(m_bIsFirstDone)
  1393. {
  1394. LINE lTemp;
  1395. Line(m_startPoint,m_spManyBd,pDC);
  1396. lTemp.pStart=m_startPoint;
  1397. lTemp.pEnd=m_spManyBd;
  1398. m_listAllLine.push_back(lTemp);
  1399. }
  1400. break;
  1401. case DsText:
  1402. m_edtTextIn.GetWindowText(tTemp.str);
  1403. if(tTemp.str!="")
  1404. {
  1405. m_startPoint.y-=7;
  1406. tTemp.p=m_startPoint;
  1407. m_vectorText.push_back(tTemp);
  1408. }
  1409. m_edtTextIn.ShowWindow(0);
  1410. m_edtTextIn.SetWindowText("");
  1411. nSize=m_vectorText.size();
  1412. if(nSize>0)
  1413. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  1414. break;
  1415. }
  1416. m_dsDrawSort=DsBezier;
  1417. }
  1418. else
  1419. {
  1420. m_listBezier.push_back(m_vectorCurBezier);
  1421. m_vectorCurBezier.clear();
  1422. m_dsDrawSort=DsNull;
  1423. }
  1424. m_bIsFirstDone=0;
  1425. m_bIsMouseDown=0;
  1426. }
  1427. void CDrawSysView::OnUpdateBezier(CCmdUI* pCmdUI) 
  1428. {
  1429. // TODO: Add your command update UI handler code here
  1430. pCmdUI->SetCheck(m_dsDrawSort==DsBezier);
  1431. }
  1432. void CDrawSysView::OnText() 
  1433. {
  1434. // TODO: Add your command handler code here
  1435. CDC *pDC=GetDC();
  1436. if(m_dsDrawSort!=DsText)
  1437. {
  1438. switch(m_dsDrawSort)
  1439. {
  1440. case DsManyBd:
  1441. if(m_bIsFirstDone)
  1442. {
  1443. CDC *pDC=GetDC();
  1444. LINE lTemp;
  1445. Line(m_startPoint,m_spManyBd,pDC);
  1446. lTemp.pStart=m_startPoint;
  1447. lTemp.pEnd=m_spManyBd;
  1448. m_listAllLine.push_back(lTemp);
  1449. }
  1450. break;
  1451. case DsBezier:
  1452. m_listBezier.push_back(m_vectorCurBezier);
  1453. m_vectorCurBezier.clear();
  1454. break;
  1455. }
  1456. m_dsDrawSort=DsText;
  1457. }
  1458. else
  1459. {
  1460. TEXT tTemp;
  1461. int nSize;
  1462. m_edtTextIn.GetWindowText(tTemp.str);
  1463. if(tTemp.str!="")
  1464. {
  1465. m_startPoint.y-=7;
  1466. tTemp.p=m_startPoint;
  1467. m_vectorText.push_back(tTemp);
  1468. }
  1469. m_edtTextIn.ShowWindow(0);
  1470. m_edtTextIn.SetWindowText("");
  1471. nSize=m_vectorText.size();
  1472. if(nSize>0)
  1473. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  1474. m_dsDrawSort=DsNull;
  1475. }
  1476. m_bIsFirstDone=0;
  1477. m_bIsMouseDown=0;
  1478. }
  1479. void CDrawSysView::OnClear() 
  1480. {
  1481. // TODO: Add your command handler code here
  1482. CRect rect;
  1483. m_listAllLine.clear();
  1484. m_listAllCircle.clear();
  1485. m_listBezier.clear();
  1486. m_vectorText.clear();
  1487. GetClientRect(&rect);
  1488. InvalidateRect(&rect,1);
  1489. }
  1490. void CDrawSysView::OnStroke() 
  1491. {
  1492. // TODO: Add your command handler code here
  1493. if(m_dsDrawSort!=DsStroke)
  1494. {
  1495. TEXT tTemp;
  1496. int nSize;
  1497. CDC *pDC=GetDC();
  1498. switch(m_dsDrawSort)
  1499. {
  1500. case DsManyBd:
  1501. if(m_bIsFirstDone)
  1502. {
  1503. LINE lTemp;
  1504. Line(m_startPoint,m_spManyBd,pDC);
  1505. lTemp.pStart=m_startPoint;
  1506. lTemp.pEnd=m_spManyBd;
  1507. m_listAllLine.push_back(lTemp);
  1508. }
  1509. break;
  1510. case DsBezier:
  1511. m_listBezier.push_back(m_vectorCurBezier);
  1512. m_vectorCurBezier.clear();
  1513. break;
  1514. case DsText:
  1515. m_edtTextIn.GetWindowText(tTemp.str);
  1516. if(tTemp.str!="")
  1517. {
  1518. m_startPoint.y-=7;
  1519. tTemp.p=m_startPoint;
  1520. m_vectorText.push_back(tTemp);
  1521. }
  1522. m_edtTextIn.ShowWindow(0);
  1523. m_edtTextIn.SetWindowText("");
  1524. nSize=m_vectorText.size();
  1525. if(nSize>0)
  1526. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  1527. break;
  1528. }
  1529. m_dsDrawSort=DsStroke;
  1530. }
  1531. else
  1532. m_dsDrawSort=DsNull;
  1533. m_bIsFirstDone=0;
  1534. m_bIsMouseDown=0;
  1535. }
  1536. void CDrawSysView::OnUpdateStroke(CCmdUI* pCmdUI) 
  1537. {
  1538. // TODO: Add your command update UI handler code here
  1539. pCmdUI->SetCheck(m_dsDrawSort==DsStroke);
  1540. }
  1541. void CDrawSysView::OnUpdateText(CCmdUI* pCmdUI) 
  1542. {
  1543. // TODO: Add your command update UI handler code here
  1544. pCmdUI->SetCheck(m_dsDrawSort==DsText);
  1545. }
  1546. void CDrawSysView::OnTest() 
  1547. {
  1548. // TODO: Add your command handler code here
  1549. CDC *pDC=GetDC();
  1550. int i;
  1551. long temp=m_lPenColor;
  1552. CBrush brush;
  1553. CRect rect;
  1554. int nScreenWidth;
  1555. int nScreenHight;
  1556. TEXT tTemp;
  1557. int nSize;
  1558. switch(m_dsDrawSort)
  1559. {
  1560. case DsManyBd:
  1561. if(m_bIsFirstDone)
  1562. {
  1563. CDC *pDC=GetDC();
  1564. LINE lTemp;
  1565. Line(m_startPoint,m_spManyBd,pDC);
  1566. lTemp.pStart=m_startPoint;
  1567. lTemp.pEnd=m_spManyBd;
  1568. m_listAllLine.push_back(lTemp);
  1569. }
  1570. break;
  1571. case DsBezier:
  1572. m_listBezier.push_back(m_vectorCurBezier);
  1573. m_vectorCurBezier.clear();
  1574. break;
  1575. case DsText:
  1576. m_edtTextIn.GetWindowText(tTemp.str);
  1577. if(tTemp.str!="")
  1578. {
  1579. m_startPoint.y-=7;
  1580. tTemp.p=m_startPoint;
  1581. m_vectorText.push_back(tTemp);
  1582. }
  1583. m_edtTextIn.ShowWindow(0);
  1584. m_edtTextIn.SetWindowText("");
  1585. nSize=m_vectorText.size();
  1586. if(nSize>0)
  1587. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  1588. break;
  1589. }
  1590. m_dsDrawSort=DsNull;
  1591. brush.CreateSolidBrush(RGB(255,255,255));
  1592. GetClientRect(&rect);
  1593. pDC->FillRect(&rect,&brush);
  1594. brush.DeleteObject();
  1595. nScreenWidth=rect.right;
  1596. nScreenHight=rect.bottom;
  1597. // for(i=5;i<=505;i++)
  1598. // {
  1599. // m_lPenColor=RGB(255,0,i*255/550);
  1600. // Line(255,320,i,70,pDC);
  1601. // }
  1602. // for(i=5;i<=505;i++)
  1603. // {
  1604. // m_lPenColor=RGB(i*255/550,255,0);
  1605. // Line(255,320,i,570,pDC);
  1606. // }
  1607. // for(i=70;i<570;i++)
  1608. // {
  1609. // m_lPenColor=RGB(0,255,i*255/650);
  1610. // Line(255,320,5,i,pDC);
  1611. // }
  1612. // for(i=70;i<570;i++)
  1613. // {
  1614. // m_lPenColor=RGB(0,i*255/650,255);
  1615. // Line(255,320,505,i,pDC);
  1616. // }
  1617. //
  1618. // for(int r=0;r<=250;r++)
  1619. // {
  1620. // m_lPenColor=RGB(0,255-r,255+r);
  1621. // Circle(760,320,r,pDC);
  1622. // }
  1623. for(i=5;i<=nScreenWidth/2+5;i++)
  1624. {
  1625. m_lPenColor=RGB(255,0,i*255/550);
  1626. Line(nScreenWidth/4+5,nScreenHight/2,i,nScreenHight/2-nScreenWidth/4,pDC);
  1627. }
  1628. for(i=5;i<=nScreenWidth/2+5;i++)
  1629. {
  1630. m_lPenColor=RGB(i*255/550,255,0);
  1631. Line(nScreenWidth/4+5,nScreenHight/2,i,nScreenHight/2+nScreenWidth/4,pDC);
  1632. }
  1633. for(i=nScreenHight/2-nScreenWidth/4;i<nScreenHight/2+nScreenWidth/4;i++)
  1634. {
  1635. m_lPenColor=RGB(0,255,i*255/650);
  1636. Line(nScreenWidth/4+5,nScreenHight/2,5,i,pDC);
  1637. }
  1638. for(i=nScreenHight/2-nScreenWidth/4;i<nScreenHight/2+nScreenWidth/4;i++)
  1639. {
  1640. m_lPenColor=RGB(0,i*255/650,255);
  1641. Line(nScreenWidth/4+5,nScreenHight/2,nScreenWidth/2+5,i,pDC);
  1642. }
  1643. for(int r=0;r<=nScreenWidth/4-5;r++)
  1644. {
  1645. m_lPenColor=RGB(0,255-r,255+r);
  1646. Circle(nScreenWidth*0.75+5,nScreenHight/2,r,pDC);
  1647. }
  1648. ReleaseDC(pDC);
  1649. }
  1650. void CDrawSysView::Line(POINT p1, POINT p2, CDC *pDC)
  1651. {
  1652. if(m_dlsDrawLSort==DlsDDA)
  1653. Line_DDA(p1,p2,pDC);
  1654. else
  1655. Line_Bresenham(p1,p2,pDC);
  1656. }
  1657. void CDrawSysView::Line(long p1x, long p1y, long p2x, long p2y, CDC *pDC)
  1658. {
  1659. POINT p1,p2;
  1660. p1.x=p1x;
  1661. p1.y=p1y;
  1662. p2.x=p2x;
  1663. p2.y=p2y;
  1664. Line(p1,p2,pDC);
  1665. }
  1666. void CDrawSysView::Circle(long pcx, long pcy, int r, CDC *pDC)
  1667. {
  1668. POINT pc;
  1669. pc.x=pcx;
  1670. pc.y=pcy;
  1671. Circle(pc,r,pDC);
  1672. }
  1673. void CDrawSysView::OnRandline() 
  1674. {
  1675. // TODO: Add your command handler code here
  1676. CDC *pDC=GetDC();
  1677. CRect rect;
  1678. int i;
  1679. LINE lTemp;
  1680. TEXT tTemp;
  1681. int nSize;
  1682. switch(m_dsDrawSort)
  1683. {
  1684. case DsManyBd:
  1685. if(m_bIsFirstDone)
  1686. {
  1687. CDC *pDC=GetDC();
  1688. LINE lTemp;
  1689. Line(m_startPoint,m_spManyBd,pDC);
  1690. lTemp.pStart=m_startPoint;
  1691. lTemp.pEnd=m_spManyBd;
  1692. m_listAllLine.push_back(lTemp);
  1693. }
  1694. break;
  1695. case DsBezier:
  1696. m_listBezier.push_back(m_vectorCurBezier);
  1697. m_vectorCurBezier.clear();
  1698. break;
  1699. case DsText:
  1700. m_edtTextIn.GetWindowText(tTemp.str);
  1701. if(tTemp.str!="")
  1702. {
  1703. m_startPoint.y-=7;
  1704. tTemp.p=m_startPoint;
  1705. m_vectorText.push_back(tTemp);
  1706. }
  1707. m_edtTextIn.ShowWindow(0);
  1708. m_edtTextIn.SetWindowText("");
  1709. nSize=m_vectorText.size();
  1710. if(nSize>0)
  1711. pDC->TextOut(m_vectorText[nSize-1].p.x,m_vectorText[nSize-1].p.y,m_vectorText[nSize-1].str);
  1712. break;
  1713. }
  1714. m_dsDrawSort=DsNull;
  1715. srand(time(0));
  1716. GetClientRect(&rect);
  1717. for(i=0;i<500;i++)
  1718. {
  1719. lTemp.pStart.x=rect.left+(rect.right-rect.left)*rand()*1.0/RAND_MAX;
  1720. lTemp.pStart.y=rect.top+(rect.bottom-rect.top)*rand()*1.0/RAND_MAX;
  1721. lTemp.pEnd.x=rect.left+(rect.right-rect.left)*rand()*1.0/RAND_MAX;
  1722. lTemp.pEnd.y=rect.top+(rect.bottom-rect.top)*rand()*1.0/RAND_MAX;
  1723. m_listAllLine.push_back(lTemp);
  1724. }
  1725. InvalidateRect(rect,1);
  1726. ReleaseDC(pDC);
  1727. }
  1728. void CDrawSysView::Draw3DText(CString str,CRect rectText,int nHeight,int nWidth,int nWeight,BYTE bItalic,LPCTSTR lpszFacename,CDC *pDC)
  1729. {
  1730. CFont fontLogo,*oldFont;
  1731. COLORREF oldColor;
  1732. fontLogo.CreateFont(nHeight,nWidth,0,0,nWeight,bItalic,FALSE,FALSE,
  1733. DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
  1734. FIXED_PITCH | FF_ROMAN,lpszFacename);
  1735. pDC->SetBkMode(TRANSPARENT);
  1736. oldFont=pDC->SelectObject(&fontLogo);
  1737. oldColor=pDC->SetTextColor(RGB(0,0,0));
  1738. pDC->DrawText(str,rectText+CPoint(1,1),DT_SINGLELINE | DT_LEFT | DT_VCENTER|DT_CENTER);
  1739. pDC->SetTextColor(::GetSysColor(COLOR_BTNFACE));
  1740. pDC->DrawText(str,rectText,DT_SINGLELINE | DT_LEFT | DT_VCENTER|DT_CENTER);
  1741. pDC->SetTextColor(oldColor);
  1742. pDC->SelectObject(oldFont);
  1743. }
  1744. void CDrawSysView::OnRefresh() 
  1745. {
  1746. // TODO: Add your command handler code here
  1747. CRect rect;
  1748. GetClientRect(&rect);
  1749. InvalidateRect(&rect,1);
  1750. }
  1751. void CDrawSysView::Circle(CIRCLE c, CDC *pDC)
  1752. {
  1753. POINT pc=c.pMid;
  1754. int r=c.r;
  1755. Circle(pc,r,pDC);
  1756. }
  1757. void CDrawSysView::Line(LINE l,CDC *pDC)
  1758. {
  1759. POINT p1=l.pStart;
  1760. POINT p2=l.pEnd;
  1761. Line(p1,p2,pDC);
  1762. }