65.9K
CodeProject is changing. Read more.
Home

CMenuEx - Ownerdrawn Menu

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (9 votes)

Aug 28, 2004

1 min read

viewsIcon

70140

downloadIcon

3848

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).