DrawVw.cpp
上传用户:seaboy_04
上传日期:2013-02-24
资源大小:284k
文件大小:27k
源码类别:

其他行业

开发平台:

Visual C++

  1. // DrawVw.cpp : implementation of the CDrawView class
  2. //
  3. #include "stdafx.h"
  4. #include <afxpriv.h>
  5. #include "DrawCli.h"
  6. #include "DrawDoc.h"
  7. #include "CntrItem.h"
  8. #include "DrawVw.h"
  9. #include "resource.h"
  10. #include "DrawObj.h"
  11. #include "drawtool.h"
  12. #include "CntrItem.h"
  13. #include "mainfrm.h"
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. extern Data_Bus  Demo_Data; 
  20. // private clipboard format (list of Draw objects)
  21. CLIPFORMAT CDrawView::m_cfDraw = (CLIPFORMAT)
  22. #ifdef _MAC
  23. ::RegisterClipboardFormat(_T("DCLI"));
  24. #else
  25. ::RegisterClipboardFormat(_T("MFC Draw Sample"));
  26. #endif
  27. CLIPFORMAT CDrawView::m_cfObjectDescriptor = NULL;
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CDrawView
  30. IMPLEMENT_DYNCREATE(CDrawView, CScrollView)
  31. BEGIN_MESSAGE_MAP(CDrawView, CScrollView)
  32. ON_WM_CONTEXTMENU()
  33. //{{AFX_MSG_MAP(CDrawView)
  34. ON_WM_DESTROY()
  35. ON_WM_SETFOCUS()
  36. ON_WM_SIZE()
  37. ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
  38. ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
  39. ON_COMMAND(ID_DRAW_SELECT, OnDrawSelect)
  40. ON_COMMAND(ID_DRAW_RECT, OnDrawRect)
  41. ON_UPDATE_COMMAND_UI(ID_DRAW_SELECT, OnUpdateDrawSelect)
  42. ON_UPDATE_COMMAND_UI(ID_DRAW_RECT, OnUpdateDrawRect)
  43. ON_WM_LBUTTONDOWN()
  44. ON_WM_LBUTTONUP()
  45. ON_WM_MOUSEMOVE()
  46. ON_WM_LBUTTONDBLCLK()
  47. ON_COMMAND(ID_DRAW_CIRCLE, OnDrawCircle)
  48. ON_COMMAND(ID_DRAW_ELLIPSE, OnDrawEllipse)
  49. ON_COMMAND(ID_DRAW_LINE, OnDrawLine)
  50. ON_COMMAND(ID_DRAW_ROUNDRECT, OnDrawRoundrect)
  51. ON_COMMAND(ID_EDIT_PROPERTIES, OnEditProperties)
  52. ON_COMMAND(ID_OBJECT_MOVEBACK, OnObjectMoveback)
  53. ON_COMMAND(ID_OBJECT_MOVEFORWARD, OnObjectMoveforward)
  54. ON_COMMAND(ID_OBJECT_MOVETOBACK, OnObjectMovetoback)
  55. ON_COMMAND(ID_OBJECT_MOVETOFRONT, OnObjectMovetofront)
  56. ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVEBACK, OnUpdateObjectMoveback)
  57. ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVEFORWARD, OnUpdateObjectMoveforward)
  58. ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVETOBACK, OnUpdateObjectMovetoback)
  59. ON_UPDATE_COMMAND_UI(ID_OBJECT_MOVETOFRONT, OnUpdateObjectMovetofront)
  60. ON_COMMAND(ID_OBJECT_FILLCOLOR, OnObjectFillcolor)
  61. ON_COMMAND(ID_OBJECT_LINECOLOR, OnObjectLinecolor)
  62. ON_UPDATE_COMMAND_UI(ID_OBJECT_FILLCOLOR, OnUpdateObjectFillcolor)
  63. ON_UPDATE_COMMAND_UI(ID_OBJECT_LINECOLOR, OnUpdateObjectLinecolor)
  64. ON_COMMAND(ID_VIEW_GRID, OnViewGrid)
  65. ON_UPDATE_COMMAND_UI(ID_VIEW_GRID, OnUpdateViewGrid)
  66. ON_COMMAND(ID_VIEW_SHOWOBJECTS, OnViewShowobjects)
  67. ON_UPDATE_COMMAND_UI(ID_VIEW_SHOWOBJECTS, OnUpdateViewShowobjects)
  68. ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  69. ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  70. ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  71. ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  72. ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
  73. ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR, OnUpdateEditClear)
  74. ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
  75. ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnUpdateEditCut)
  76. ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
  77. ON_UPDATE_COMMAND_UI(ID_EDIT_SELECT_ALL, OnUpdateEditSelectAll)
  78. ON_WM_CREATE()
  79. //}}AFX_MSG_MAP
  80. // Standard printing commands
  81. ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
  82. ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
  83. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  84. ON_MESSAGE(WM_DEMO_DATA,DemoData)
  85. END_MESSAGE_MAP()
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CDrawView construction/destruction
  88. CDrawView::CDrawView()
  89. {
  90. // m_pSelection = NULL;
  91. m_bGrid = FALSE; 
  92. m_gridColor = RGB(0, 0, 128);
  93. m_bActive = FALSE;
  94. }
  95. CDrawView::~CDrawView()
  96. {
  97. }
  98. BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
  99. {
  100. // TODO: Modify the Window class or styles here by modifying
  101. //  the CREATESTRUCT cs
  102. return CScrollView::PreCreateWindow(cs);
  103. }
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CDrawView drawing
  106. void CDrawView::OnDraw(CDC* pDC)
  107. {
  108. CDrawDoc* pDoc = GetDocument();
  109. ASSERT_VALID(pDoc);
  110. CDC dc;
  111. CDC* pDrawDC = pDC;
  112. CBitmap bitmap;
  113. CBitmap* pOldBitmap;
  114. // only paint the rect that needs repainting
  115. CRect client;
  116. pDC->GetClipBox(client);
  117. CRect rect = client;
  118. DocToClient(rect);
  119. if (!pDC->IsPrinting())
  120. {
  121. // draw to offscreen bitmap for fast looking repaints
  122. if (dc.CreateCompatibleDC(pDC))
  123. {
  124. if (bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()))
  125. {
  126. OnPrepareDC(&dc, NULL);
  127. pDrawDC = &dc;
  128. // offset origin more because bitmap is just piece of the whole drawing
  129. dc.OffsetViewportOrg(-rect.left, -rect.top);
  130. pOldBitmap = dc.SelectObject(&bitmap);
  131. dc.SetBrushOrg(rect.left % 8, rect.top % 8);
  132. // might as well clip to the same rectangle
  133. dc.IntersectClipRect(client);
  134. }
  135. }
  136. }
  137. // paint background
  138. CBrush brush;
  139. if (!brush.CreateSolidBrush(pDoc->GetPaperColor()))
  140. return;
  141. brush.UnrealizeObject();
  142. pDrawDC->FillRect(client, &brush);
  143. if (!pDC->IsPrinting() && m_bGrid)
  144. DrawGrid(pDrawDC);
  145. pDoc->Draw(pDrawDC, this);
  146. if (pDrawDC != pDC)
  147. {
  148. pDC->SetViewportOrg(0, 0);
  149. pDC->SetWindowOrg(0,0);
  150. pDC->SetMapMode(MM_TEXT);
  151. dc.SetViewportOrg(0, 0);
  152. dc.SetWindowOrg(0,0);
  153. dc.SetMapMode(MM_TEXT);
  154. pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
  155. &dc, 0, 0, SRCCOPY);
  156. dc.SelectObject(pOldBitmap);
  157. }
  158. }
  159. void CDrawView::OnInitialUpdate()
  160. {
  161. // CScrollView::OnInitialUpdate();
  162.    CSize size = GetDocument()->GetSize();
  163. CClientDC dc(NULL);
  164. size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100);
  165. size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100);
  166. SetScrollSizes(MM_TEXT, size);
  167. }
  168. /////////////////////////////////////////////////////////////////////////////
  169. // CDrawView printing
  170. BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
  171. {
  172. // default preparation
  173. return DoPreparePrinting(pInfo);
  174. }
  175. void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  176. {
  177. // TODO: add extra initialization before printing
  178. }
  179. void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  180. {
  181. // TODO: add cleanup after printing
  182. }
  183. void CDrawView::OnDestroy()
  184. {
  185. // Deactivate the item on destruction; this is important
  186. // when a splitter view is being used.
  187.    CScrollView::OnDestroy();
  188.    COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  189.    if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
  190.    {
  191.       pActiveItem->Deactivate();
  192.       ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  193.    }
  194. }
  195. /////////////////////////////////////////////////////////////////////////////
  196. // OLE Client support and commands
  197. BOOL CDrawView::IsSelected(const CObject* pDocItem) const
  198. {
  199. /*
  200. // The implementation below is adequate if your selection consists of
  201. //  only CDrawItem objects.  To handle different selection
  202. //  mechanisms, the implementation here should be replaced.
  203. // TODO: implement this function that tests for a selected OLE client item
  204. return pDocItem == m_pSelection;
  205. */
  206. CDrawObj* pDrawObj = (CDrawObj*)pDocItem;
  207. if (pDocItem->IsKindOf(RUNTIME_CLASS(CDrawItem)))
  208. pDrawObj = ((CDrawItem*)pDocItem)->m_pDrawObj;
  209. return m_selection.Find(pDrawObj) != NULL;
  210. }
  211. void CDrawView::OnInsertObject()
  212. {
  213. // Invoke the standard Insert Object dialog box to obtain information
  214. //  for new CDrawItem object.
  215. COleInsertDialog dlg;
  216. if (dlg.DoModal() != IDOK)
  217. return;
  218. BeginWaitCursor();
  219. // First create the C++ object
  220. CDrawOleObj* pObj = new CDrawOleObj(GetInitialPosition());
  221. ASSERT_VALID(pObj);
  222. CDrawItem* pItem = new CDrawItem(GetDocument(), pObj);
  223. ASSERT_VALID(pItem);
  224. pObj->m_pClientItem = pItem;
  225. // Now create the OLE object/item
  226. TRY
  227. {
  228. if (!dlg.CreateItem(pObj->m_pClientItem))
  229. AfxThrowMemoryException();
  230. // add the object to the document
  231. GetDocument()->Add(pObj);
  232. // try to get initial presentation data
  233. pItem->UpdateLink();
  234. pItem->UpdateExtent();
  235. // if insert new object -- initially show the object
  236. if (dlg.GetSelectionType() == COleInsertDialog::createNewItem)
  237. pItem->DoVerb(OLEIVERB_SHOW, this);
  238. }
  239. CATCH_ALL(e)
  240. {
  241. // clean up item
  242. pItem->Delete();
  243. pObj->m_pClientItem = NULL;
  244. GetDocument()->Remove(pObj);
  245. pObj->Remove();
  246. AfxMessageBox(IDP_FAILED_TO_CREATE);
  247. }
  248. END_CATCH_ALL
  249. EndWaitCursor();
  250. }
  251. // The following command handler provides the standard keyboard
  252. //  user interface to cancel an in-place editing session.  Here,
  253. //  the container (not the server) causes the deactivation.
  254. void CDrawView::OnCancelEditCntr()
  255. {
  256. // Close any in-place active item on this view.
  257. COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  258. if (pActiveItem != NULL)
  259. {
  260. pActiveItem->Close();
  261. }
  262. ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
  263. }
  264. // Special handling of OnSetFocus and OnSize are required for a container
  265. //  when an object is being edited in-place.
  266. void CDrawView::OnSetFocus(CWnd* pOldWnd)
  267. {
  268. COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  269. if (pActiveItem != NULL &&
  270. pActiveItem->GetItemState() == COleClientItem::activeUIState)
  271. {
  272. // need to set focus to this item if it is in the same view
  273. CWnd* pWnd = pActiveItem->GetInPlaceWindow();
  274. if (pWnd != NULL)
  275. {
  276. pWnd->SetFocus();   // don't call the base class
  277. return;
  278. }
  279. }
  280. CScrollView::OnSetFocus(pOldWnd);
  281. }
  282. void CDrawView::OnSize(UINT nType, int cx, int cy)
  283. {
  284. CScrollView::OnSize(nType, cx, cy);
  285. COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
  286. if (pActiveItem != NULL)
  287. pActiveItem->SetItemRects();
  288. }
  289. /////////////////////////////////////////////////////////////////////////////
  290. // CDrawView diagnostics
  291. #ifdef _DEBUG
  292. void CDrawView::AssertValid() const
  293. {
  294. CScrollView::AssertValid();
  295. }
  296. void CDrawView::Dump(CDumpContext& dc) const
  297. {
  298. CScrollView::Dump(dc);
  299. }
  300. CDrawDoc* CDrawView::GetDocument() // non-debug version is inline
  301. {
  302. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
  303. return (CDrawDoc*)m_pDocument;
  304. }
  305. #endif //_DEBUG
  306. /////////////////////////////////////////////////////////////////////////////
  307. // CDrawView message handlers
  308. void CDrawView::OnContextMenu(CWnd*, CPoint point)
  309. {
  310. // CG: This block was added by the Pop-up Menu component { if (point.x == -1 && point.y == -1){ //keystroke invocation CRect rect; GetClientRect(rect); ClientToScreen(rect); point = rect.TopLeft(); point.Offset(5, 5); } CMenu menu; VERIFY(menu.LoadMenu(ID_POPUP_MENU)); CMenu* pPopup = menu.GetSubMenu(0); ASSERT(pPopup != NULL); CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD) pWndPopupOwner = pWndPopupOwner->GetParent(); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, pWndPopupOwner); }
  311. }
  312. void CDrawView::InvalObj(CDrawObj *pObj)
  313. {
  314. CRect rect = pObj->m_position;
  315. DocToClient(rect);
  316. if (m_bActive && IsSelected(pObj))
  317. {
  318. rect.left -= 4;
  319. rect.top -= 5;
  320. rect.right += 5;
  321. rect.bottom += 4;
  322. }
  323. rect.InflateRect(1, 1); // handles CDrawOleObj objects
  324. InvalidateRect(rect, FALSE);
  325. }
  326. void CDrawView::ClientToDoc(CPoint& point)
  327. {
  328. CClientDC dc(this);
  329. OnPrepareDC(&dc, NULL);
  330. dc.DPtoLP(&point);
  331. }
  332. void CDrawView::ClientToDoc(CRect& rect)
  333. {
  334. CClientDC dc(this);
  335. OnPrepareDC(&dc, NULL);
  336. dc.DPtoLP(rect);
  337. ASSERT(rect.left <= rect.right);
  338. ASSERT(rect.bottom <= rect.top);
  339. }
  340. void CDrawView::DocToClient(CPoint& point)
  341. {
  342. CClientDC dc(this);
  343. OnPrepareDC(&dc, NULL);
  344. dc.LPtoDP(&point);
  345. }
  346. void CDrawView::DocToClient(CRect& rect)
  347. {
  348. CClientDC dc(this);
  349. OnPrepareDC(&dc, NULL);
  350. dc.LPtoDP(rect);
  351. rect.NormalizeRect();
  352. }
  353. void CDrawView::Remove(CDrawObj* pObj)
  354. {
  355. POSITION pos = m_selection.Find(pObj);
  356. if (pos != NULL)
  357. m_selection.RemoveAt(pos);
  358. }
  359. void CDrawView::Select(CDrawObj* pObj,BOOL bAdd)
  360. {
  361. if (!bAdd)
  362. {
  363. OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  364. m_selection.RemoveAll();
  365. }
  366. if (pObj == NULL || IsSelected(pObj))
  367. return;
  368. m_selection.AddTail(pObj);
  369. InvalObj(pObj);
  370. }
  371. void CDrawView::CloneSelection()
  372. {
  373. POSITION pos = m_selection.GetHeadPosition();
  374. while (pos != NULL)
  375. {
  376. CDrawObj* pObj = m_selection.GetNext(pos);
  377. pObj->Clone(pObj->m_pDocument);
  378. // copies object and adds it to the document
  379. }
  380. }
  381. void CDrawView::Deselect(CDrawObj* pObj)
  382. {
  383. POSITION pos = m_selection.Find(pObj);
  384. if (pos != NULL)
  385. {
  386. InvalObj(pObj);
  387. m_selection.RemoveAt(pos);
  388. }
  389. }
  390. void CDrawView::SelectWithinRect(CRect rect,BOOL bAdd)
  391. {
  392. if (!bAdd)
  393. Select(NULL);
  394. ClientToDoc(rect);
  395. CDrawObjList* pObList = GetDocument()->GetObjects();
  396. POSITION posObj = pObList->GetHeadPosition();
  397. while (posObj != NULL)
  398. {
  399. CDrawObj* pObj = pObList->GetNext(posObj);
  400. if (pObj->Intersects(rect))
  401. Select(pObj, TRUE);
  402. }
  403. }
  404. void CDrawView::OnDrawSelect() 
  405. {
  406. CDrawTool::c_drawShape = selection;
  407. }
  408. void CDrawView::OnDrawRect() 
  409. {
  410. CDrawTool::c_drawShape = rect;
  411. }
  412. void CDrawView::OnUpdateDrawSelect(CCmdUI* pCmdUI) 
  413. {
  414. pCmdUI->SetRadio(CDrawTool::c_drawShape == selection);
  415. }
  416. void CDrawView::OnUpdateDrawRect(CCmdUI* pCmdUI) 
  417. {
  418. pCmdUI->SetRadio(CDrawTool::c_drawShape == rect);
  419. }
  420. void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
  421. {
  422. if (!m_bActive)
  423. return;
  424. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  425. if (pTool != NULL)
  426. pTool->OnLButtonDown(this, nFlags, point);
  427. }
  428. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
  429. {
  430. if (!m_bActive)
  431. return;
  432. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  433. if (pTool != NULL)
  434. pTool->OnLButtonUp(this, nFlags, point);
  435. }
  436. void CDrawView::OnMouseMove(UINT nFlags, CPoint point) 
  437. {
  438. if (!m_bActive)
  439. return;
  440. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  441. if (pTool != NULL)
  442. pTool->OnMouseMove(this, nFlags, point);
  443. //  m_wndStatusBar.SetPaneText(1,LPCTSTR("abcdefgh"),TRUE);
  444. }
  445. void CDrawView::OnLButtonDblClk(UINT nFlags, CPoint point) 
  446. {
  447. if (!m_bActive)
  448. return;
  449. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  450. if (pTool != NULL)
  451. pTool->OnLButtonDblClk(this, nFlags, point);
  452. }
  453. void CDrawView::DrawGrid(CDC* pDC)
  454. {
  455.     CDrawDoc* pDoc = GetDocument();
  456. COLORREF oldBkColor = pDC->SetBkColor(pDoc->GetPaperColor());
  457. CRect rect;
  458. rect.left = -pDoc->GetSize().cx / 2;
  459. rect.top = -pDoc->GetSize().cy / 2;
  460. rect.right = rect.left + pDoc->GetSize().cx;
  461. rect.bottom = rect.top + pDoc->GetSize().cy;
  462. // Center lines
  463. CPen penDash;
  464. penDash.CreatePen(PS_DASH, 1, m_gridColor);
  465. CPen* pOldPen = pDC->SelectObject(&penDash);
  466. pDC->MoveTo(0, rect.top);
  467. pDC->LineTo(0, rect.bottom);
  468. pDC->MoveTo(rect.left, 0);
  469. pDC->LineTo(rect.right, 0);
  470. // Major unit lines
  471. CPen penDot;
  472. penDot.CreatePen(PS_DOT, 1, m_gridColor);
  473. pDC->SelectObject(&penDot);
  474. //for (int x = rect.left / 100 * 100; x < rect.right; x += 100)
  475. for (int x = rect.left / 50 * 50; x < rect.right; x += 50)
  476. {
  477. if (x != 0)
  478. {
  479. pDC->MoveTo(x, rect.top);
  480. pDC->LineTo(x, rect.bottom);
  481. }
  482. }
  483. //for (int y = rect.top / 100 * 100; y < rect.bottom; y += 100)
  484. for (int y = rect.top / 50 * 50; y < rect.bottom; y += 50)
  485. {
  486. if (y != 0)
  487. {
  488. pDC->MoveTo(rect.left, y);
  489. pDC->LineTo(rect.right, y);
  490. }
  491. }
  492. // Outlines
  493. CPen penSolid;
  494. penSolid.CreatePen(PS_SOLID, 1, m_gridColor);
  495. pDC->SelectObject(&penSolid);
  496. pDC->MoveTo(rect.left, rect.top);
  497. pDC->LineTo(rect.right, rect.top);
  498. pDC->LineTo(rect.right, rect.bottom);
  499. pDC->LineTo(rect.left, rect.bottom);
  500. pDC->LineTo(rect.left, rect.top);
  501. pDC->SelectObject(pOldPen);
  502. pDC->SetBkColor(oldBkColor);
  503. }
  504. void CDrawView::SetPageSize(CSize size)
  505. {
  506.     CClientDC dc(NULL);
  507. size.cx = MulDiv(size.cx, dc.GetDeviceCaps(LOGPIXELSX), 100);
  508. size.cy = MulDiv(size.cy, dc.GetDeviceCaps(LOGPIXELSY), 100);
  509. SetScrollSizes(MM_TEXT, size);
  510. GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_WINDOW, NULL);
  511. }
  512. void CDrawView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) 
  513. {
  514.   CScrollView::OnPrepareDC(pDC, pInfo);
  515. // mapping mode is MM_ANISOTROPIC
  516. // these extents setup a mode similar to MM_LOENGLISH
  517. // MM_LOENGLISH is in .01 physical inches
  518. // these extents provide .01 logical inches
  519. pDC->SetMapMode(MM_ANISOTROPIC);
  520. pDC->SetViewportExt(pDC->GetDeviceCaps(LOGPIXELSX),
  521. pDC->GetDeviceCaps(LOGPIXELSY));
  522. pDC->SetWindowExt(100, -100);
  523. // set the origin of the coordinate system to the center of the page
  524. CPoint ptOrg;
  525. ptOrg.x = GetDocument()->GetSize().cx / 2;
  526. ptOrg.y = GetDocument()->GetSize().cy / 2;
  527. // ptOrg is in logical coordinates
  528. pDC->OffsetWindowOrg(-ptOrg.x,ptOrg.y);
  529. }
  530. CRect CDrawView::GetInitialPosition()
  531. {
  532. CRect rect(10, 10, 10, 10);
  533. ClientToDoc(rect);
  534. return rect;
  535. }
  536. //用来显示所画图形
  537. void CDrawView::OnActivateView(BOOL bActivate,CView* pActiveView,CView* pDeactiveView)
  538. {
  539. CView::OnActivateView(bActivate, pActiveView, pDeactiveView);
  540. // invalidate selections when active status changes
  541. if (m_bActive != bActivate)
  542. {
  543. if (bActivate)  // if becoming active update as if active
  544. m_bActive = bActivate;
  545. if (!m_selection.IsEmpty())
  546. OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  547. m_bActive = bActivate;
  548. }
  549. }
  550. void CDrawView::OnDrawCircle() 
  551. {
  552. CDrawTool::c_drawShape = circle;
  553. }
  554. void CDrawView::OnDrawEllipse() 
  555. {
  556. CDrawTool::c_drawShape = ellipse;
  557. }
  558. void CDrawView::OnDrawLine() 
  559. {
  560. CDrawTool::c_drawShape = line;
  561. }
  562. void CDrawView::OnDrawRoundrect() 
  563. {
  564. CDrawTool::c_drawShape = roundRect;
  565. }
  566. void CDrawView::OnEditProperties() 
  567. {
  568. if (m_selection.GetCount() == 1 && CDrawTool::c_drawShape == selection)
  569. {
  570. CDrawTool* pTool = CDrawTool::FindTool(CDrawTool::c_drawShape);
  571. ASSERT(pTool != NULL);
  572. pTool->OnEditProperties(this);
  573. }
  574. }
  575. void CDrawView::OnObjectMoveback() 
  576. {
  577. CDrawDoc* pDoc = GetDocument();
  578. CDrawObj* pObj = m_selection.GetHead();
  579. CDrawObjList* pObjects = pDoc->GetObjects();
  580. POSITION pos = pObjects->Find(pObj);
  581. ASSERT(pos != NULL);
  582. if (pos != pObjects->GetHeadPosition())
  583. {
  584. POSITION posPrev = pos;
  585. pObjects->GetPrev(posPrev);
  586. pObjects->RemoveAt(pos);
  587. pObjects->InsertBefore(posPrev, pObj);
  588. InvalObj(pObj);
  589. }
  590. }
  591. void CDrawView::OnObjectMoveforward() 
  592. {
  593. CDrawDoc* pDoc = GetDocument();
  594. CDrawObj* pObj = m_selection.GetHead();
  595. CDrawObjList* pObjects = pDoc->GetObjects();
  596. POSITION pos = pObjects->Find(pObj);
  597. ASSERT(pos != NULL);
  598. if (pos != pObjects->GetTailPosition())
  599. {
  600. POSITION posNext = pos;
  601. pObjects->GetNext(posNext);
  602. pObjects->RemoveAt(pos);
  603. pObjects->InsertAfter(posNext, pObj);
  604. InvalObj(pObj);
  605. }
  606. }
  607. void CDrawView::OnObjectMovetoback() 
  608. {
  609. CDrawDoc* pDoc = GetDocument();
  610. CDrawObj* pObj = m_selection.GetHead();
  611. CDrawObjList* pObjects = pDoc->GetObjects();
  612. POSITION pos = pObjects->Find(pObj);
  613. ASSERT(pos != NULL);
  614. pObjects->RemoveAt(pos);
  615. pObjects->AddHead(pObj);
  616. InvalObj(pObj);
  617. }
  618. void CDrawView::OnObjectMovetofront() 
  619. {
  620. CDrawDoc* pDoc = GetDocument();
  621. CDrawObj* pObj = m_selection.GetHead();
  622. CDrawObjList* pObjects = pDoc->GetObjects();
  623. POSITION pos = pObjects->Find(pObj);
  624. ASSERT(pos != NULL);
  625. pObjects->RemoveAt(pos);
  626. pObjects->AddTail(pObj);
  627. InvalObj(pObj);
  628. }
  629. void CDrawView::OnUpdateObjectMoveback(CCmdUI* pCmdUI) 
  630. {
  631. pCmdUI->Enable(m_selection.GetCount() == 1);
  632. }
  633. void CDrawView::OnUpdateObjectMoveforward(CCmdUI* pCmdUI) 
  634. {
  635. pCmdUI->Enable(m_selection.GetCount() == 1);
  636. }
  637. void CDrawView::OnUpdateObjectMovetoback(CCmdUI* pCmdUI) 
  638. {
  639. pCmdUI->Enable(m_selection.GetCount() == 1);
  640. }
  641. void CDrawView::OnUpdateObjectMovetofront(CCmdUI* pCmdUI) 
  642. {
  643. pCmdUI->Enable(m_selection.GetCount() == 1);
  644. }
  645. void CDrawView::OnObjectFillcolor() 
  646. {
  647. CColorDialog dlg;
  648. if (dlg.DoModal() != IDOK)
  649. return;
  650. COLORREF color = dlg.GetColor();
  651. POSITION pos = m_selection.GetHeadPosition();
  652. while (pos != NULL)
  653. {
  654. CDrawObj* pObj = m_selection.GetNext(pos);
  655. pObj->SetFillColor(color);
  656. }
  657. }
  658. void CDrawView::OnObjectLinecolor() 
  659. {
  660. CColorDialog dlg;
  661. if (dlg.DoModal() != IDOK)
  662. return;
  663. COLORREF color = dlg.GetColor();
  664. POSITION pos = m_selection.GetHeadPosition();
  665. while (pos != NULL)
  666. {
  667. CDrawObj* pObj = m_selection.GetNext(pos);
  668. pObj->SetLineColor(color);
  669. }
  670. }
  671. void CDrawView::OnUpdateObjectFillcolor(CCmdUI* pCmdUI) 
  672. {
  673. pCmdUI->Enable(m_selection.GetCount() == 1);
  674. }
  675. void CDrawView::OnUpdateObjectLinecolor(CCmdUI* pCmdUI) 
  676. {
  677. pCmdUI->Enable(m_selection.GetCount() == 1);
  678. }
  679. void CDrawView::OnViewGrid() 
  680. {
  681. m_bGrid = !m_bGrid;
  682. Invalidate(FALSE);
  683. }
  684. void CDrawView::OnUpdateViewGrid(CCmdUI* pCmdUI) 
  685. {
  686. pCmdUI->SetCheck(m_bGrid);
  687. }
  688. void CDrawView::OnViewShowobjects() 
  689. {
  690. CDrawOleObj::c_bShowItems = !CDrawOleObj::c_bShowItems;
  691. GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_OLE_ITEMS, NULL);
  692. }
  693. void CDrawView::OnUpdateViewShowobjects(CCmdUI* pCmdUI) 
  694. {
  695. pCmdUI->SetCheck(CDrawOleObj::c_bShowItems);
  696. }
  697. void CDrawView::OnEditClear() 
  698. {
  699. GetDocument()->UpdateAllViews(NULL, HINT_DELETE_SELECTION, &m_selection);
  700. OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  701. // now remove the selection from the document
  702. POSITION pos = m_selection.GetHeadPosition();
  703. while (pos != NULL)
  704. {
  705. CDrawObj* pObj = m_selection.GetNext(pos);
  706. GetDocument()->Remove(pObj);
  707. pObj->Remove();
  708. }
  709. m_selection.RemoveAll();
  710. }
  711. void CDrawView::OnEditCopy() 
  712. {
  713. ASSERT_VALID(this);
  714. ASSERT(m_cfDraw != NULL);
  715. // Create a shared file and associate a CArchive with it
  716. CSharedFile file;
  717. CArchive ar(&file, CArchive::store);
  718. // Serialize selected objects to the archive
  719. m_selection.Serialize(ar);
  720. ar.Close();
  721. COleDataSource* pDataSource = NULL;
  722. TRY
  723. {
  724. pDataSource = new COleDataSource;
  725. // put on local format instead of or in addation to
  726. pDataSource->CacheGlobalData(m_cfDraw, file.Detach());
  727. // if only one item and it is a COleClientItem then also
  728. // paste in that format
  729. CDrawObj* pDrawObj = m_selection.GetHead();
  730. if (m_selection.GetCount() == 1 &&
  731. pDrawObj->IsKindOf(RUNTIME_CLASS(CDrawOleObj)))
  732. {
  733. CDrawOleObj* pDrawOle = (CDrawOleObj*)pDrawObj;
  734. pDrawOle->m_pClientItem->GetClipboardData(pDataSource, FALSE);
  735. }
  736. pDataSource->SetClipboard();
  737. }
  738. CATCH_ALL(e)
  739. {
  740. delete pDataSource;
  741. THROW_LAST();
  742. }
  743. END_CATCH_ALL
  744. }
  745. void CDrawView::OnEditCut() 
  746. {
  747. OnEditCopy();
  748. OnEditClear();
  749. }
  750. void CDrawView::OnEditPaste() 
  751. {
  752. COleDataObject dataObject;
  753. dataObject.AttachClipboard();
  754. // invalidate current selection since it will be deselected
  755. OnUpdate(NULL, HINT_UPDATE_SELECTION, NULL);
  756. m_selection.RemoveAll();
  757. if (dataObject.IsDataAvailable(m_cfDraw))
  758. {
  759. PasteNative(dataObject);
  760. // now add all items in m_selection to document
  761. POSITION pos = m_selection.GetHeadPosition();
  762. while (pos != NULL)
  763. GetDocument()->Add(m_selection.GetNext(pos));
  764. }
  765. else
  766. PasteEmbedded(dataObject, GetInitialPosition().TopLeft() );
  767. GetDocument()->SetModifiedFlag();
  768. // invalidate new pasted stuff
  769. GetDocument()->UpdateAllViews(NULL, HINT_UPDATE_SELECTION, &m_selection);
  770. }
  771. void CDrawView::OnEditSelectAll() 
  772. {
  773. CDrawObjList* pObList = GetDocument()->GetObjects();
  774. POSITION pos = pObList->GetHeadPosition();
  775. while (pos != NULL)
  776. Select(pObList->GetNext(pos), TRUE);
  777. }
  778. void CDrawView::PasteNative(COleDataObject& dataObject)
  779. {
  780. // get file refering to clipboard data
  781. CFile* pFile = dataObject.GetFileData(m_cfDraw);
  782. if (pFile == NULL)
  783. return;
  784. // connect the file to the archive
  785. CArchive ar(pFile, CArchive::load);
  786. TRY
  787. {
  788. ar.m_pDocument = GetDocument(); // set back-pointer in archive
  789. // read the selection
  790. m_selection.Serialize(ar);
  791. }
  792. CATCH_ALL(e)
  793. {
  794. ar.Close();
  795. delete pFile;
  796. THROW_LAST();
  797. }
  798. END_CATCH_ALL
  799. ar.Close();
  800. delete pFile;
  801. }
  802. void CDrawView::PasteEmbedded(COleDataObject& dataObject, CPoint point)
  803. {
  804. BeginWaitCursor();
  805. // paste embedded
  806. CDrawOleObj* pObj = new CDrawOleObj(GetInitialPosition());
  807. ASSERT_VALID(pObj);
  808. CDrawItem* pItem = new CDrawItem(GetDocument(), pObj);
  809. ASSERT_VALID(pItem);
  810. pObj->m_pClientItem = pItem;
  811. TRY
  812. {
  813. if (!pItem->CreateFromData(&dataObject) &&
  814. !pItem->CreateStaticFromData(&dataObject))
  815. {
  816. AfxThrowMemoryException();      // any exception will do
  817. }
  818. // add the object to the document
  819. GetDocument()->Add(pObj);
  820. m_selection.AddTail(pObj);
  821. ClientToDoc( point );
  822. pObj->MoveTo( CRect( point, pObj->m_extent ), this );
  823. // try to get initial presentation data
  824. pItem->UpdateLink();
  825. pItem->UpdateExtent();
  826. }
  827. CATCH_ALL(e)
  828. {
  829. // clean up item
  830. pItem->Delete();
  831. pObj->m_pClientItem = NULL;
  832. GetDocument()->Remove(pObj);
  833. pObj->Remove();
  834. AfxMessageBox(IDP_FAILED_TO_CREATE);
  835. }
  836. END_CATCH_ALL
  837. EndWaitCursor();
  838. }
  839. void CDrawView::OnUpdateEditClear(CCmdUI* pCmdUI) 
  840. {
  841. pCmdUI->Enable(!m_selection.IsEmpty());
  842. }
  843. void CDrawView::OnUpdateEditCopy(CCmdUI* pCmdUI) 
  844. {
  845. pCmdUI->Enable(!m_selection.IsEmpty());
  846. }
  847. void CDrawView::OnUpdateEditCut(CCmdUI* pCmdUI) 
  848. {
  849. pCmdUI->Enable(!m_selection.IsEmpty());
  850. }
  851. void CDrawView::OnUpdateEditPaste(CCmdUI* pCmdUI) 
  852. {
  853. // determine if private or standard OLE formats are on the clipboard
  854. COleDataObject dataObject;
  855. BOOL bEnable = dataObject.AttachClipboard() &&
  856. (dataObject.IsDataAvailable(m_cfDraw) ||
  857.  COleClientItem::CanCreateFromData(&dataObject));
  858. // enable command based on availability
  859. pCmdUI->Enable(bEnable);
  860. }
  861. void CDrawView::OnUpdateEditSelectAll(CCmdUI* pCmdUI) 
  862. {
  863. pCmdUI->Enable(GetDocument()->GetObjects()->GetCount() != 0);
  864. }
  865. void CDrawView::OnUpdate(CView* , LPARAM lHint, CObject* pHint)
  866. {
  867. switch (lHint)
  868. {
  869. case HINT_UPDATE_WINDOW:    // redraw entire window
  870. Invalidate(FALSE);
  871. break;
  872. case HINT_UPDATE_DRAWOBJ:   // a single object has changed
  873. InvalObj((CDrawObj*)pHint);
  874. break;
  875. case HINT_UPDATE_SELECTION: // an entire selection has changed
  876. {
  877. CDrawObjList* pList = pHint != NULL ?
  878. (CDrawObjList*)pHint : &m_selection;
  879. POSITION pos = pList->GetHeadPosition();
  880. while (pos != NULL)
  881. InvalObj(pList->GetNext(pos));
  882. }
  883. break;
  884. case HINT_DELETE_SELECTION: // an entire selection has been removed
  885. if (pHint != &m_selection)
  886. {
  887. CDrawObjList* pList = (CDrawObjList*)pHint;
  888. POSITION pos = pList->GetHeadPosition();
  889. while (pos != NULL)
  890. {
  891. CDrawObj* pObj = pList->GetNext(pos);
  892. InvalObj(pObj);
  893. Remove(pObj);   // remove it from this view's selection
  894. }
  895. }
  896. break;
  897. case HINT_UPDATE_OLE_ITEMS:
  898. {
  899. CDrawDoc* pDoc = GetDocument();
  900. POSITION pos = pDoc->GetObjects()->GetHeadPosition();
  901. while (pos != NULL)
  902. {
  903. CDrawObj* pObj = pDoc->GetObjects()->GetNext(pos);
  904. if (pObj->IsKindOf(RUNTIME_CLASS(CDrawOleObj)))
  905. InvalObj(pObj);
  906. }
  907. }
  908. break;
  909. default:
  910. ASSERT(FALSE);
  911. break;
  912. }
  913. }
  914. void CDrawView::DemoData()
  915. {
  916. COLORREF   color;
  917. POSITION   pos;
  918.   CDrawObjList* pObList=GetDocument()->GetObjects();
  919.     pos=pObList->GetHeadPosition();
  920. while (pos!=NULL)
  921. {
  922. CDrawObj* pObj=pObList->GetNext(pos);
  923. if (pObj->m_nType==1) // DI/DO状态量
  924. {
  925. color=(Demo_Data.DI_DO[pObj->m_nOrder]==0)?RGB(0,255,0):RGB(255,0,0);
  926. pObj->SetFillColor(color);
  927. }
  928. }
  929. CDrawDoc* pDoc = GetDocument();
  930. ASSERT_VALID(pDoc);
  931. pDoc->SetModifiedFlag(FALSE);
  932. }
  933. int CDrawView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  934. {
  935. if (CScrollView::OnCreate(lpCreateStruct) == -1)
  936. return -1;
  937. CMainFrame *pFrame =(CMainFrame*)AfxGetApp()->m_pMainWnd;
  938. pFrame->vHwnd=this->m_hWnd;
  939. return 0;
  940. }