home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / mdidocvw / hellovw.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.9 KB  |  266 lines

  1. // HelloVw.cpp : implementation of the CHelloView class
  2. //
  3.  
  4. // This is a part of the Microsoft Foundation Classes C++ library.
  5. // Copyright (C) 1992-1998 Microsoft Corporation
  6. // All rights reserved.
  7. //
  8. // This source code is only intended as a supplement to the
  9. // Microsoft Foundation Classes Reference and related
  10. // electronic documentation provided with the library.
  11. // See these sources for detailed information regarding the
  12. // Microsoft Foundation Classes product.
  13.  
  14. #include "stdafx.h"
  15. #include "MDI.h"
  16.  
  17. #include "HelloDoc.h"
  18. #include "HelloVw.h"
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CHelloView
  28.  
  29. IMPLEMENT_DYNCREATE(CHelloView, CView)
  30.  
  31. BEGIN_MESSAGE_MAP(CHelloView, CView)
  32.     //{{AFX_MSG_MAP(CHelloView)
  33.     ON_UPDATE_COMMAND_UI(ID_BLUE, OnUpdateBlue)
  34.     ON_UPDATE_COMMAND_UI(ID_GREEN, OnUpdateGreen)
  35.     ON_UPDATE_COMMAND_UI(ID_RED, OnUpdateRed)
  36.     ON_UPDATE_COMMAND_UI(ID_WHITE, OnUpdateWhite)
  37.     ON_UPDATE_COMMAND_UI(ID_BLACK, OnUpdateBlack)
  38.     ON_COMMAND(ID_CUSTOM, OnCustom)
  39.     ON_UPDATE_COMMAND_UI(ID_CUSTOM, OnUpdateCustom)
  40.     ON_COMMAND(ID_BLACK, OnColor)
  41.     ON_COMMAND(ID_BLUE, OnColor)
  42.     ON_COMMAND(ID_GREEN, OnColor)
  43.     ON_COMMAND(ID_RED, OnColor)
  44.     ON_COMMAND(ID_WHITE, OnColor)
  45.     //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CHelloView construction/destruction
  50.  
  51. CHelloView::CHelloView()
  52. {
  53. }
  54.  
  55. CHelloView::~CHelloView()
  56. {
  57. }
  58.  
  59. BOOL CHelloView::PreCreateWindow(CREATESTRUCT& cs)
  60. {
  61.     return CView::PreCreateWindow(cs);
  62. }
  63.  
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CHelloView drawing
  66.  
  67. void CHelloView::OnDraw(CDC* pDC)
  68. {
  69.     CHelloDoc* pDoc = GetDocument();
  70.     ASSERT_VALID(pDoc);
  71.  
  72.     CRect rect;
  73.     COLORREF clr = pDoc->m_clrText;
  74.     CString tmpStr = pDoc->m_str;
  75.  
  76.     pDC->SetTextColor(clr);
  77.     pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
  78.     GetClientRect(rect);
  79.     pDC->DrawText(tmpStr, -1, rect,
  80.         DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  81. }
  82.  
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CHelloView diagnostics
  85.  
  86. #ifdef _DEBUG
  87. void CHelloView::AssertValid() const
  88. {
  89.     CView::AssertValid();
  90. }
  91.  
  92. void CHelloView::Dump(CDumpContext& dc) const
  93. {
  94.     CView::Dump(dc);
  95. }
  96.  
  97. CHelloDoc* CHelloView::GetDocument() // non-debug version is inline
  98. {
  99.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHelloDoc)));
  100.     return (CHelloDoc*)m_pDocument;
  101. }
  102. #endif //_DEBUG
  103.  
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CHelloView message handlers
  106.  
  107. // Update handlers for each color
  108. void CHelloView::OnUpdateBlue(CCmdUI* pCmdUI)
  109. {
  110.     CHelloDoc* pDoc = GetDocument();
  111.     ASSERT_VALID(pDoc);
  112.  
  113.     pCmdUI->SetCheck(pDoc->m_bBlue);
  114. }
  115.  
  116. void CHelloView::OnUpdateGreen(CCmdUI* pCmdUI)
  117. {
  118.     CHelloDoc* pDoc = GetDocument();
  119.     ASSERT_VALID(pDoc);
  120.  
  121.     pCmdUI->SetCheck(pDoc->m_bGreen);
  122. }
  123.  
  124. void CHelloView::OnUpdateRed(CCmdUI* pCmdUI)
  125. {
  126.     CHelloDoc* pDoc = GetDocument();
  127.     ASSERT_VALID(pDoc);
  128.  
  129.     pCmdUI->SetCheck(pDoc->m_bRed);
  130. }
  131.  
  132. void CHelloView::OnUpdateWhite(CCmdUI* pCmdUI)
  133. {
  134.     CHelloDoc* pDoc = GetDocument();
  135.     ASSERT_VALID(pDoc);
  136.  
  137.     pCmdUI->SetCheck(pDoc->m_bWhite);
  138. }
  139.  
  140. void CHelloView::OnUpdateBlack(CCmdUI* pCmdUI)
  141. {
  142.     CHelloDoc* pDoc = GetDocument();
  143.     ASSERT_VALID(pDoc);
  144.  
  145.     pCmdUI->SetCheck(pDoc->m_bBlack);
  146. }
  147.  
  148. void CHelloView::MixColors()
  149. {
  150.     CHelloDoc* pDoc = GetDocument();
  151.     ASSERT_VALID(pDoc);
  152.  
  153.     COLORREF tmpClr;
  154.     int r, g, b;
  155.     BOOL bSetColor;
  156.  
  157.     // Determine which colors are currently chosen.
  158.  
  159.     bSetColor = pDoc->m_bRed || pDoc->m_bGreen || pDoc->m_bBlue
  160.         || pDoc->m_bWhite || pDoc->m_bBlack;
  161.  
  162.     // If the current color is custom, ignore mix request.
  163.  
  164.     if(!bSetColor && pDoc->m_bCustom)
  165.         return;
  166.  
  167.     // Set color value to black and then add the necessary colors.
  168.  
  169.     r = g = b = 0;
  170.  
  171.     if(pDoc->m_bRed)
  172.      r = 255;
  173.     if(pDoc->m_bGreen)
  174.      g = 255;
  175.     if(pDoc->m_bBlue)
  176.      b = 255;
  177.     tmpClr = RGB(r, g, b);
  178.  
  179. // NOTE: Because a simple algorithm is used to mix colors
  180. // if the current selection contains black or white, the
  181. // result will be black or white; respectively. This is due
  182. // to the additive method for mixing the colors.
  183.  
  184.     if(pDoc->m_bBlack)
  185.      tmpClr = RGB(0, 0, 0);
  186.  
  187.     if(pDoc->m_bWhite)
  188.      tmpClr = RGB(255, 255, 255);
  189.  
  190.     // Once the color has been determined, update document
  191.     // data, and force repaint of all views.
  192.  
  193.     if(!bSetColor)
  194.         pDoc->m_bBlack = TRUE;
  195.     pDoc->m_clrText = tmpClr;
  196.     pDoc->m_bCustom = FALSE;
  197.     pDoc->UpdateAllViews(NULL);
  198. }
  199.  
  200. void CHelloView::OnCustom()
  201. {
  202.     CHelloDoc* pDoc = GetDocument();
  203.     ASSERT_VALID(pDoc);
  204.  
  205.     CColorDialog dlgColor(pDoc->m_clrText);
  206.     if (dlgColor.DoModal() == IDOK)
  207.     {
  208.         pDoc->m_clrText = dlgColor.GetColor();
  209.         pDoc->ClearAllColors();
  210.         pDoc->m_bCustom = TRUE;
  211.         pDoc->UpdateAllViews(NULL);
  212.     }
  213. }
  214.  
  215. void CHelloView::OnUpdateCustom(CCmdUI* pCmdUI)
  216. {
  217.     CHelloDoc* pDoc = GetDocument();
  218.     ASSERT_VALID(pDoc);
  219.  
  220.     pCmdUI->SetCheck(pDoc->m_bCustom);
  221. }
  222.  
  223. void CHelloView::OnColor()
  224. {
  225.     CHelloDoc* pDoc = GetDocument();
  226.     ASSERT_VALID(pDoc);
  227.  
  228.     UINT m_nIDColor;
  229.  
  230.     m_nIDColor = LOWORD(GetCurrentMessage()->wParam);
  231.  
  232. // Determines the color being modified
  233. // and then updates the color state
  234.  
  235.     switch(m_nIDColor)
  236.     {
  237.      case ID_BLACK:
  238.         pDoc->ClearAllColors();
  239.         pDoc->m_bBlack = !(pDoc->m_bBlack);
  240.         break;
  241.      case ID_WHITE:
  242.         pDoc->ClearAllColors();
  243.         pDoc->m_bWhite = !(pDoc->m_bWhite);
  244.         break;
  245.      case ID_RED:
  246.         pDoc->m_bRed = !(pDoc->m_bRed);
  247.         pDoc->m_bBlack = FALSE;
  248.         pDoc->m_bWhite = FALSE;
  249.         break;
  250.      case ID_GREEN:
  251.         pDoc->m_bGreen = !(pDoc->m_bGreen);
  252.         pDoc->m_bBlack = FALSE;
  253.         pDoc->m_bWhite = FALSE;
  254.         break;
  255.      case ID_BLUE:
  256.         pDoc->m_bBlue = !(pDoc->m_bBlue);
  257.         pDoc->m_bBlack = FALSE;
  258.         pDoc->m_bWhite = FALSE;
  259.         break;
  260.      default:
  261.          AfxMessageBox(IDS_UNKCOLOR);
  262.          return;
  263.     }
  264.     MixColors();
  265. }
  266.