home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c07 / scroll / scroview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  2.9 KB  |  116 lines

  1. // ScroView.cpp : implementation of the CMyScrollView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Scroll.h"
  6.  
  7. #include "ScrolDoc.h"
  8. #include "ScroView.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. // CMyScrollView
  18.  
  19. IMPLEMENT_DYNCREATE(CMyScrollView, CScrollView)
  20.  
  21. BEGIN_MESSAGE_MAP(CMyScrollView, CScrollView)
  22.     //{{AFX_MSG_MAP(CMyScrollView)
  23.     ON_WM_LBUTTONDOWN()
  24.     //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CMyScrollView construction/destruction
  29.  
  30. CMyScrollView::CMyScrollView()
  31. {
  32. }
  33.  
  34. CMyScrollView::~CMyScrollView()
  35. {
  36. }
  37.  
  38. BOOL CMyScrollView::PreCreateWindow(CREATESTRUCT& cs)
  39. {
  40.     return CScrollView::PreCreateWindow(cs);
  41. }
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CMyScrollView drawing
  45.  
  46. void CMyScrollView::OnDraw(CDC* pDC)
  47. {
  48.     CScrollDoc * pDoc = GetDocument();
  49.     CPoint pt = pDoc->GetLocation();
  50.     pDC->TextOut(pt.x, pt.y, pDoc->GetPhrase());
  51. }
  52.  
  53. void CMyScrollView::OnInitialUpdate()
  54. {
  55.     CScrollView::OnInitialUpdate();
  56.     CSize sizeTotal;
  57.     
  58.     sizeTotal.cx = sizeTotal.cy = 200;
  59.     SetScrollSizes(MM_TEXT, sizeTotal);
  60.  
  61.     GetParentFrame()->RecalcLayout();
  62.     ResizeParentToFit(FALSE);
  63. }
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CMyScrollView diagnostics
  67.  
  68. #ifdef _DEBUG
  69. void CMyScrollView::AssertValid() const
  70. {
  71.     CScrollView::AssertValid();
  72. }
  73.  
  74. void CMyScrollView::Dump(CDumpContext& dc) const
  75. {
  76.     CScrollView::Dump(dc);
  77. }
  78.  
  79. CScrollDoc* CMyScrollView::GetDocument() // non-debug version is inline
  80. {
  81.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CScrollDoc)));
  82.     return (CScrollDoc*)m_pDocument;
  83. }
  84. #endif //_DEBUG
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CMyScrollView message handlers
  88.  
  89. // The CPoint argument represents the location of the mouse in
  90. // device (physical) coordinates. OnDraw, which will be called
  91. // as a result of the call to Invalidate at the end of this
  92. // function, always draws to logical space. Therefore the CPoint
  93. // argument must be converted to logical coordinates before storing
  94. // the new location into the document object. To show the necessity
  95. // of doing this, comment out the first 3 lines of this function,
  96. // re-compile and execute.
  97. void CMyScrollView::OnLButtonDown(UINT nFlags, CPoint point) 
  98. {
  99.     // Create a client dc that points to the current view
  100.     CClientDC dc(this);
  101.  
  102.     // Given that this is a scroll view, call OnPrepareDC to
  103.     // adjust attributes of the device context appropriately.
  104.     OnPrepareDC(&dc);
  105.  
  106.     dc.DPtoLP(&point);
  107.  
  108.     // Store the new location in the document and invalidate the view.
  109.     CScrollDoc * pDoc = GetDocument();
  110.     pDoc->SetLocation(point);
  111.     
  112.     Invalidate();
  113.     
  114.     CScrollView::OnLButtonDown(nFlags, point);
  115. }
  116.