Click here to Skip to main content
Click here to Skip to main content

Owner drawn menu with bitmaps, icons, and colors

By , 8 Sep 2002
 

Sample Image - MenuCH.jpg

Introduction

The article presents an implementation of an owner drawn menu with the Windows XP and Icon select style. To use this code download the demo project. The demo project has 3 menu styles which demonstrate how to create XP menu and Icon selected menu. To add owner drawn menus to your own project follow these 6 steps:

Step 1

Add the following files to your project:

  • MenuCH.cpp
  • MenuCH.h

Step 2

In MainFrm.h - add this line to the top of the file:

#include "MenuCH.h"

Step 3

In MainFrm.h - Add CMainFrame member variables of type CMenuCH.

protected:  // control bar embedded members
    CStatusBar  m_wndStatusBar;
    CToolBar    m_wndToolBar;
    CMenuCH    m_FileMenu, m_EditMenu,
        m_ViewMenu, m_HelpMenu;
    CMenuCH    m_ElementMenu;
    CMenuCH    GraphMenu,ColorMenu;

Step 4

In MainFrm.cpp - place the following statements in CMainFrame()

/////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
    // TODO: add member initialization code here
    m_FileMenu.CreatePopupMenu();
    m_FileMenu.SetMenuHeight(20);
    m_FileMenu.SetMenuWidth(150);
    m_FileMenu.SetMenuType(MIT_XP);
    m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_NEW,
        "&New\tCtrl+N",IDB_NEW);
    m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_OPEN,
        "&Open\tCtrl+O",IDB_OPEN);
    m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_CLOSE,
        "&Close");
    m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_SAVE,
        "&Save\tCtrl+S",IDB_SAVE);
    m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_SAVE_AS,
        "Save &As...");
    m_FileMenu.AppendMenu(MF_SEPARATOR,0,"");
    m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_PRINT,
        "&Print...",IDB_PRINT);
    m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_PRINT_PREVIEW,
        "Print Pre&view");
    m_FileMenu.AppendMenu(MF_ENABLED,ID_FILE_PRINT_SETUP,
        "P&rint Setup...");
    m_FileMenu.AppendMenu(MF_SEPARATOR,0,"");
    m_FileMenu.AppendMenu(MF_ENABLED,ID_APP_EXIT,
        "E&xit");

    m_EditMenu.CreatePopupMenu();
    m_EditMenu.SetMenuHeight(20);
    m_EditMenu.SetMenuWidth(165);
    m_EditMenu.SetMenuType(MIT_XP);
    m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_UNDO,
        "Redo\tCtrl+Z",IDB_UNDO);
    m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_REDO,
        "Undo\tCtrl+Y",IDB_REDO);
    m_EditMenu.AppendMenu(MF_SEPARATOR,0,"");
    m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_CUT, 
        "Cut\tCtrl+X",IDB_CUT);
    m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_COPY, 
        "Copy\tCtrl+C",IDB_COPY);
    m_EditMenu.AppendMenu(MF_ENABLED,ID_EDIT_PASTE,
        "Paste\tCtrl+V",IDB_PASTE);


    m_ViewMenu.CreatePopupMenu();
    m_ViewMenu.SetMenuHeight(20);
    m_ViewMenu.SetMenuWidth(170);
    m_ViewMenu.SetMenuType(MIT_XP);
    m_ViewMenu.AppendMenu(MF_ENABLED,ID_VIEW_TOOLBAR, 
        "&Toolbar");
    m_ViewMenu.AppendMenu(MF_ENABLED,ID_VIEW_STATUS_BAR,
        "&Status Bar");

    m_HelpMenu.CreatePopupMenu();
    m_HelpMenu.SetMenuHeight(20);
    m_HelpMenu.SetMenuWidth(160);
    m_HelpMenu.SetMenuType(MIT_XP);
    m_HelpMenu.AppendMenu(MF_ENABLED,ID_APP_ABOUT,
        "&About BmpMenuDemo...",IDB_HELP);

    GraphMenu.CreateMenu();
    GraphMenu.AppendMenu(MF_ENABLED,ID_GRAPHPART1BOX,"",
        NULL,AfxGetApp()->LoadIcon(IDI_LINE));
    GraphMenu.AppendMenu(MF_ENABLED,ID_GRAPHPART2BOX,"",
        NULL,AfxGetApp()->LoadIcon(IDI_GRAPHIC));
    GraphMenu.AppendMenu(MF_ENABLED|MF_MENUBREAK,ID_GRAPHPART3BOX,
        "",NULL,AfxGetApp()->LoadIcon(IDI_CIRCLE));
    GraphMenu.AppendMenu(MF_ENABLED,ID_GRAPHPART4BOX,"",
        NULL,AfxGetApp()->LoadIcon(IDI_POLYGON));
    GraphMenu.AppendMenu(MF_ENABLED|MF_MENUBREAK,
        ID_GRAPHPART5BOX,"",NULL,AfxGetApp()->LoadIcon(IDI_ARC));
    GraphMenu.AppendMenu(MF_ENABLED,ID_GRAPHPART6BOX,"",
        NULL,AfxGetApp()->LoadIcon(IDI_TEXT));

    m_ElementMenu.CreatePopupMenu();
    m_ElementMenu.SetMenuType(MIT_ICON);
    m_ElementMenu.AppendMenu(MF_SEPARATOR,0,"");
    m_ElementMenu.AppendMenu(MF_POPUP,
        (UINT)GraphMenu.m_hMenu,"Element");

    ColorMenu.CreatePopupMenu();
    ColorMenu.SetMenuHeight(18);
    ColorMenu.SetMenuWidth(6);
    ColorMenu.SetMenuType(MIT_COLOR);
    char clrValue[64];
    for(int i=1; i<=16; i++)
    {
        wsprintf(clrValue,"%d",rgbColors[i-1]);
        if( i%4 == 1 )
            ColorMenu.AppendMenu(MF_MENUBREAK|MF_ENABLED,
            i,clrValue);
        else
            ColorMenu.AppendMenu(MF_ENABLED,i,clrValue);
    }
    m_ElementMenu.AppendMenu(MF_POPUP,(UINT)ColorMenu.m_hMenu,
        "Colors");
}

Step 5

In MainFrm.cpp - Add CMainFrame member function CreateMenu() and place the following statements:

///////////////////////////////////////////////////////
// CMainFrame message handlers
void CMainFrame::CreateMenu()
{
    CMenu* pMenu = this->GetMenu();
    pMenu->RemoveMenu(0,MF_BYPOSITION);
    pMenu->RemoveMenu(0,MF_BYPOSITION);
    pMenu->RemoveMenu(0,MF_BYPOSITION);
    pMenu->RemoveMenu(0,MF_BYPOSITION);
    pMenu->RemoveMenu(0,MF_BYPOSITION);

    pMenu->InsertMenu(0,MF_BYPOSITION|MF_POPUP,
        (UINT)m_FileMenu.m_hMenu,"&File");  
    pMenu->InsertMenu(1,MF_BYPOSITION|MF_POPUP,
        (UINT)m_EditMenu.m_hMenu,"&Edit");
    pMenu->InsertMenu(2,MF_BYPOSITION|MF_POPUP,
        (UINT)m_ViewMenu.m_hMenu,"&View");
    pMenu->InsertMenu(3,MF_BYPOSITION|MF_POPUP,
        (UINT)m_HelpMenu.m_hMenu,"&Help");
    pMenu->InsertMenu(4,MF_BYPOSITION|MF_POPUP,
        (UINT)m_ElementMenu.m_hMenu,"&Element");
}

Step 6

In MainFrm.cpp - call CreateMenu() in CMainFrame::OnCreate.

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    ...

        CreateMenu();
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

chia hung liu
Web Developer
Taiwan Taiwan
I'm a software engineer for a R&D department developing specialised user interface software for the automation industry.
 
I enjoy coding and researching algorithm.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanks Bro It works vote 5+memberomkarpardeshi12327-Sep-12 2:28 
GeneralCFontmemberVishal Lakhanpal24-Apr-07 5:20 
Questionhow to set my menus back color by API directly?memberzhyaaa12310-Oct-06 20:41 
QuestionHow to use CMenuCH in mouse right click popup menu?membermaohbao10-Oct-06 2:22 
AnswerRe: How to use CMenuCH in mouse right click popup menu?memberdenny lu10-Jun-07 0:02 
Generalwhy the hot key can't workmemberjohnxie30-Mar-06 16:32 
GeneralMenu access keysmemberOle Martin Brynildsen19-Apr-05 23:31 
GeneralI only want to change font for menu captionsussAnonymous15-Oct-03 19:36 
Please give me the code to change font for menu caption.
As the WINDOWS Display/Apearence/ and change font for menu.
GeneralJust what I wantedsussAlton Williams30-May-03 23:26 
QuestionNon Doc/View application compatible?memberSteveBob31-Dec-02 11:35 
QuestionWhy?memberhien_ng8028-Oct-02 1:33 
AnswerRe: Why?memberchia hung liu1-Jan-03 15:41 
GeneralI have problem with PopUp Menumemberthanh200224-Oct-02 7:31 
GeneralRe: I have problem with PopUp MenusussAnonymous27-Oct-02 16:20 
GeneralCMenuChmemberOld Timer25-Sep-02 8:49 
GeneralRe: CMenuChmemberchia hung liu13-Oct-02 15:38 
QuestionCan be used in a dialog based application?memberchen23-Sep-02 7:21 
AnswerRe: Can be used in a dialog based application?memberchia hung liu29-Sep-02 15:23 
GeneralRe: Can be used in a dialog based application?memberchen29-Sep-02 16:20 
GeneralRe: Can be used in a dialog based application?memberchia hung liu2-Oct-02 23:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130617.1 | Last Updated 9 Sep 2002
Article Copyright 2002 by chia hung liu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid