TRACKVW.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:8k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // trackvw.cpp : implementation of the CTrackerView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. #include "stdafx.h"
  13. #include "trackapp.h"
  14. #include "trackdoc.h"
  15. #include "trackvw.h"
  16. #include "dialogs.h"
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CTrackerView
  23. IMPLEMENT_DYNCREATE(CTrackerView, CView)
  24. BEGIN_MESSAGE_MAP(CTrackerView, CView)
  25. //{{AFX_MSG_MAP(CTrackerView)
  26. ON_WM_LBUTTONDOWN()
  27. ON_WM_SETCURSOR()
  28. ON_COMMAND(ID_VIEW_SETHANDLESIZE, OnViewSethandlesize)
  29. ON_COMMAND(ID_VIEW_SETMINIMUMSIZE, OnViewSetminimumsize)
  30. //}}AFX_MSG_MAP
  31. // Standard printing commands
  32. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  33. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CTrackerView construction/destruction
  37. CTrackerView::CTrackerView()
  38. {
  39. // TODO: add construction code here
  40. }
  41. CTrackerView::~CTrackerView()
  42. {
  43. }
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CTrackerView drawing
  46. static void SetNormalRect(CRect& rect, int left, int top, int width, int height)
  47. {
  48. // set it
  49. rect.left = left;
  50. rect.top = top;
  51. rect.right = left + width;
  52. rect.bottom = top + height;
  53. // normalize it
  54. int nTemp;
  55. if (rect.left > rect.right)
  56. {
  57. nTemp = rect.left;
  58. rect.left = rect.right;
  59. rect.right = nTemp;
  60. }
  61. if (rect.top > rect.bottom)
  62. {
  63. nTemp = rect.top;
  64. rect.top = rect.bottom;
  65. rect.bottom = nTemp;
  66. }
  67. }
  68. void CTrackerView::OnDraw(CDC* pDC)
  69. {
  70. CTrackerDoc* pDoc = GetDocument();
  71. CBrush* pOldBrush = NULL;
  72. TRY
  73. {
  74. // draw inside in various colors
  75. CBrush brush1, brush2;
  76. CRect rect;
  77. int nWidth = pDoc->m_tracker.m_rect.Width();
  78. int nHeight = pDoc->m_tracker.m_rect.Height();
  79. int nSgnX = nWidth != 0 ? nWidth / abs(nWidth) : 1;
  80. int nSgnY = nHeight != 0 ? nHeight / abs(nHeight) : 1;
  81. pDC->SetTextAlign(TA_CENTER);
  82. pDC->SetBkMode(TRANSPARENT);
  83. int nCenterX, nCenterY;
  84. TEXTMETRIC tm;
  85. pDC->GetTextMetrics(&tm);
  86. brush1.CreateSolidBrush(RGB(255, 0, 0));
  87. pOldBrush = pDC->SelectObject(&brush1);
  88. SetNormalRect(rect, pDoc->m_tracker.m_rect.left,
  89. pDoc->m_tracker.m_rect.top, nWidth/2, nHeight/2);
  90. pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  91. nCenterX = rect.left + rect.Width()/2;
  92. nCenterY = rect.top + rect.Height()/2 - tm.tmHeight/2;
  93. pDC->ExtTextOut(nCenterX, nCenterY, ETO_CLIPPED, rect, _T("1"), 1, NULL);
  94. brush2.CreateSolidBrush(RGB(0, 255, 0));
  95. pDC->SelectObject(&brush2);
  96. brush1.DeleteObject();
  97. SetNormalRect(rect, pDoc->m_tracker.m_rect.left+nWidth/2,
  98. pDoc->m_tracker.m_rect.top, (nWidth+nSgnX)/2, nHeight/2);
  99. pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  100. nCenterX = rect.left + rect.Width()/2;
  101. nCenterY = rect.top + rect.Height()/2 - tm.tmHeight/2;
  102. pDC->ExtTextOut(nCenterX, nCenterY, ETO_CLIPPED, rect, _T("2"), 1, NULL);
  103. brush1.CreateSolidBrush(RGB(0, 0, 255));
  104. pDC->SelectObject(&brush1);
  105. brush2.DeleteObject();
  106. SetNormalRect(rect, pDoc->m_tracker.m_rect.left,
  107. pDoc->m_tracker.m_rect.top+nHeight/2, nWidth/2, (nHeight+nSgnY)/2);
  108. pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  109. nCenterX = rect.left + rect.Width()/2;
  110. nCenterY = rect.top + rect.Height()/2 - tm.tmHeight/2;
  111. pDC->ExtTextOut(nCenterX, nCenterY, ETO_CLIPPED, rect, _T("3"), 1, NULL);
  112. brush2.CreateSolidBrush(RGB(192, 192, 192));
  113. pDC->SelectObject(&brush2);
  114. brush1.DeleteObject();
  115. SetNormalRect(rect, pDoc->m_tracker.m_rect.left+nWidth/2,
  116. pDoc->m_tracker.m_rect.top+nHeight/2,
  117. (nWidth+nSgnX)/2, (nHeight+nSgnY)/2);
  118. pDC->PatBlt(rect.left, rect.top, rect.Width(), rect.Height(), PATCOPY);
  119. nCenterX = rect.left + rect.Width()/2;
  120. nCenterY = rect.top + rect.Height()/2 - tm.tmHeight/2;
  121. pDC->ExtTextOut(nCenterX, nCenterY, ETO_CLIPPED, rect, _T("4"), 1, NULL);
  122. // cleanup DC
  123. if (pOldBrush != NULL)
  124. pDC->SelectObject(pOldBrush);
  125. brush2.DeleteObject();
  126. // draw tracker on outside
  127. pDoc->m_tracker.Draw(pDC);
  128. }
  129. CATCH_ALL(e)
  130. {
  131. if (pOldBrush != NULL)
  132. pDC->SelectObject(pOldBrush);
  133. }
  134. END_CATCH_ALL
  135. }
  136. /////////////////////////////////////////////////////////////////////////////
  137. // CTrackerView printing
  138. BOOL CTrackerView::OnPreparePrinting(CPrintInfo* pInfo)
  139. {
  140. // default preparation
  141. return DoPreparePrinting(pInfo);
  142. }
  143. void CTrackerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  144. {
  145. // TODO: add extra initialization before printing
  146. }
  147. void CTrackerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  148. {
  149. // TODO: add cleanup after printing
  150. }
  151. /////////////////////////////////////////////////////////////////////////////
  152. // CTrackerView diagnostics
  153. #ifdef _DEBUG
  154. void CTrackerView::AssertValid() const
  155. {
  156. CView::AssertValid();
  157. }
  158. void CTrackerView::Dump(CDumpContext& dc) const
  159. {
  160. CView::Dump(dc);
  161. }
  162. CTrackerDoc* CTrackerView::GetDocument() // non-debug version is inline
  163. {
  164. return STATIC_DOWNCAST(CTrackerDoc, m_pDocument);
  165. }
  166. #endif //_DEBUG
  167. /////////////////////////////////////////////////////////////////////////////
  168. // CTrackerView update
  169. void CTrackerView::OnUpdate(CView* /*pSender*/, LPARAM lHint, CObject* /*pHint*/)
  170. {
  171. if (lHint == 0)
  172. {
  173. CRect rectTrue;
  174. CTrackerDoc* pDoc = GetDocument();
  175. pDoc->m_tracker.GetTrueRect(&rectTrue);
  176. InvalidateRect(rectTrue);
  177. }
  178. else
  179. {
  180. InvalidateRect((CRect*)lHint);
  181. }
  182. }
  183. /////////////////////////////////////////////////////////////////////////////
  184. // CTrackerView message handlers
  185. void CTrackerView::OnLButtonDown(UINT nFlags, CPoint point)
  186. {
  187. CTrackerDoc* pDoc = GetDocument();
  188. CRect rectSave;
  189. pDoc->m_tracker.GetTrueRect(rectSave);
  190. if (pDoc->m_tracker.HitTest(point) < 0)
  191. {
  192. // just to demonstrate CRectTracker::TrackRubberBand
  193. CRectTracker tracker;
  194. if (tracker.TrackRubberBand(this, point, pDoc->m_bAllowInvert))
  195. {
  196. MessageBeep(0); // beep indicates TRUE
  197. // see if rubber band intersects with the doc's tracker
  198. CRect rectT;
  199. tracker.m_rect.NormalizeRect(); // so intersect rect works
  200. if (rectT.IntersectRect(tracker.m_rect, pDoc->m_tracker.m_rect))
  201. {
  202. // if so, put resize handles on it (ie. select it)
  203. if (pDoc->m_tracker.m_nStyle & CRectTracker::resizeInside)
  204. {
  205. // swap from resize inside to resize outside for effect
  206. pDoc->m_tracker.m_nStyle &= ~CRectTracker::resizeInside;
  207. pDoc->m_tracker.m_nStyle |= CRectTracker::resizeOutside;
  208. }
  209. else
  210. {
  211. // just use inside resize handles on first time
  212. pDoc->m_tracker.m_nStyle &= ~CRectTracker::resizeOutside;
  213. pDoc->m_tracker.m_nStyle |= CRectTracker::resizeInside;
  214. }
  215. pDoc->SetModifiedFlag();
  216. pDoc->UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectSave);
  217. pDoc->UpdateAllViews(NULL);
  218. }
  219. }
  220. }
  221. else if (pDoc->m_tracker.Track(this, point, pDoc->m_bAllowInvert))
  222. {
  223. // normal tracking action, when tracker is "hit"
  224. pDoc->SetModifiedFlag();
  225. pDoc->UpdateAllViews(NULL, (LPARAM)(LPCRECT)rectSave);
  226. pDoc->UpdateAllViews(NULL);
  227. }
  228. CView::OnLButtonDown(nFlags, point);
  229. }
  230. BOOL CTrackerView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  231. {
  232. // forward to tracker
  233. CTrackerDoc* pDoc = GetDocument();
  234. if (pWnd == this && pDoc->m_tracker.SetCursor(this, nHitTest))
  235. return TRUE;
  236. return CView::OnSetCursor(pWnd, nHitTest, message);
  237. }
  238. void CTrackerView::OnViewSethandlesize()
  239. {
  240. CTrackerDoc* pDoc = GetDocument();
  241. CHandleSize dlg;
  242. dlg.m_nHandleSize = pDoc->m_tracker.m_nHandleSize;
  243. if (dlg.DoModal() == IDOK)
  244. {
  245. pDoc->m_tracker.m_nHandleSize = dlg.m_nHandleSize;
  246. pDoc->UpdateAllViews(NULL);
  247. }
  248. }
  249. void CTrackerView::OnViewSetminimumsize()
  250. {
  251. CTrackerDoc* pDoc = GetDocument();
  252. CMinSize dlg;
  253. dlg.m_nMinX = pDoc->m_tracker.m_sizeMin.cx;
  254. dlg.m_nMinY = pDoc->m_tracker.m_sizeMin.cy;
  255. if (dlg.DoModal() == IDOK)
  256. {
  257. pDoc->m_tracker.m_sizeMin.cx = dlg.m_nMinX;
  258. pDoc->m_tracker.m_sizeMin.cy = dlg.m_nMinY;
  259. pDoc->UpdateAllViews(NULL);
  260. }
  261. }