home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual C++ 4 (2nd Edition) / VisualC4.ISO / minidrw1 / minidrvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  3.7 KB  |  151 lines

  1. // MiniDrVw.cpp : implementation of the CMiniDrawView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MiniDraw.h"
  6.  
  7. #include "MiniDDoc.h"
  8. #include "MiniDrVw.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CMiniDrawView
  18.  
  19. IMPLEMENT_DYNCREATE(CMiniDrawView, CView)
  20.  
  21. BEGIN_MESSAGE_MAP(CMiniDrawView, CView)
  22.    //{{AFX_MSG_MAP(CMiniDrawView)
  23.    ON_WM_LBUTTONDOWN()
  24.    ON_WM_MOUSEMOVE()
  25.    ON_WM_LBUTTONUP()
  26.    //}}AFX_MSG_MAP
  27. END_MESSAGE_MAP()
  28.  
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CMiniDrawView construction/destruction
  31.  
  32. CMiniDrawView::CMiniDrawView()
  33. {
  34.    // TODO: add construction code here
  35.  
  36.    m_Dragging = 0;
  37.    m_HCross = AfxGetApp ()->LoadStandardCursor (IDC_CROSS);
  38. }
  39.  
  40. CMiniDrawView::~CMiniDrawView()
  41. {
  42. }
  43.  
  44. BOOL CMiniDrawView::PreCreateWindow(CREATESTRUCT& cs)
  45. {
  46.    // TODO: Modify the Window class or styles here by modifying
  47.    //  the CREATESTRUCT cs
  48.  
  49.    m_ClassName = AfxRegisterWndClass
  50.       (CS_HREDRAW | CS_VREDRAW,                // class styles
  51.       0,                                       // no cursor
  52.       (HBRUSH)::GetStockObject (WHITE_BRUSH),  // assign white background brush
  53.       0);                                      // no icon
  54.    cs.lpszClass = m_ClassName;      
  55.  
  56.    return CView::PreCreateWindow(cs);
  57. }
  58.    
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CMiniDrawView drawing
  61.  
  62. void CMiniDrawView::OnDraw(CDC* pDC)
  63. {
  64.    CMiniDrawDoc* pDoc = GetDocument();
  65.    ASSERT_VALID(pDoc);
  66.  
  67.    // TODO: add draw code for native data here
  68. }
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CMiniDrawView diagnostics
  72.  
  73. #ifdef _DEBUG
  74. void CMiniDrawView::AssertValid() const
  75. {
  76.    CView::AssertValid();
  77. }
  78.  
  79. void CMiniDrawView::Dump(CDumpContext& dc) const
  80. {
  81.    CView::Dump(dc);
  82. }
  83.  
  84. CMiniDrawDoc* CMiniDrawView::GetDocument() // non-debug version is inline
  85. {
  86.    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMiniDrawDoc)));
  87.    return (CMiniDrawDoc*)m_pDocument;
  88. }
  89. #endif //_DEBUG
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CMiniDrawView message handlers
  93.  
  94. void CMiniDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
  95. {
  96.    // TODO: Add your message handler code here and/or call default
  97.  
  98.    m_PointOrigin = point;
  99.    m_PointOld = point;
  100.    SetCapture ();
  101.    m_Dragging = 1;
  102.    
  103.    RECT Rect;
  104.    GetClientRect (&Rect);
  105.    ClientToScreen (&Rect);
  106.    ::ClipCursor (&Rect);
  107.  
  108.    CView::OnLButtonDown(nFlags, point);
  109. }
  110.  
  111. void CMiniDrawView::OnMouseMove(UINT nFlags, CPoint point) 
  112. {
  113.    // TODO: Add your message handler code here and/or call default
  114.    
  115.    ::SetCursor (m_HCross);
  116.  
  117.    if (m_Dragging)
  118.       {
  119.       CClientDC ClientDC (this);
  120.       ClientDC.SetROP2 (R2_NOT);
  121.       ClientDC.MoveTo (m_PointOrigin);
  122.       ClientDC.LineTo (m_PointOld);
  123.       ClientDC.MoveTo (m_PointOrigin);
  124.       ClientDC.LineTo (point);
  125.       m_PointOld = point;
  126.       } 
  127.  
  128.    CView::OnMouseMove(nFlags, point);
  129. }
  130.  
  131. void CMiniDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
  132. {
  133.    // TODO: Add your message handler code here and/or call default
  134.    
  135.    if (m_Dragging)
  136.       {
  137.       m_Dragging = 0;
  138.       ::ReleaseCapture ();
  139.       ::ClipCursor (NULL);
  140.       CClientDC ClientDC (this);
  141.       ClientDC.SetROP2 (R2_NOT);
  142.       ClientDC.MoveTo (m_PointOrigin);
  143.       ClientDC.LineTo (m_PointOld);
  144.       ClientDC.SetROP2 (R2_COPYPEN);
  145.       ClientDC.MoveTo (m_PointOrigin);
  146.       ClientDC.LineTo (point);      
  147.       }   
  148.  
  149.    CView::OnLButtonUp(nFlags, point);
  150. }
  151.