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