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

书籍源码

开发平台:

Visual C++

  1. // MoveSelObView.cpp : implementation of the CMoveSelObView class
  2. //
  3. #include "stdafx.h"
  4. #include "MoveSelOb.h"
  5. #include "MoveSelObDoc.h"
  6. #include "MoveSelObView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CMoveSelObView
  14. IMPLEMENT_DYNCREATE(CMoveSelObView, CScrollView)
  15. BEGIN_MESSAGE_MAP(CMoveSelObView, CScrollView)
  16. //{{AFX_MSG_MAP(CMoveSelObView)
  17. ON_WM_LBUTTONDOWN()
  18. ON_WM_LBUTTONUP()
  19. ON_WM_MOUSEMOVE()
  20. //}}AFX_MSG_MAP
  21. // Standard printing commands
  22. ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CMoveSelObView construction/destruction
  28. CMoveSelObView::CMoveSelObView() : m_sizeEllipse(100, -100),
  29.    m_pointTopLeft(0, 0),
  30.    m_sizeOffset(0, 0)
  31. {
  32.     m_bCaptured = FALSE;
  33. }
  34. CMoveSelObView::~CMoveSelObView()
  35. {
  36. }
  37. BOOL CMoveSelObView::PreCreateWindow(CREATESTRUCT& cs)
  38. {
  39. // TODO: Modify the Window class or styles here by modifying
  40. //  the CREATESTRUCT cs
  41. return CScrollView::PreCreateWindow(cs);
  42. }
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CMoveSelObView drawing
  45. void CMoveSelObView::OnDraw(CDC* pDC)
  46. {
  47. CMoveSelObDoc* pDoc = GetDocument();
  48. ASSERT_VALID(pDoc);
  49. //创建红色画刷
  50. CBrush brushHatch(HS_DIAGCROSS, RGB(255, 0, 0));
  51.     CPoint point(0, 0);                  
  52. //转换逻辑坐标到设备坐标
  53.     pDC->LPtoDP(&point);                 
  54.     pDC->SetBrushOrg(point);             
  55.     CBrush* pOldBrush = pDC->SelectObject(&brushHatch);
  56. //画红色圆圈
  57.     pDC->Ellipse(CRect(m_pointTopLeft, m_sizeEllipse));
  58. pDC->SelectObject(pOldBrush);
  59. // TODO: add draw code for native data here
  60. }
  61. void CMoveSelObView::OnInitialUpdate()
  62. {
  63. CScrollView::OnInitialUpdate();
  64. //设置滚动范围
  65. CSize sizeTotal(800, 1050); // 8-by-10.5 inches
  66.     CSize sizePage(sizeTotal.cx / 2, sizeTotal.cy / 2);
  67.     CSize sizeLine(sizeTotal.cx / 50, sizeTotal.cy / 50);
  68.     SetScrollSizes(MM_LOENGLISH, sizeTotal, sizePage, sizeLine);
  69. }
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CMoveSelObView printing
  72. BOOL CMoveSelObView::OnPreparePrinting(CPrintInfo* pInfo)
  73. {
  74. // default preparation
  75. return DoPreparePrinting(pInfo);
  76. }
  77. void CMoveSelObView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  78. {
  79. // TODO: add extra initialization before printing
  80. }
  81. void CMoveSelObView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  82. {
  83. // TODO: add cleanup after printing
  84. }
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CMoveSelObView diagnostics
  87. #ifdef _DEBUG
  88. void CMoveSelObView::AssertValid() const
  89. {
  90. CScrollView::AssertValid();
  91. }
  92. void CMoveSelObView::Dump(CDumpContext& dc) const
  93. {
  94. CScrollView::Dump(dc);
  95. }
  96. CMoveSelObDoc* CMoveSelObView::GetDocument() // non-debug version is inline
  97. {
  98. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMoveSelObDoc)));
  99. return (CMoveSelObDoc*)m_pDocument;
  100. }
  101. #endif //_DEBUG
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CMoveSelObView message handlers
  104. void CMoveSelObView::OnLButtonDown(UINT nFlags, CPoint point) 
  105. {
  106. CRect rectEllipse(m_pointTopLeft, m_sizeEllipse); 
  107.     CRgn  circle;
  108.     CClientDC dc(this);
  109.     OnPrepareDC(&dc);
  110.     dc.LPtoDP(rectEllipse); 
  111.     circle.CreateEllipticRgnIndirect(rectEllipse);
  112.     if (circle.PtInRegion(point)) {
  113.         //捕捉鼠标
  114.         SetCapture();
  115.         m_bCaptured = TRUE;
  116.         CPoint pointTopLeft(m_pointTopLeft);
  117.         dc.LPtoDP(&pointTopLeft);
  118.         m_sizeOffset = point - pointTopLeft; 
  119.         //设置光标形状为十字状
  120.         ::SetCursor(::LoadCursor(NULL, IDC_CROSS));
  121.     }
  122. CScrollView::OnLButtonDown(nFlags, point);
  123. }
  124. void CMoveSelObView::OnLButtonUp(UINT nFlags, CPoint point) 
  125. {
  126. // TODO: Add your message handler code here and/or call default
  127. if (m_bCaptured) {
  128. //释放鼠标捕捉
  129.         ::ReleaseCapture();
  130.         m_bCaptured = FALSE;
  131.     }
  132. CScrollView::OnLButtonUp(nFlags, point);
  133. }
  134. void CMoveSelObView::OnMouseMove(UINT nFlags, CPoint point) 
  135. {
  136.     if (m_bCaptured) {
  137. //如果有图形被选中,并且鼠标被捕捉则更新图形位置
  138.         CClientDC dc(this);
  139.         OnPrepareDC(&dc);
  140.         CRect rectOld(m_pointTopLeft, m_sizeEllipse);
  141.         dc.LPtoDP(rectOld);
  142. //擦除老位置的图形
  143.         InvalidateRect(rectOld, TRUE);
  144.         m_pointTopLeft = point - m_sizeOffset;
  145.         dc.DPtoLP(&m_pointTopLeft);
  146. //在新位置画图形
  147.         CRect rectNew(m_pointTopLeft, m_sizeEllipse);
  148.         dc.LPtoDP(rectNew);
  149.         InvalidateRect(rectNew, TRUE);
  150.     }
  151. CScrollView::OnMouseMove(nFlags, point);
  152. }