Click here to Skip to main content
15,867,594 members
Articles / Desktop Programming / MFC

Owner Drawn Menu with Bitmaps, Icons, and Colors

Rate me:
Please Sign up or sign in to vote.
4.55/5 (8 votes)
8 Sep 2002 167.3K   4.7K   53   20
Use application resources to create your owner drawn menu

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:

C++
#include "MenuCH.h"

Step 3

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

C++
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():

C++
/////////////////////////////////////////////////////////
// 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:

C++
///////////////////////////////////////////////////////
// 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.

C++
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.


Written By
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.

Comments and Discussions

 
GeneralThanks Bro It works vote 5+ Pin
omkarpardeshi12327-Sep-12 2:28
omkarpardeshi12327-Sep-12 2:28 
GeneralCFont Pin
Vishal Lakhanpal24-Apr-07 5:20
Vishal Lakhanpal24-Apr-07 5:20 
Questionhow to set my menus back color by API directly? Pin
zhyaaa12310-Oct-06 20:41
zhyaaa12310-Oct-06 20:41 
QuestionHow to use CMenuCH in mouse right click popup menu? Pin
maohbao10-Oct-06 2:22
maohbao10-Oct-06 2:22 
AnswerRe: How to use CMenuCH in mouse right click popup menu? Pin
denny lu10-Jun-07 0:02
denny lu10-Jun-07 0:02 
Generalwhy the hot key can't work Pin
johnxie30-Mar-06 16:32
johnxie30-Mar-06 16:32 
GeneralMenu access keys Pin
Ole Martin Brynildsen19-Apr-05 23:31
Ole Martin Brynildsen19-Apr-05 23:31 
GeneralI only want to change font for menu caption Pin
Anonymous15-Oct-03 19:36
Anonymous15-Oct-03 19:36 
GeneralJust what I wanted Pin
Alton Williams30-May-03 23:26
Alton Williams30-May-03 23:26 
QuestionNon Doc/View application compatible? Pin
Steven M Hunt31-Dec-02 11:35
Steven M Hunt31-Dec-02 11:35 
QuestionWhy? Pin
hien_ng8028-Oct-02 1:33
hien_ng8028-Oct-02 1:33 
AnswerRe: Why? Pin
chia hung liu1-Jan-03 15:41
chia hung liu1-Jan-03 15:41 
GeneralI have problem with PopUp Menu Pin
thanh200224-Oct-02 7:31
thanh200224-Oct-02 7:31 
GeneralRe: I have problem with PopUp Menu Pin
Anonymous27-Oct-02 16:20
Anonymous27-Oct-02 16:20 
GeneralCMenuCh Pin
Old Timer25-Sep-02 8:49
Old Timer25-Sep-02 8:49 
GeneralRe: CMenuCh Pin
chia hung liu13-Oct-02 15:38
chia hung liu13-Oct-02 15:38 
QuestionCan be used in a dialog based application? Pin
chen23-Sep-02 7:21
chen23-Sep-02 7:21 
AnswerRe: Can be used in a dialog based application? Pin
chia hung liu29-Sep-02 15:23
chia hung liu29-Sep-02 15:23 
GeneralRe: Can be used in a dialog based application? Pin
chen29-Sep-02 16:20
chen29-Sep-02 16:20 
Hi,

Thanks for the respones.
In fact, i means that would you please
say in detail how to use it in a dialog based application (not in SDI/MDI
based appliucation) ?



chen
GeneralRe: Can be used in a dialog based application? Pin
chia hung liu2-Oct-02 23:02
chia hung liu2-Oct-02 23:02 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.