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

  1. // StatView.cpp : implementation of the CStatusBarsView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "StatBar.h"
  6.  
  7. #include "StatBDoc.h"
  8. #include "StatView.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. // CStatusBarsView
  23.  
  24. IMPLEMENT_DYNCREATE(CStatusBarsView, CView)
  25.  
  26. BEGIN_MESSAGE_MAP(CStatusBarsView, CView)
  27.     //{{AFX_MSG_MAP(CStatusBarsView)
  28.         // NOTE - the ClassWizard will add and remove mapping macros here.
  29.         //    DO NOT EDIT what you see in these blocks of generated code!
  30.     //}}AFX_MSG_MAP
  31.     ON_COMMAND_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnStandardColors)
  32.     ON_UPDATE_COMMAND_UI_RANGE(ID_COLORS_BLACK, ID_COLORS_BLUE, OnUpdateStandardColors)
  33. END_MESSAGE_MAP()
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CStatusBarsView construction/destruction
  37.  
  38. CStatusBarsView::CStatusBarsView()
  39. {
  40. }
  41.  
  42. CStatusBarsView::~CStatusBarsView()
  43. {
  44. }
  45.  
  46. BOOL CStatusBarsView::PreCreateWindow(CREATESTRUCT& cs)
  47. {
  48.     // TODO: Modify the Window class or styles here by modifying
  49.     //  the CREATESTRUCT cs
  50.  
  51.     return CView::PreCreateWindow(cs);
  52. }
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // CStatusBarsView drawing
  56.  
  57. void CStatusBarsView::OnDraw(CDC* pDC)
  58. {
  59.     CStatusBarsDoc* pDoc = GetDocument();
  60.     ASSERT_VALID(pDoc);
  61.  
  62.     CRect r;
  63.     GetClientRect(&r);
  64.     int x = r.right / 2, y = r.bottom / 2;
  65.  
  66.     pDC->SetTextColor(pDoc->GetColor());
  67.     pDC->SetTextAlign (TA_CENTER | TA_BASELINE);
  68.     pDC->TextOut (x, y, pDoc->GetPhrase());
  69. }
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CStatusBarsView diagnostics
  73.  
  74. #ifdef _DEBUG
  75. void CStatusBarsView::AssertValid() const
  76. {
  77.     CView::AssertValid();
  78. }
  79.  
  80. void CStatusBarsView::Dump(CDumpContext& dc) const
  81. {
  82.     CView::Dump(dc);
  83. }
  84.  
  85. CStatusBarsDoc* CStatusBarsView::GetDocument() // non-debug version is inline
  86. {
  87.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStatusBarsDoc)));
  88.     return (CStatusBarsDoc*)m_pDocument;
  89. }
  90. #endif //_DEBUG
  91.  
  92. /////////////////////////////////////////////////////////////////////////////
  93. // CStatusBarsView message handlers
  94. void CStatusBarsView::OnStandardColors(UINT nID)
  95. {
  96.     CStatusBarsDoc* pDoc = GetDocument();
  97.     ASSERT_VALID(pDoc);
  98.  
  99.     pDoc->SetColor(IDtoColorRef(nID));
  100.     Invalidate();
  101. }
  102.  
  103. void CStatusBarsView::OnUpdateStandardColors(CCmdUI* pCmdUI)
  104. {
  105.     CStatusBarsDoc* pDoc = GetDocument();
  106.     ASSERT_VALID(pDoc);
  107.  
  108.     pCmdUI->SetCheck(pDoc->GetColor() == IDtoColorRef(pCmdUI->m_nID));
  109. }
  110.  
  111. // This function converts one of the 4 Command IDs to a COLORREF value.
  112. COLORREF CStatusBarsView::IDtoColorRef(int nID)
  113. {
  114.     switch (nID)
  115.     {
  116.         case ID_COLORS_RED:
  117.             return RED;
  118.         case ID_COLORS_GREEN:
  119.             return GREEN;        
  120.         case ID_COLORS_BLUE:
  121.             return BLUE;
  122.         default:
  123.             return BLACK;        
  124.     }
  125. }
  126.