Annotation of mstools/mfc/samples/mdi/hello.cpp, revision 1.1.1.1

1.1       root        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: { 
                     71:        if (m_bWindowActive)
                     72:        {
                     73:                // Suppress Foundation DestroyMenu done in CMenu destructor 
                     74:                // (Windows takes care of menu cleanup for the active window)
                     75:                //
                     76:                m_pMenuCurrent->Detach();
                     77:        }
                     78: }
                     79: 
                     80: // OnCreate:
                     81: // Set up colors -- this also could have been done in constructor
                     82: //
                     83: int CHelloWnd::OnCreate(LPCREATESTRUCT /* p */)
                     84: {
                     85:        m_nColor = IDM_BLACK;
                     86:        m_clrText = RGB (0, 0, 0);
                     87: 
                     88:        return 0;
                     89: }
                     90: 
                     91: // OnDestroy:
                     92: // Notify app main MDI frame window of destruction so it may
                     93: // do some cleanup.  Note: this uses a custom message -- see
                     94: // mdi.h and mdi.cpp for the custom message handler
                     95: //
                     96: void CHelloWnd::OnDestroy()
                     97: {
                     98:        m_pMDIFrameWnd->SendMessage(WM_CHILDDESTROY, (UINT)m_hWnd, 0);
                     99: }
                    100: 
                    101: // OnColor:
                    102: // Change menu checkmarks to indicate the newly selected color.
                    103: //
                    104: void CHelloWnd::OnColor()
                    105: {
                    106:        CMenu* pMenu = m_pMDIFrameWnd->GetMenu();
                    107:        pMenu->CheckMenuItem(m_nColor, MF_UNCHECKED);
                    108: 
                    109:        m_nColor = GetCurrentMessage()->wParam;
                    110:        pMenu->CheckMenuItem(m_nColor, MF_CHECKED);
                    111: 
                    112:        if (m_nColor != IDM_CUSTOM)
                    113:                m_clrText = clrTextArray[m_nColor - IDM_BLACK];
                    114:        else
                    115:        {
                    116:                CColorDialog dlgColor(m_clrText);
                    117:                if (dlgColor.DoModal() == IDOK)
                    118:                        m_clrText = dlgColor.GetColor();
                    119:        }
                    120: 
                    121:        // Force the client area text to be repainted in the new color
                    122: 
                    123:        Invalidate();
                    124: }
                    125: 
                    126: // OnPaint:
                    127: // Draw a string in the center of the client area.
                    128: //
                    129: void CHelloWnd::OnPaint()
                    130: {
                    131:        CPaintDC dc(this);
                    132:        CRect rect;
                    133: 
                    134:        dc.SetTextColor(m_clrText);
                    135:        dc.SetBkColor(::GetSysColor(COLOR_WINDOW));
                    136:        GetClientRect(rect);
                    137:        dc.DrawText("Hello, World!", -1, rect, 
                    138:                DT_SINGLELINE | DT_CENTER | DT_VCENTER);
                    139: }
                    140: 
                    141: 
                    142: // OnMDIActivate:
                    143: // This window is being activated or deactivated.  
                    144: // Switch the menus appropriately.
                    145: //
                    146: void CHelloWnd::OnMDIActivate(BOOL bActivate, CWnd* /*pActive*/, 
                    147:                                                          CWnd* /*pDeActive*/)
                    148: {
                    149:        CMDIFrameWnd* pFrame = m_pMDIFrameWnd;
                    150:        CMenu* pWinPopupMenu = NULL;
                    151:        CMenu* pMenu = NULL;
                    152: 
                    153:        m_bWindowActive = bActivate;
                    154: 
                    155:        if (bActivate)
                    156:        {
                    157:                pMenu = new CMenu;
                    158:                pMenu->LoadMenu("MdiMenuHello");
                    159:                pWinPopupMenu = pMenu->GetSubMenu(HELLO_MENU_POS);
                    160: 
                    161:                CMenu* pLastMenu = pFrame->MDISetMenu(pMenu, pWinPopupMenu);
                    162:                pLastMenu->DestroyMenu();
                    163: 
                    164:                pMenu->CheckMenuItem(m_nColor, MF_CHECKED);
                    165: 
                    166:                delete m_pMenuCurrent;
                    167:                m_pMenuCurrent = pMenu;
                    168:        }
                    169:        else    
                    170:        {
                    171:                pMenu = new CMenu;
                    172:                pMenu->LoadMenu("MdiMenuInit");
                    173:                pWinPopupMenu = pMenu->GetSubMenu(INIT_MENU_POS);
                    174: 
                    175:                CMenu* pLastMenu = pFrame->MDISetMenu(pMenu, pWinPopupMenu);
                    176:                pLastMenu->DestroyMenu();
                    177: 
                    178:                delete m_pMenuCurrent;
                    179:                m_pMenuCurrent = pMenu;
                    180:        }
                    181: 
                    182:        pFrame->DrawMenuBar();
                    183: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.