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:
- Include MenuEx.h and MenuEx.cpp in your project.
- Edit the header of the class handling the menu (usually MainFrm.h).
#include "MenuEx/MenuEx.h"
DECLARE_MENUEX()
public:
CMenuEx* m_pMainMenu;
- Edit the source of the class handling the menu (usually MainFrm.cpp).
IMPLEMENT_MENUEX(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_MENUEX_MESSAGES()
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))
{
::DestroyMenu(m_hMenuDefault);
SetMenu(m_pMainMenu);
m_hMenuDefault = m_pMainMenu->GetSafeHmenu();
}
- If you want the menu to use the images of an existing toolbar, add the following code:
if(m_pMainMenu->LoadMenu(IDR_MAINFRAME))
{
m_pMainMenu->UseToolBarImages(&m_wndToolBar);
::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).