ZoomPartView.cpp
上传用户:aokegd
上传日期:2009-12-14
资源大小:1276k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // ZoomPartView.cpp : implementation of the CZoomPartView class
  2. //
  3. #include "stdafx.h"
  4. #include "ZoomPart.h"
  5. #include "ZoomPartDoc.h"
  6. #include "ZoomPartView.h"
  7. #include "MainFrm.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CZoomPartView
  15. IMPLEMENT_DYNCREATE(CZoomPartView, CView)
  16. BEGIN_MESSAGE_MAP(CZoomPartView, CView)
  17. //{{AFX_MSG_MAP(CZoomPartView)
  18. ON_WM_MOUSEMOVE()
  19. ON_WM_LBUTTONDOWN()
  20. ON_WM_RBUTTONDOWN()
  21. //}}AFX_MSG_MAP
  22. // Standard printing commands
  23. ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  25. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  26. END_MESSAGE_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CZoomPartView construction/destruction
  29. CZoomPartView::CZoomPartView()
  30. {
  31. // TODO: add construction code here
  32. m_pdcMem = new CDC;
  33. m_pBitmap = new CBitmap;
  34. recover = true;
  35. s = 30; d = 45;
  36. mana = SRCCOPY;
  37. }
  38. CZoomPartView::~CZoomPartView()
  39. {
  40. delete m_pdcMem;
  41. delete m_pBitmap;
  42. }
  43. BOOL CZoomPartView::PreCreateWindow(CREATESTRUCT& cs)
  44. {
  45. // TODO: Modify the Window class or styles here by modifying
  46. //  the CREATESTRUCT cs
  47. return CView::PreCreateWindow(cs);
  48. }
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CZoomPartView drawing
  51. void CZoomPartView::OnDraw(CDC* pDC)
  52. {
  53. CZoomPartDoc* pDoc = GetDocument();
  54. ASSERT_VALID(pDoc);
  55. // TODO: add draw code for native data here
  56. //声明判断是否load位图的静态标志
  57. static bool load;
  58. //按原来大小显示位图
  59. if (!load) {
  60. BITMAP bm;
  61. load = !load;
  62. m_pBitmap->LoadBitmap(IDB_BITMAP1);
  63. m_pdcMem->CreateCompatibleDC(pDC);
  64. m_pdcMem->SelectObject(m_pBitmap);
  65. m_pBitmap->GetObject(sizeof(bm),&bm);
  66. m_sizeSource.cx = bm.bmWidth;
  67. m_sizeSource.cy = bm.bmHeight;
  68. m_sizeDest = m_sizeSource;
  69. pDC->StretchBlt(0,0,m_sizeSource.cx,m_sizeSource.cy,
  70. m_pdcMem,0,0,m_sizeSource.cx,m_sizeSource.cy,mana);
  71. }
  72. else {
  73. pDC->StretchBlt(0,0,m_sizeSource.cx,m_sizeSource.cy,
  74. m_pdcMem,0,0,m_sizeSource.cx,m_sizeSource.cy,mana);
  75. }
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CZoomPartView printing
  79. BOOL CZoomPartView::OnPreparePrinting(CPrintInfo* pInfo)
  80. {
  81. // default preparation
  82. return DoPreparePrinting(pInfo);
  83. }
  84. void CZoomPartView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  85. {
  86. // TODO: add extra initialization before printing
  87. }
  88. void CZoomPartView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  89. {
  90. // TODO: add cleanup after printing
  91. }
  92. /////////////////////////////////////////////////////////////////////////////
  93. // CZoomPartView diagnostics
  94. #ifdef _DEBUG
  95. void CZoomPartView::AssertValid() const
  96. {
  97. CView::AssertValid();
  98. }
  99. void CZoomPartView::Dump(CDumpContext& dc) const
  100. {
  101. CView::Dump(dc);
  102. }
  103. CZoomPartDoc* CZoomPartView::GetDocument() // non-debug version is inline
  104. {
  105. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CZoomPartDoc)));
  106. return (CZoomPartDoc*)m_pDocument;
  107. }
  108. #endif //_DEBUG
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CZoomPartView message handlers
  111. void CZoomPartView::OnMouseMove(UINT nFlags, CPoint point) 
  112. {
  113. //计算要放大的局部矩形的源图像位置和目标位置
  114. CString cord;
  115. int dd;
  116.     CRect srect,drect,mrect;
  117. srect.left = point.x - s;
  118. srect.top = point.y - s;
  119. srect.right = point.x + s;
  120. srect.bottom = point.y + s;
  121. drect.left = point.x - d;
  122. drect.top = point.y - d;
  123. drect.right = point.x + d;
  124. drect.bottom = point.y + d;
  125. mrect.left = oldx - d;
  126. mrect.top = oldy - d;
  127. mrect.right = oldx + d;
  128. mrect.bottom = oldy + d;
  129. dd = 2*d;
  130. CDC * pDC = GetDC();
  131. OnPrepareDC(pDC);
  132. //放大图像
  133. if (recover)
  134. {
  135. pDC->BitBlt(mrect.left,mrect.top,dd,dd,
  136. m_pdcMem,mrect.left,mrect.top,mana);
  137. }
  138. pDC->StretchBlt(drect.left,drect.top,
  139. drect.Width(),drect.Height(),m_pdcMem,srect.left,
  140. srect.top,srect.Width(),srect.Height(),SRCCOPY);
  141. oldx = point.x; oldy = point.y;
  142. ReleaseDC(pDC);
  143. recover = true;
  144. CView::OnMouseMove(nFlags, point);
  145. }
  146. void CZoomPartView::OnLButtonDown(UINT nFlags, CPoint point) 
  147. {
  148. //如果鼠标位置不在位图上,则还原位图大小显示
  149. CRect rc(0,0,m_sizeSource.cx,m_sizeSource.cy);
  150. if(!rc.PtInRect(point))
  151. {
  152. Invalidate();
  153. }
  154. else if (d > 5)//如果放大倍数大于5,就继续减小放大倍数,然后进行放大显示
  155. {
  156. CDC * pDC = GetDC();
  157. pDC->StretchBlt(oldx - d,oldy - d,2*d,
  158. 2*d,m_pdcMem,oldx - d,oldy - d,2*d,2*d,mana);
  159. d -= 10;
  160. ReleaseDC(pDC);
  161. CZoomPartView::OnMouseMove(nFlags, point);
  162. CView::OnLButtonDown(nFlags, point);
  163. }
  164. void CZoomPartView::OnRButtonDown(UINT nFlags, CPoint point) 
  165. {
  166. //如果鼠标位置不在位图上,则还原位图大小显示
  167. CRect rc(0,0,m_sizeSource.cx,m_sizeSource.cy);
  168. if(!rc.PtInRect(point))
  169. {
  170. Invalidate();
  171. }
  172. else if (d <150)//如果放大倍数小于150,就继续增加放大倍数,然后进行放大显示
  173. d += 10;
  174. CZoomPartView::OnMouseMove(nFlags, point); 
  175. CView::OnRButtonDown(nFlags, point);
  176. }