home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c05 / menus3 / menus3view.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.2 KB  |  142 lines

  1. // Menus3View.cpp : implementation of the CMenus3View class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Menus3.h"
  6.  
  7. #include "Menus3Doc.h"
  8. #include "Menus3View.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. const COLORREF BLACK=RGB(0,0,0);
  17. const COLORREF RED=RGB(255,0,0);
  18. const COLORREF GREEN=RGB(0,255,0);
  19. const COLORREF BLUE=RGB(0,0,255);
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CMenus3View
  23.  
  24. IMPLEMENT_DYNCREATE(CMenus3View, CView)
  25.  
  26. BEGIN_MESSAGE_MAP(CMenus3View, CView)
  27.     //{{AFX_MSG_MAP(CMenus3View)
  28.     ON_COMMAND(ID_COLORS_BLACK, OnColors)
  29.     ON_COMMAND(ID_COLORS_BLUE, OnColors)
  30.     ON_COMMAND(ID_COLORS_GREEN, OnColors)
  31.     ON_COMMAND(ID_COLORS_RED, OnColors)
  32.     ON_UPDATE_COMMAND_UI(ID_COLORS_BLACK, OnUpdateColors)
  33.     ON_UPDATE_COMMAND_UI(ID_COLORS_BLUE, OnUpdateColors)
  34.     ON_UPDATE_COMMAND_UI(ID_COLORS_GREEN, OnUpdateColors)
  35.     ON_UPDATE_COMMAND_UI(ID_COLORS_RED, OnUpdateColors)
  36.     //}}AFX_MSG_MAP
  37. END_MESSAGE_MAP()
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CMenus3View construction/destruction
  41.  
  42. CMenus3View::CMenus3View()
  43. {
  44.     // TODO: add construction code here
  45.  
  46. }
  47.  
  48. CMenus3View::~CMenus3View()
  49. {
  50. }
  51.  
  52. BOOL CMenus3View::PreCreateWindow(CREATESTRUCT& cs)
  53. {
  54.     return CView::PreCreateWindow(cs);
  55. }
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CMenus3View drawing
  59.  
  60. void CMenus3View::OnDraw(CDC* pDC)
  61. {
  62.     CMenus3Doc* pDoc = GetDocument();
  63.     ASSERT_VALID(pDoc);
  64.  
  65.     CRect r;
  66.     GetClientRect(&r);
  67.     int x = r.right / 2, y = r.bottom / 2;
  68.  
  69.     pDC->SetTextColor(pDoc->GetColor());
  70.     pDC->SetTextAlign (TA_CENTER | TA_BASELINE);
  71.     pDC->TextOut (x, y, pDoc->GetPhrase());
  72. }
  73.  
  74. ////////////////////////////////////////////////////////////////////////////
  75. // CMenus3View diagnostics
  76.  
  77. #ifdef _DEBUG
  78. void CMenus3View::AssertValid() const
  79. {
  80.     CView::AssertValid();
  81. }
  82.  
  83. void CMenus3View::Dump(CDumpContext& dc) const
  84. {
  85.     CView::Dump(dc);
  86. }
  87.  
  88. CMenus3Doc* CMenus3View::GetDocument() // non-debug version is inline
  89. {
  90.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMenus3Doc)));
  91.     return (CMenus3Doc*)m_pDocument;
  92. }
  93. #endif //_DEBUG
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CMenus3View message handlers
  97.  
  98. void CMenus3View::OnColors()
  99. {
  100.     CMenus3Doc* pDoc = GetDocument();
  101.  
  102.     // Since all 4 menu selections end up here (see message map)
  103.     // we need to know how we got here.
  104.     MSG const * pMsg = GetCurrentMessage();
  105.  
  106.     switch (LOWORD(pMsg->wParam))
  107.     {
  108.         case ID_COLORS_RED:
  109.             pDoc->SetColor(RED);
  110.             break;
  111.         case ID_COLORS_GREEN:
  112.             pDoc->SetColor(GREEN);
  113.             break;
  114.         case ID_COLORS_BLUE:
  115.             pDoc->SetColor(BLUE);
  116.             break;
  117.         default:
  118.             pDoc->SetColor(BLACK);
  119.     }
  120.     pDoc->UpdateAllViews(NULL);
  121. }
  122.  
  123. void CMenus3View::OnUpdateColors(CCmdUI* pCmdUI) 
  124. {
  125.     CMenus3Doc* pDoc = GetDocument();
  126.  
  127.     switch (pCmdUI->m_nID)
  128.     {
  129.         case ID_COLORS_RED:
  130.             pCmdUI->Enable(pDoc->GetColor() != RED);
  131.             break;
  132.         case ID_COLORS_GREEN:
  133.             pCmdUI->Enable(pDoc->GetColor() != GREEN);
  134.             break;
  135.         case ID_COLORS_BLUE:
  136.             pCmdUI->Enable(pDoc->GetColor() != BLUE);
  137.             break;
  138.         default:
  139.             pCmdUI->Enable(pDoc->GetColor() != BLACK);
  140.     }
  141. }
  142.