Annotation of mstools/mfc/samples/about2/about2.cpp, revision 1.1

1.1     ! root        1: // about2.cpp : Defines the class behaviors for the About2 application.
        !             2: //              This application demonstrates typical modal dialog use,
        !             3: //              painting special graphics on a modal dialog, and when
        !             4: //              compared to the original C code, shows equivalent
        !             5: //              functionality between C code and C++ Foundation code.
        !             6: //
        !             7: // This is a part of the Microsoft Foundation Classes C++ library.
        !             8: // Copyright (C) 1992 Microsoft Corporation
        !             9: // All rights reserved.
        !            10: //
        !            11: // This source code is only intended as a supplement to the
        !            12: // Microsoft Foundation Classes Reference and Microsoft
        !            13: // QuickHelp documentation provided with the library.
        !            14: // See these sources for detailed information regarding the
        !            15: // Microsoft Foundation Classes product.
        !            16: //
        !            17: // Based on the About2 application by Charles Petzold.
        !            18: // The original application appeared in
        !            19: // "Programming Windows", Second Edition (pp. 417-423),
        !            20: // Copyright (C) 1990 Charles Petzold,
        !            21: // published by Microsoft Press. Used with permission.
        !            22: 
        !            23: #include <afxwin.h>
        !            24: #include "resource.h"
        !            25: 
        !            26: #include "about2.h"
        !            27: 
        !            28: /////////////////////////////////////////////////////////////////////////////
        !            29: 
        !            30: // An application can only contain one CWinApp-derived instance
        !            31: 
        !            32: CAbout2App theApp;
        !            33: 
        !            34: /////////////////////////////////////////////////////////////////////////////
        !            35: 
        !            36: // Paint:
        !            37: // A helper function which paints a window with the given figure.  Note
        !            38: // it can either be called with an existing CDC or NULL.  If NULL is sent,
        !            39: // this routine creates its own display context and discards it when
        !            40: // finished.
        !            41: //
        !            42: void Paint(CWnd* wnd, CDC* dc, short nColor, short nFigure)
        !            43: {
        !            44:        static DWORD dwColor[8] = { RGB (0,     0, 0), RGB (  0,   0, 255),
        !            45:                                                                RGB (0,   255, 0), RGB (  0, 255, 255),
        !            46:                                                                RGB (255,   0, 0), RGB (255,   0, 255),
        !            47:                                                                RGB (255, 255, 0), RGB (255, 255, 255) } ;
        !            48: 
        !            49:        CDC* odc = dc;
        !            50: 
        !            51:        if(!odc)
        !            52:        {
        !            53:                wnd->InvalidateRect(NULL, TRUE);
        !            54:                wnd->UpdateWindow(); // paints the background
        !            55:                dc = wnd->GetDC();
        !            56:        }
        !            57: 
        !            58:        CRect rect;
        !            59:        wnd->GetClientRect(rect);
        !            60: 
        !            61:        CBrush brush;
        !            62:        CBrush* oldbrush;
        !            63:        brush.CreateSolidBrush(dwColor[nColor-IDD_BLACK]);
        !            64:        oldbrush = dc->SelectObject(&brush);
        !            65: 
        !            66:        if(nFigure == IDD_RECT)
        !            67:                dc->Rectangle(rect);
        !            68:        else
        !            69:                dc->Ellipse(rect);
        !            70: 
        !            71:        dc->SelectObject(oldbrush);
        !            72:        brush.DeleteObject();
        !            73: 
        !            74:        if(!odc)
        !            75:                wnd->ReleaseDC(dc);
        !            76: }
        !            77: 
        !            78: /////////////////////////////////////////////////////////////////////////////
        !            79: // CAbout2Dlg
        !            80: 
        !            81: // OnInitDialog:
        !            82: // Called once when the DoModal member function is called.
        !            83: // Set up the checked/unchecked or focused states of the child controls.
        !            84: //
        !            85: // Note that this member function does not need an entry in the message map.
        !            86: //
        !            87: BOOL CAbout2Dlg::OnInitDialog()
        !            88: {
        !            89:        CheckRadioButton(IDD_BLACK, IDD_WHITE, m_nColor);
        !            90:        CheckRadioButton(IDD_RECT, IDD_ELL, m_nFigure);
        !            91: 
        !            92:        m_ctrl = GetDlgItem(IDD_PAINT);
        !            93:        GetDlgItem(m_nColor)->SetFocus();
        !            94: 
        !            95:        return FALSE;
        !            96: }
        !            97: 
        !            98: // OnColor:
        !            99: // This is a custom message handler, which we call anytime a radio button
        !           100: // between IDD_BLACK and IDD_WHITE are pushed.  We just set all the color
        !           101: // radio buttons, and repaint the inset graphic m_ctrl.
        !           102: //
        !           103: void CAbout2Dlg::OnColor()
        !           104: {
        !           105:        // Since we cannot pass any arguments with the ON_COMMAND message map,
        !           106:        // we use the GetCurrentMessage call to see which control was pushed.
        !           107:        //
        !           108:        m_nColor = GetCurrentMessage()->wParam;
        !           109:        CheckRadioButton(IDD_BLACK, IDD_WHITE, m_nColor);
        !           110: 
        !           111:        Paint(m_ctrl, NULL, m_nColor, m_nFigure);
        !           112: }
        !           113: 
        !           114: // OnFigure:
        !           115: // Like OnColor, except this handles the IDD_RECT and IDD_ELL radio buttons.
        !           116: //
        !           117: void CAbout2Dlg::OnFigure()
        !           118: {
        !           119:        // We use the GetCurrentMessage call to see which control was pushed.
        !           120:        //
        !           121:        m_nFigure = GetCurrentMessage()->wParam;
        !           122:        CheckRadioButton(IDD_RECT, IDD_ELL, m_nFigure);
        !           123: 
        !           124:        Paint(m_ctrl, NULL, m_nColor, m_nFigure);
        !           125: }
        !           126: 
        !           127: // OnPaint:
        !           128: // The dialog is not clean.  Let the base class (CModalDialog::OnPaint)
        !           129: // do the regular paint behavior, then redraw the special inset figure in
        !           130: // the m_ctrl control.
        !           131: //
        !           132: void CAbout2Dlg::OnPaint()
        !           133: {
        !           134:        CModalDialog::OnPaint();
        !           135: 
        !           136:        Paint(m_ctrl, NULL, m_nColor, m_nFigure);
        !           137: }
        !           138: 
        !           139: // CAbout2Dlg message map:
        !           140: // Each Windows message we handle is tied to an CAbout2Dlg member function.
        !           141: //
        !           142: BEGIN_MESSAGE_MAP(CAbout2Dlg, CModalDialog)
        !           143: 
        !           144:        // These are the click command notifications from our radio buttons.
        !           145:        // Associate them all to our OnColor member function for processing.
        !           146:        // These assume the function is "void OnColor()".
        !           147:        //
        !           148:        ON_COMMAND(IDD_BLACK, OnColor)
        !           149:        ON_COMMAND(IDD_RED, OnColor)
        !           150:        ON_COMMAND(IDD_GREEN, OnColor)
        !           151:        ON_COMMAND(IDD_YELLOW, OnColor)
        !           152:        ON_COMMAND(IDD_BLUE, OnColor)
        !           153:        ON_COMMAND(IDD_MAGENTA, OnColor)
        !           154:        ON_COMMAND(IDD_CYAN, OnColor)
        !           155:        ON_COMMAND(IDD_WHITE, OnColor)
        !           156: 
        !           157:        // Associate both of these to the OnFigure member function.
        !           158:        // These assume the function is "void OnFigure()".
        !           159:        //
        !           160:        ON_COMMAND(IDD_RECT, OnFigure)
        !           161:        ON_COMMAND(IDD_ELL, OnFigure)
        !           162: 
        !           163:        // Associate the WM_PAINT message to the OnPaint member function.
        !           164:        // This assumes the function is "void OnPaint()".
        !           165:        //
        !           166:        ON_WM_PAINT()
        !           167: 
        !           168:        // OnInitDialog and OnOK are automatically tied to the messaging system.
        !           169: 
        !           170: END_MESSAGE_MAP()
        !           171: 
        !           172: /////////////////////////////////////////////////////////////////////////////
        !           173: // CMainWnd
        !           174: 
        !           175: // CMainWnd constructor:
        !           176: // When the class object is created, the constructor should tell Windows
        !           177: // to create a window of the appropriate style and size.  The menu resource
        !           178: // "About2" in about2.rc is also loaded for the window.
        !           179: //
        !           180: CMainWnd::CMainWnd()
        !           181: {
        !           182:        if (!Create(NULL, "About Box Demo Program",
        !           183:                WS_OVERLAPPEDWINDOW, rectDefault, NULL, "About2"))
        !           184:                 AfxAbort();
        !           185:        m_nCurrentColor = IDD_BLACK;
        !           186:        m_nCurrentFigure = IDD_RECT;
        !           187: }
        !           188: 
        !           189: // OnPaint:
        !           190: // The image of our client area needs to be refreshed.
        !           191: //
        !           192: void CMainWnd::OnPaint()
        !           193: {
        !           194:        CPaintDC dc(this);
        !           195:        Paint(this, &dc, m_nCurrentColor, m_nCurrentFigure);
        !           196: }
        !           197: 
        !           198: // OnAbout:
        !           199: // The "About About2..." item of the menu was selected.  All we need to
        !           200: // do is create a dialog box object (CAbout2Dlg), and tell it to start up.
        !           201: // Note that if it is successful (the user pressed OK), then the image we
        !           202: // are currently displaying may be invalid.
        !           203: //
        !           204: void CMainWnd::OnAbout()
        !           205: {
        !           206:        CAbout2Dlg box(this, m_nCurrentColor, m_nCurrentFigure);
        !           207: 
        !           208:        if(box.DoModal() == IDOK)
        !           209:        {
        !           210:                // The user accepted the changes so get the information from the
        !           211:                // dialog object and invalidate the client area.
        !           212:                m_nCurrentColor = box.GetColor();
        !           213:                m_nCurrentFigure = box.GetFigure();
        !           214:                InvalidateRect(NULL, TRUE);
        !           215:        }
        !           216: }
        !           217: 
        !           218: BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
        !           219:        ON_COMMAND(IDM_ABOUT, OnAbout)
        !           220: 
        !           221:        ON_WM_PAINT()
        !           222: END_MESSAGE_MAP()
        !           223: 
        !           224: /////////////////////////////////////////////////////////////////////////////
        !           225: // CAbout2App
        !           226: 
        !           227: // InitInstance:
        !           228: // Called once when the application begins.  All we need to do is to create
        !           229: // a new CMainWnd object which can handle itself once made visible.
        !           230: //
        !           231: BOOL CAbout2App::InitInstance()
        !           232: {
        !           233:        m_pMainWnd = new CMainWnd();
        !           234:        m_pMainWnd->ShowWindow(m_nCmdShow);
        !           235:        m_pMainWnd->UpdateWindow();
        !           236: 
        !           237:        return TRUE;
        !           238: }
        !           239: 

unix.superglobalmegacorp.com

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