Click here to Skip to main content
15,895,777 members
Articles / Desktop Programming / MFC
Article

CMenuEx - Ownerdrawn Menu

Rate me:
Please Sign up or sign in to vote.
3.00/5 (9 votes)
27 Aug 20041 min read 69.6K   3.8K   21   7
An easy-to-use ownerdrawn menu, derived from CMenu.

Sample Image - CMenuEx.JPG

Introduction

This article introduces a CMenu-derived class for an owner drawn menu with customization available at runtime.

Background

The intention of this class is to provide an easy way of implementing custom menus with Office XP or Visual Studio .NET style.

All colors used in drawing the menu may be changed at runtime, and there is a variety of other settings provided for customizing the menu style.

Using the code

In order to use CMenuEx, you have to follow these steps:

  1. Include MenuEx.h and MenuEx.cpp in your project.
  2. Edit the header of the class handling the menu (usually MainFrm.h).
    #include "MenuEx/MenuEx.h"
    // ...
    DECLARE_MENUEX()
    // ...
    public:
        CMenuEx*    m_pMainMenu;
  3. Edit the source of the class handling the menu (usually MainFrm.cpp).
    IMPLEMENT_MENUEX(CMainFrame, CFrameWnd)
    // ...
    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
        // ...
        ON_MENUEX_MESSAGES()    /* CMenuEx */
    END_MESSAGE_MAP()
    // ...
    CMainFrame::CMainFrame()
    {
        // ...
        m_pMainMenu = NULL;
    }
    // ...
    CMainFrame::~CMainFrame()
    {
        // ...
        delete m_pMainMenu;
    }
    // ...
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        // ...
        m_pMainMenu = new CMenuEx(this);
        if(m_pMainMenu->LoadMenu(IDR_MAINFRAME))
        {
            /*
                Custom color & size settings
                for startup should go here
            */
            ::DestroyMenu(m_hMenuDefault);
            SetMenu(m_pMainMenu);
            m_hMenuDefault = m_pMainMenu->GetSafeHmenu();
        }
        // ...
  4. If you want the menu to use the images of an existing toolbar, add the following code:
    if(m_pMainMenu->LoadMenu(IDR_MAINFRAME))
    {
        /*
            Custom color & size settings
            for startup should go here
        */
        m_pMainMenu->UseToolBarImages(&m_wndToolBar);    // Added line
        ::DestroyMenu(m_hMenuDefault);
        SetMenu(m_pMainMenu);
        m_hMenuDefault = m_pMainMenu->GetSafeHmenu();
    }
    

    This sample code replaces the standard Main Menu of a SDI/MDI project with a CMenuEx, using the default settings of CMenuEx and the images of the default application toolbar.

Remarks

Menu icons and images are not yet supported. Also, it is not recommended to change the item size settings during runtime. Changing colors works fine though.

History

  • 08/28/2004 - Version 1.0.
  • 08/30/2004 - Version 1.1 (Added support for Images and Checkmarks).

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
Germany Germany
I am from Germany and started working as a VC++ / MFC developer in 1996.
The company I am working for provides software for car workshops and sellers. Since I am working there, I developed one of their main products from scratch, first in VC++ 6, later porting it to VC++ 7.
Another part of my work is providing and maintaining various ASP and ASP.NET applications, the latter usually developed using C# and using a MS SQL Server as data provider.

Comments and Discussions

 
GeneralDynamically created Problem!~ [modified] Pin
angeltwb12345618-Aug-10 20:01
angeltwb12345618-Aug-10 20:01 
GeneralDraw BackgroundColour of Top level Menu Pin
AndiW13-Jun-05 5:36
AndiW13-Jun-05 5:36 
GeneralRe: Draw BackgroundColour of Top level Menu Pin
Devil for ever9-Jun-06 8:34
Devil for ever9-Jun-06 8:34 
Questionhow to change font Pin
muharrem8-Dec-04 23:01
muharrem8-Dec-04 23:01 
GeneralProblem Pin
David Simmonds12-Nov-04 6:42
David Simmonds12-Nov-04 6:42 
QuestionCan you upload source code? Pin
Anonymous19-Oct-04 4:36
Anonymous19-Oct-04 4:36 
AnswerRe: Can you upload source code? Pin
C. Bügenburg19-Oct-04 5:02
C. Bügenburg19-Oct-04 5: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.