home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK8 / MFC / SAMPLES / MDI / HELLO.CP$ / hello
Encoding:
Text File  |  1992-03-18  |  4.4 KB  |  184 lines

  1. // hello.cpp : Defines the class behaviors for the Hello child window.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and Microsoft
  9. // QuickHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12. //
  13.  
  14. #include "mdi.h"
  15.  
  16. static COLORREF clrTextArray[] = { RGB (0,   0, 0), RGB (255, 0,   0),
  17.                                    RGB (0, 255, 0), RGB (  0, 0, 255),
  18.                                    RGB (255, 255, 255) } ;
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CHelloWnd Member Functions
  22.  
  23.  
  24. BEGIN_MESSAGE_MAP(CHelloWnd, CMDIChildWnd)
  25.     ON_WM_CREATE()
  26.     ON_COMMAND(IDM_BLACK, OnColor)
  27.     ON_COMMAND(IDM_RED, OnColor)
  28.     ON_COMMAND(IDM_GREEN, OnColor)
  29.     ON_COMMAND(IDM_BLUE, OnColor)
  30.     ON_COMMAND(IDM_WHITE, OnColor)
  31.     ON_COMMAND(IDM_CUSTOM, OnColor)
  32.     ON_WM_PAINT()
  33.     ON_WM_MDIACTIVATE()
  34.     ON_WM_DESTROY()
  35. END_MESSAGE_MAP()
  36.  
  37. // Create:
  38. // Register a custom WndClass and create a window.
  39. // This must be done because CHelloWnd has a custom icon.
  40. // 
  41. BOOL CHelloWnd::Create(LPCSTR szTitle, LONG style /* = 0 */,
  42.     const RECT& rect /* = rectDefault */,
  43.     CMDIFrameWnd* parent /* = NULL */)
  44. {
  45.     const char* pszHelloClass = 
  46.           AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,
  47.             LoadCursor(NULL, IDC_ARROW), 
  48.             (HBRUSH) (COLOR_WINDOW+1),
  49.             LoadIcon(AfxGetInstanceHandle(), "hello"));
  50.  
  51.     return CMDIChildWnd::Create(pszHelloClass, szTitle, style, rect, parent);
  52. }
  53.  
  54.  
  55. // Constructor
  56. // Do minimum initialization
  57. //
  58. CHelloWnd::CHelloWnd()
  59. {
  60.     m_nColor = 0;
  61.     m_clrText = 0;
  62.     m_pMenuCurrent = NULL;
  63.     m_bWindowActive = FALSE;
  64. }
  65.  
  66. // Destructor:
  67. // Clean up menu iff Windows won't
  68. //
  69. CHelloWnd::~CHelloWnd()
  70.     if (m_bWindowActive)
  71.     {
  72.         // Suppress Foundation DestroyMenu done in CMenu destructor 
  73.         // (Windows takes care of menu cleanup for the active window)
  74.         //
  75.         m_pMenuCurrent->Detach();
  76.     }
  77. }
  78.  
  79. // OnCreate:
  80. // Set up colors -- this also could have been done in constructor
  81. //
  82. int CHelloWnd::OnCreate(LPCREATESTRUCT /* p */)
  83. {
  84.     m_nColor = IDM_BLACK;
  85.     m_clrText = RGB (0, 0, 0);
  86.  
  87.     return 0;
  88. }
  89.  
  90. // OnDestroy:
  91. // Notify app main MDI frame window of destruction so it may
  92. // do some cleanup.  Note: this uses a custom message -- see
  93. // mdi.h and mdi.cpp for the custom message handler
  94. //
  95. void CHelloWnd::OnDestroy()
  96. {
  97.     m_pMDIFrameWnd->SendMessage(WM_CHILDDESTROY, (UINT)m_hWnd, 0);
  98. }
  99.  
  100. // OnColor:
  101. // Change menu checkmarks to indicate the newly selected color.
  102. //
  103. void CHelloWnd::OnColor()
  104. {
  105.     CMenu* pMenu = m_pMDIFrameWnd->GetMenu();
  106.     pMenu->CheckMenuItem(m_nColor, MF_UNCHECKED);
  107.  
  108.     m_nColor = GetCurrentMessage()->wParam;
  109.     pMenu->CheckMenuItem(m_nColor, MF_CHECKED);
  110.  
  111.     if (m_nColor != IDM_CUSTOM)
  112.         m_clrText = clrTextArray[m_nColor - IDM_BLACK];
  113.     else
  114.     {
  115.         CColorDialog dlgColor(m_clrText);
  116.         if (dlgColor.DoModal() == IDOK)
  117.             m_clrText = dlgColor.GetColor();
  118.     }
  119.  
  120.     // Force the client area text to be repainted in the new color
  121.  
  122.     Invalidate();
  123. }
  124.  
  125. // OnPaint:
  126. // Draw a string in the center of the client area.
  127. //
  128. void CHelloWnd::OnPaint()
  129. {
  130.     CPaintDC dc(this);
  131.     CRect rect;
  132.  
  133.     dc.SetTextColor(m_clrText);
  134.     dc.SetBkColor(::GetSysColor(COLOR_WINDOW));
  135.     GetClientRect(rect);
  136.     dc.DrawText("Hello, World!", -1, rect, 
  137.         DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  138. }
  139.  
  140.  
  141. // OnMDIActivate:
  142. // This window is being activated or deactivated.  
  143. // Switch the menus appropriately.
  144. //
  145. void CHelloWnd::OnMDIActivate(BOOL bActivate, CWnd* /*pActive*/, 
  146.                               CWnd* /*pDeActive*/)
  147. {
  148.     CMDIFrameWnd* pFrame = m_pMDIFrameWnd;
  149.     CMenu* pWinPopupMenu = NULL;
  150.     CMenu* pMenu = NULL;
  151.  
  152.     m_bWindowActive = bActivate;
  153.  
  154.     if (bActivate)
  155.     {
  156.         pMenu = new CMenu;
  157.         pMenu->LoadMenu("MdiMenuHello");
  158.         pWinPopupMenu = pMenu->GetSubMenu(HELLO_MENU_POS);
  159.  
  160.         CMenu* pLastMenu = pFrame->MDISetMenu(pMenu, pWinPopupMenu);
  161.         pLastMenu->DestroyMenu();
  162.  
  163.         pMenu->CheckMenuItem(m_nColor, MF_CHECKED);
  164.  
  165.         delete m_pMenuCurrent;
  166.         m_pMenuCurrent = pMenu;
  167.     }
  168.     else    
  169.     {
  170.         pMenu = new CMenu;
  171.         pMenu->LoadMenu("MdiMenuInit");
  172.         pWinPopupMenu = pMenu->GetSubMenu(INIT_MENU_POS);
  173.  
  174.         CMenu* pLastMenu = pFrame->MDISetMenu(pMenu, pWinPopupMenu);
  175.         pLastMenu->DestroyMenu();
  176.  
  177.         delete m_pMenuCurrent;
  178.         m_pMenuCurrent = pMenu;
  179.     }
  180.  
  181.     pFrame->DrawMenuBar();
  182. }
  183.