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

  1. // MDIView.cpp : implementation of the CMDIAppView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MDIApp.h"
  6.  
  7. #include "MDIDoc.h"
  8. #include "MDIView.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. // CMDIAppView
  18.  
  19. IMPLEMENT_DYNCREATE(CMDIAppView, CView)
  20.  
  21. BEGIN_MESSAGE_MAP(CMDIAppView, CView)
  22.     //{{AFX_MSG_MAP(CMDIAppView)
  23.     //}}AFX_MSG_MAP
  24.     // Standard printing commands
  25.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  26.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  27.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  28.     ON_COMMAND_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnColors)
  29.     ON_UPDATE_COMMAND_UI_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnUpdateColors)
  30. END_MESSAGE_MAP()
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CMDIAppView construction/destruction
  34.  
  35. CMDIAppView::CMDIAppView()
  36. {
  37. }
  38.  
  39. CMDIAppView::~CMDIAppView()
  40. {
  41. }
  42.  
  43. BOOL CMDIAppView::PreCreateWindow(CREATESTRUCT& cs)
  44. {
  45.     return CView::PreCreateWindow(cs);
  46. }
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CMDIAppView drawing
  50.  
  51. void CMDIAppView::OnDraw(CDC* pDC)
  52. {
  53.     CMDIAppDoc* pDoc = GetDocument();
  54.     ASSERT_VALID(pDoc);
  55.  
  56.     CRect r;
  57.     GetClientRect(&r);
  58.     int x = r.right / 2, y = r.bottom / 2;
  59.  
  60.     pDC->SetTextColor(pDoc->GetColor());
  61.     pDC->SetTextAlign (TA_CENTER | TA_BASELINE);
  62.     pDC->TextOut (x, y, pDoc->GetPhrase());
  63. }
  64.  
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CMDIAppView printing
  67.  
  68. BOOL CMDIAppView::OnPreparePrinting(CPrintInfo* pInfo)
  69. {
  70.     // default preparation
  71.     return DoPreparePrinting(pInfo);
  72. }
  73.  
  74. void CMDIAppView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  75. {
  76.     // TODO: add extra initialization before printing
  77. }
  78.  
  79. void CMDIAppView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  80. {
  81.     // TODO: add cleanup after printing
  82. }
  83.  
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CMDIAppView diagnostics
  86.  
  87. #ifdef _DEBUG
  88. void CMDIAppView::AssertValid() const
  89. {
  90.     CView::AssertValid();
  91. }
  92.  
  93. void CMDIAppView::Dump(CDumpContext& dc) const
  94. {
  95.     CView::Dump(dc);
  96. }
  97.  
  98. CMDIAppDoc* CMDIAppView::GetDocument() // non-debug version is inline
  99. {
  100.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMDIAppDoc)));
  101.     return (CMDIAppDoc*)m_pDocument;
  102. }
  103. #endif //_DEBUG
  104.  
  105. /////////////////////////////////////////////////////////////////////////////
  106. // CMDIAppView message handlers
  107.  
  108. void CMDIAppView::OnColors(UINT nID)
  109. {
  110.     CMDIAppDoc* pDoc = GetDocument();
  111.     ASSERT_VALID(pDoc);
  112.  
  113.     pDoc->SetColor(IDtoColorRef(nID));
  114.     
  115.     // If this were an SDI app then you'd call
  116.         // Invalidate();
  117.     // Since it's an MDI app, you have to call
  118.     pDoc->UpdateAllViews(NULL);
  119. }
  120.  
  121. void CMDIAppView::OnUpdateColors(CCmdUI* pCmdUI)
  122. {
  123.     CMDIAppDoc* pDoc = GetDocument();
  124.     ASSERT_VALID(pDoc);
  125.  
  126.     pCmdUI->SetCheck(pDoc->GetColor() == IDtoColorRef(pCmdUI->m_nID));
  127. }
  128.  
  129. const COLORREF BLACK=RGB(0,0,0);
  130. const COLORREF RED=RGB(255,0,0);
  131. const COLORREF GREEN=RGB(0,255,0);
  132. const COLORREF BLUE=RGB(0,0,255);
  133.  
  134. // This function converts one of the 4 Command IDs to a COLORREF value.
  135. COLORREF CMDIAppView::IDtoColorRef(int nID)
  136. {
  137.     switch (nID)
  138.     {
  139.         case ID_COLORS_RED:
  140.             return RED;
  141.         case ID_COLORS_GREEN:
  142.             return GREEN;        
  143.         case ID_COLORS_BLUE:
  144.             return BLUE;
  145.         default:
  146.             return BLACK;        
  147.     }
  148. }
  149.