Click here to Skip to main content
15,880,967 members
Articles / Desktop Programming / MFC

A Powerful Ownerdraw Menu

Rate me:
Please Sign up or sign in to vote.
4.92/5 (33 votes)
23 Oct 20012 min read 730.2K   9K   111   105
An XP-style ownerdrawn menu with support for background images and icon shadow

Sample Image - MenuXP.jpg Sample Image - MenuXP2.jpg Sample Image - MenuXP3.jpg

A Fully Featured Owner-draw Menu Class

CMenuXP is a class derived from CMenu using ownerdraw technology. I named it MenuXP because I expected it to be like the menus found in Office XP and Windows XP, but I failed to accomplish it. The main difficulty that I had was converting the 3D border of the menu into a flat one, but I hope it is still useful to you.

I constructed the class from the Scribble sample application and some of the drawing code is copied from the CCoolMenuManager class. Additionally, I have also used a class named CBCGKeyHelper from BCGControlBar to show the accelerator key text.

Features

  1. Menu with icons, like in Office 97
  2. A sidebar in any level of the popup menu
  3. Supports button-style menu items, such as is found in some drawing toolbars of the Microsoft Office suite
  4. All colors, fonts and sizes can be customized

Introduction

An item in the menu is represented by the CMenuXPItem class. Some further classes are derived from it which provide convenient uses:

  • CMenuXPText presents a normal menu item with text and an optional icon
  • CMenuXPSeparator presents a separator
  • CMenuXPSideBar presents a sidebar on the left of a popup menu
  • CMenuXPButton presents a button only menu item, which contains the icon only

Knowing this will help you to understand the code.

How to Use as a Popup Menu

  1. Construct a CMenuXP instance
  2. Call CreatePopupMenu
  3. Add a sidebar using AddSideBar if needed
  4. Add some menu items using AppendODMenu
  5. If there is second level popup menu, construct it using steps 1 to 4 and add it to the current menu using AppendODPopup
  6. Call TrackPopupMenu as normal

The example code would be like this:

C++
void CMenuXPAppView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	CMenuXP	*pMenu = new CMenuXP;
	pMenu->CreatePopupMenu();

	pMenu->AddSideBar(new CMenuXPSideBar(24, "MenuXP"));
	pMenu->AppendODMenu(0, new CMenuXPText(10, "First Item",
                               AfxGetApp()->LoadIcon(IDI_ICON1)));
	pMenu->AppendODMenu(0, new CMenuXPText(11, "Second Item",
                               AfxGetApp()->LoadIcon(IDI_ICON2)));
	pMenu->AppendODMenu(0, new CMenuXPText(12, "Another Item",
                               AfxGetApp()->LoadIcon(IDI_ICON3)));
	pMenu->AppendODMenu(0, new CMenuXPText(13, "No Icon"));

	CMenuXP *pPopup = new CMenuXP;
	pPopup->CreatePopupMenu();
	pPopup->AppendODMenu(0, new CMenuXPButton(21,
                                AfxGetApp()->LoadIcon(IDI_ICON4)));
	pPopup->AppendODMenu(0, new CMenuXPButton(22,
                                AfxGetApp()->LoadIcon(IDI_ICON5)));
	pPopup->AppendODMenu(0, new CMenuXPButton(23,
                                AfxGetApp()->LoadIcon(IDI_ICON6)));
	pPopup->Break();
	pPopup->AppendODMenu(0, new CMenuXPButton(24,
                                AfxGetApp()->LoadIcon(IDI_ICON7)));
	pPopup->AppendODMenu(0, new CMenuXPButton(25,
                                AfxGetApp()->LoadIcon(IDI_ICON8)));
	pPopup->AppendODMenu(0, new CMenuXPButton(26,
                                AfxGetApp()->LoadIcon(IDI_ICON9)));
	pPopup->Break();
	pPopup->AppendODMenu(0, new CMenuXPButton(27,
                                AfxGetApp()->LoadIcon(IDI_ICON10)));
	pPopup->AppendODMenu(0, new CMenuXPButton(28,
                                AfxGetApp()->LoadIcon(IDI_ICON11)));
	pPopup->AppendODMenu(0, new CMenuXPButton(29,
                                AfxGetApp()->LoadIcon(IDI_ICON12)));

	pMenu->AppendODPopup(0, pPopup, new CMenuXPText(0, "Popup",
                                AfxGetApp()->LoadIcon(IDI_ICON1)));

	pMenu->TrackPopupMenu(TPM_LEFTBUTTON, point.x, point.y, this);

	delete pMenu;
}

The object constructed on the heap will be destroyed automatically, except for the toplevel popup menu which will need to be destroyed manually.

Remember to add the code below in the WM_MEASUREITEM handler of your parent window:

C++
void CMenuXPAppView::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	// TODO: Add your message handler code here and/or call default
	HMENU hMenu = AfxGetThreadState()->m_hTrackingMenu;
	CMenu	*pMenu = CMenu::FromHandle(hMenu);
	pMenu->MeasureItem(lpMeasureItemStruct);

	CView::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}

That's all, I hope this is useful to you. Bug reports and improvements are welcome.

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
China China
I'm a chinese programer living in Shanghai, currently working for a software company whose main business is to deliver computer based testing. Software simulation for computer based testing and certifications is my main responsibility in this company. Execpt for software development, I like out-door activities and photography. I am willing to make friends in China and all over the world, so contact me if you have anything in common with meSmile | :)

Comments and Discussions

 
GeneralRe: memory leaks!!! Pin
trudy_la_fee19-Aug-03 0:26
trudy_la_fee19-Aug-03 0:26 
GeneralRe: memory leaks!!! Pin
djjames19-Aug-03 20:33
djjames19-Aug-03 20:33 
Generaldialog please example Pin
Haitham Khedre22-May-03 6:35
Haitham Khedre22-May-03 6:35 
GeneralProblems using keys and various CMenu calls Pin
Michael Pauli13-May-03 23:09
Michael Pauli13-May-03 23:09 
QuestionWhat about MenuXP versions? Pin
Jaime Stuardo8-Apr-03 16:44
Jaime Stuardo8-Apr-03 16:44 
AnswerRe: What about MenuXP versions? Pin
Yaozhifeng9-Apr-03 14:56
sussYaozhifeng9-Apr-03 14:56 
GeneralDocking or align the CToolBarXP Pin
BlackNM31-Mar-03 4:17
sussBlackNM31-Mar-03 4:17 
GeneralRe: Docking or align the CToolBarXP Pin
Neil Yao1-Apr-03 17:39
Neil Yao1-Apr-03 17:39 
This control is not designed to dock. If you must dock the control, use a dialogbar and put the control in it.
QuestionMenuXP For Dialog Based Project? Pin
Da_Hero11-Feb-03 0:38
professionalDa_Hero11-Feb-03 0:38 
AnswerRe: MenuXP For Dialog Based Project? Pin
Neil Yao12-Feb-03 16:54
Neil Yao12-Feb-03 16:54 
GeneralRe: MenuXP For Dialog Based Project? Pin
Anonymous17-May-03 22:32
Anonymous17-May-03 22:32 
GeneralI never move to the sub menu Pin
zyz27-Jan-03 16:37
zyz27-Jan-03 16:37 
GeneralRe: I never move to the sub menu Pin
Neil Yao28-Jan-03 15:28
Neil Yao28-Jan-03 15:28 
GeneralExcel XP color selection style Pin
Member 13544818-Dec-02 22:11
Member 13544818-Dec-02 22:11 
GeneralRe: Excel XP color selection style Pin
Neil Yao18-Dec-02 22:15
Neil Yao18-Dec-02 22:15 
GeneralRe: Excel XP color selection style Pin
Member 13544818-Dec-02 23:01
Member 13544818-Dec-02 23:01 
GeneralRe: Excel XP color selection style Pin
Neil Yao19-Dec-02 14:00
Neil Yao19-Dec-02 14:00 
Generalsome thing,like a bug. Pin
rambo_bb17-Dec-02 19:13
rambo_bb17-Dec-02 19:13 
GeneralRe: some thing,like a bug. Pin
Neil Yao17-Dec-02 19:26
Neil Yao17-Dec-02 19:26 
Generalthe menu's color Pin
Pretty Bug22-Oct-02 22:32
Pretty Bug22-Oct-02 22:32 
GeneralRe: the menu's color Pin
Neil Yao22-Oct-02 22:44
Neil Yao22-Oct-02 22:44 
GeneralDynamically filling submenus Pin
S Ganapathi Raman30-Jul-02 3:10
S Ganapathi Raman30-Jul-02 3:10 
Questionhow to load current menu-resource?? Pin
22-May-02 18:20
suss22-May-02 18:20 
Questionhow to change the color of the toolbar? Pin
1-May-02 18:17
suss1-May-02 18:17 
QuestionHow Can I use the Menu Resource ? Pin
29-Apr-02 15:49
suss29-Apr-02 15:49 

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.