Annotation of mstools/mfc/samples/ctrltest/custmenu.cpp, revision 1.1

1.1     ! root        1: // custmenu.cpp : custom menu
        !             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: #include "ctrltest.h"
        !            14: 
        !            15: /////////////////////////////////////////////////////////////////////////////
        !            16: 
        !            17: // for owner draw menus, the CMenu object is embedded in the main frame window
        !            18: //  the CMenu stays attached to the HMENU while it is running so that
        !            19: //  owner draw messages are delegated to this class.
        !            20: //  Since we attach the HMENU to a menu bar (with ModifyMenu below), we
        !            21: //  don't want to delete the menu twice - so we detach on the destructor.
        !            22: 
        !            23: CColorMenu::CColorMenu()
        !            24: {
        !            25:        VERIFY(CreateMenu());
        !            26: }
        !            27: 
        !            28: CColorMenu::~CColorMenu()
        !            29: {
        !            30:        Detach();
        !            31:        ASSERT(m_hMenu == NULL);    // defaul CMenu::~CMenu will destroy
        !            32: }
        !            33: 
        !            34: void CColorMenu::AppendColorMenuItem(UINT nID, COLORREF color)
        !            35: {
        !            36:        VERIFY(AppendMenu(MF_ENABLED | MF_OWNERDRAW, nID, (LPCSTR)color));
        !            37: }
        !            38: 
        !            39: /////////////////////////////////////////////////////////////////////////////
        !            40: 
        !            41: #define COLOR_BOX_WIDTH     20
        !            42: #define COLOR_BOX_HEIGHT    20
        !            43: 
        !            44: 
        !            45: void CColorMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
        !            46: {
        !            47:        // all items are of fixed size
        !            48:        lpMIS->itemWidth = COLOR_BOX_WIDTH;
        !            49:        lpMIS->itemHeight = COLOR_BOX_HEIGHT;
        !            50: }
        !            51: 
        !            52: void CColorMenu::DrawItem(LPDRAWITEMSTRUCT lpDIS)
        !            53: {
        !            54:        CDC* pDC = CDC::FromHandle(lpDIS->hDC);
        !            55:        COLORREF cr = lpDIS->itemData; // RGB in item data
        !            56: 
        !            57:        if (lpDIS->itemAction & ODA_DRAWENTIRE)
        !            58:        {
        !            59:                // Paint the color item in the color requested
        !            60:                CBrush br(cr);
        !            61:                pDC->FillRect(&lpDIS->rcItem, &br);
        !            62:        }
        !            63: 
        !            64:        if ((lpDIS->itemState & ODS_SELECTED) &&
        !            65:                (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
        !            66:        {
        !            67:                // item has been selected - hilite frame
        !            68:                COLORREF crHilite = RGB(255-GetRValue(cr),
        !            69:                                                255-GetGValue(cr), 255-GetBValue(cr));
        !            70:                CBrush br(crHilite);
        !            71:                pDC->FrameRect(&lpDIS->rcItem, &br);
        !            72:        }
        !            73: 
        !            74:        if (!(lpDIS->itemState & ODS_SELECTED) &&
        !            75:                (lpDIS->itemAction & ODA_SELECT))
        !            76:        {
        !            77:                // Item has been de-selected -- remove frame
        !            78:                CBrush br(cr);
        !            79:                pDC->FrameRect(&lpDIS->rcItem, &br);
        !            80:        }
        !            81: }
        !            82: 
        !            83: /////////////////////////////////////////////////////////////////////////////
        !            84: 
        !            85: // Call AttachCustomMenu once
        !            86: //   it will replace the menu item with the ID  'IDM_TEST_CUSTOM_MENU'
        !            87: //   with a color menu popup
        !            88: // Replace the specified menu item with a color popup
        !            89: void CTestWindow::AttachCustomMenu()
        !            90: {
        !            91:        // now add a few new menu items
        !            92:        for (int iColor = 0; iColor <= (IDM_COLOR_LAST-IDM_COLOR_FIRST); iColor++)
        !            93:        {
        !            94:                // 3 bit encoded RGB values
        !            95:                BYTE red = (iColor & 4) * 255;
        !            96:                BYTE green = (iColor & 2) * 255;
        !            97:                BYTE blue = (iColor & 1) * 255;
        !            98: 
        !            99:                m_colorMenu.AppendColorMenuItem(IDM_COLOR_FIRST + iColor,
        !           100:                        RGB(red, green, blue));
        !           101:        }
        !           102: 
        !           103:        // Replace the specified menu item with a color popup
        !           104:        //  (note: will only work once)
        !           105:        CMenu* pMenuBar = GetMenu();
        !           106:        ASSERT(pMenuBar != NULL);
        !           107:        char szString[256];     // don't change the string
        !           108: 
        !           109:        pMenuBar->GetMenuString(IDM_TEST_CUSTOM_MENU, szString, sizeof(szString),
        !           110:                MF_BYCOMMAND);
        !           111:        VERIFY(GetMenu()->ModifyMenu(IDM_TEST_CUSTOM_MENU, MF_BYCOMMAND | MF_POPUP,
        !           112:                (UINT)m_colorMenu.m_hMenu, szString));
        !           113: }
        !           114: 
        !           115: /////////////////////////////////////////////////////////////////////////////
        !           116: 
        !           117: char* colorNames[] =
        !           118: {
        !           119:        "black", "blue", "green", "cyan",
        !           120:        "red", "magenta", "yellow", "white"
        !           121: };
        !           122: 
        !           123: BOOL CTestWindow::OnCommand(UINT wParam, LONG lParam)
        !           124: {
        !           125:        if (wParam < IDM_COLOR_FIRST || wParam > IDM_COLOR_LAST)
        !           126:                return CFrameWnd::OnCommand(wParam, lParam);        // default
        !           127: 
        !           128:        // special color selected
        !           129:        char szT[256];
        !           130:        sprintf(szT, "You picked %s", colorNames[wParam - IDM_COLOR_FIRST]);
        !           131:        MessageBox(szT, "MenuTest");
        !           132:        return TRUE;
        !           133: }
        !           134: 
        !           135: /////////////////////////////////////////////////////////////////////////////

unix.superglobalmegacorp.com

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