home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual C++ 4 (2nd Edition) / VisualC4.ISO / minidrw3 / minidrvw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  3.9 KB  |  158 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.    int Index = pDoc->GetNumLines ();
  70.    while (Index--)
  71.       pDoc->GetLine (Index)->Draw (pDC);      
  72. }
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CMiniDrawView diagnostics
  76.  
  77. #ifdef _DEBUG
  78. void CMiniDrawView::AssertValid() const
  79. {
  80.    CView::AssertValid();
  81. }
  82.  
  83. void CMiniDrawView::Dump(CDumpContext& dc) const
  84. {
  85.    CView::Dump(dc);
  86. }
  87.  
  88. CMiniDrawDoc* CMiniDrawView::GetDocument() // non-debug version is inline
  89. {
  90.    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMiniDrawDoc)));
  91.    return (CMiniDrawDoc*)m_pDocument;
  92. }
  93. #endif //_DEBUG
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CMiniDrawView message handlers
  97.  
  98. void CMiniDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
  99. {
  100.    // TODO: Add your message handler code here and/or call default
  101.  
  102.    m_PointOrigin = point;
  103.    m_PointOld = point;
  104.    SetCapture ();
  105.    m_Dragging = 1;
  106.    
  107.    RECT Rect;
  108.    GetClientRect (&Rect);
  109.    ClientToScreen (&Rect);
  110.    ::ClipCursor (&Rect);
  111.  
  112.    CView::OnLButtonDown(nFlags, point);
  113. }
  114.  
  115. void CMiniDrawView::OnMouseMove(UINT nFlags, CPoint point) 
  116. {
  117.    // TODO: Add your message handler code here and/or call default
  118.    
  119.    ::SetCursor (m_HCross);
  120.  
  121.    if (m_Dragging)
  122.       {
  123.       CClientDC ClientDC (this);
  124.       ClientDC.SetROP2 (R2_NOT);
  125.       ClientDC.MoveTo (m_PointOrigin);
  126.       ClientDC.LineTo (m_PointOld);
  127.       ClientDC.MoveTo (m_PointOrigin);
  128.       ClientDC.LineTo (point);
  129.       m_PointOld = point;
  130.       } 
  131.  
  132.    CView::OnMouseMove(nFlags, point);
  133. }
  134.  
  135. void CMiniDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
  136. {
  137.    // TODO: Add your message handler code here and/or call default
  138.    
  139.    if (m_Dragging)
  140.       {
  141.       m_Dragging = 0;
  142.       ::ReleaseCapture ();
  143.       ::ClipCursor (NULL);
  144.       CClientDC ClientDC (this);
  145.       ClientDC.SetROP2 (R2_NOT);
  146.       ClientDC.MoveTo (m_PointOrigin);
  147.       ClientDC.LineTo (m_PointOld);
  148.       ClientDC.SetROP2 (R2_COPYPEN);
  149.       ClientDC.MoveTo (m_PointOrigin);
  150.       ClientDC.LineTo (point);      
  151.  
  152.       CMiniDrawDoc* PDoc = GetDocument();
  153.       PDoc->AddLine (m_PointOrigin.x, m_PointOrigin.y, point.x, point.y);
  154.       }   
  155.  
  156.    CView::OnLButtonUp(nFlags, point);
  157. }
  158.