Click here to Skip to main content
11,582,849 members (73,033 online)
Click here to Skip to main content

A Powerfull Ownerdraw Menu

, 23 Oct 2001 369.3K 7.6K 107
Rate this:
Please Sign up or sign in to vote.
An XP-style ownerdrawn menu with support for background images and icon shadow.
<!-- Download Links --> <!-- Article image -->

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

<!-- Add the rest of your HTML here -->

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:-

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:-

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

Share

About the Author

Neil Yao
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 | :)

You may also be interested in...

Comments and Discussions

 
Questionmenu created but menu itmes not displaying any text Pin
omkarpardeshi12327-Sep-12 0:46
memberomkarpardeshi12327-Sep-12 0:46 
QuestionHere how I disable menu items Pin
bredator22-Aug-12 23:41
memberbredator22-Aug-12 23:41 
Generalbug with background bitmaps Pin
tonim16-Apr-10 23:07
membertonim16-Apr-10 23:07 
Questionwhy these icons show only once?? Pin
guyuewuhua15-Sep-09 23:42
memberguyuewuhua15-Sep-09 23:42 
GeneralExcellent work Pin
k77710-Mar-09 5:15
memberk77710-Mar-09 5:15 
Generalhi budbak!! Pin
Sreenivas00310-Oct-07 23:42
memberSreenivas00310-Oct-07 23:42 
Generalthis can not used as a popup menu for the woodtray Pin
Member #25656674-Feb-07 20:35
memberMember #25656674-Feb-07 20:35 
GeneralI found sth. like bug Pin
jiangfm8-Aug-06 18:00
memberjiangfm8-Aug-06 18:00 
Questionhow to create a owned draw menu with xp style? Pin
sting_lee5-Jul-06 22:03
membersting_lee5-Jul-06 22:03 
QuestionChanging Menu Font at run-time ??? Pin
ana_v1235-Apr-06 21:29
memberana_v1235-Apr-06 21:29 
Thanks for this excellent article.

How To change menu font (font of menu and submenu items) at runtime ?

Is it possible ? How to implement it ?

BCGControlBar library has somehow implemented it (but that library is too expensive for

small project)

Any idea or direction ?

Ana

Ana_v123
GeneralNot owner-drawn when built on w2K/AS with VC6 Pin
AceWizardly7-Jul-05 2:12
memberAceWizardly7-Jul-05 2:12 
QuestionHow to disable popUp menu items? Pin
MunazzahNawaz12-Apr-05 10:06
memberMunazzahNawaz12-Apr-05 10:06 
GeneralSize not calculated for popup items Pin
naveed21-Oct-04 9:44
membernaveed21-Oct-04 9:44 
GeneralAdditionnal features Pin
asenechal26-Apr-04 4:33
memberasenechal26-Apr-04 4:33 
GeneralWeird behavior on NT 4 Pin
asenechal26-Apr-04 4:29
memberasenechal26-Apr-04 4:29 
GeneralRe: Weird behavior on NT 4 Pin
Yao Zhifeng26-Apr-04 16:08
memberYao Zhifeng26-Apr-04 16:08 
GeneralConversion to Active X Pin
hprahul24-Mar-04 19:42
memberhprahul24-Mar-04 19:42 
GeneralRe: Conversion to Active X Pin
Yao Zhifeng24-Mar-04 21:48
memberYao Zhifeng24-Mar-04 21:48 
GeneralRe: Conversion to Active X Pin
hprahul25-Mar-04 4:23
memberhprahul25-Mar-04 4:23 
GeneralXP colored side bar Pin
webteca21-Aug-03 4:53
memberwebteca21-Aug-03 4:53 
GeneralXP icon colors: only 256 Pin
webteca20-Aug-03 4:02
memberwebteca20-Aug-03 4:02 
GeneralRe: XP icon colors: only 256 Pin
Yao Zhifeng20-Aug-03 15:01
memberYao Zhifeng20-Aug-03 15:01 
GeneralSideBar Pin
trudy_la_fee20-Aug-03 2:47
susstrudy_la_fee20-Aug-03 2:47 
GeneralRe: SideBar Pin
Yao Zhifeng20-Aug-03 14:55
memberYao Zhifeng20-Aug-03 14:55 
GeneralMeasure Item Pin
trudy_la_fee18-Aug-03 3:24
susstrudy_la_fee18-Aug-03 3:24 
GeneralRe: Measure Item Pin
Yao Zhifeng18-Aug-03 15:01
memberYao Zhifeng18-Aug-03 15:01 
GeneralRe: Measure Item Pin
trudy_la_fee19-Aug-03 0:12
susstrudy_la_fee19-Aug-03 0:12 
GeneralRe: Measure Item Pin
naveed25-Oct-04 8:22
membernaveed25-Oct-04 8:22 
GeneralAssistance please Pin
RobertoSchipp10-Aug-03 7:42
memberRobertoSchipp10-Aug-03 7:42 
GeneralRe: Assistance please Pin
Yao Zhifeng11-Aug-03 15:00
memberYao Zhifeng11-Aug-03 15:00 
Generalmemory leaks!!! Pin
djjames21-Jul-03 3:56
memberdjjames21-Jul-03 3:56 
GeneralRe: memory leaks!!! Pin
Yao Zhifeng21-Jul-03 22:13
memberYao Zhifeng21-Jul-03 22:13 
GeneralRe: memory leaks!!! Pin
trudy_la_fee19-Aug-03 0:26
susstrudy_la_fee19-Aug-03 0:26 
GeneralRe: memory leaks!!! Pin
djjames19-Aug-03 20:33
memberdjjames19-Aug-03 20:33 
Generaldialog please example Pin
zero_code22-May-03 6:35
memberzero_code22-May-03 6:35 
GeneralProblems using keys and various CMenu calls Pin
Michael Mogensen13-May-03 23:09
memberMichael Mogensen13-May-03 23:09 
QuestionWhat about MenuXP versions? Pin
jstuardo8-Apr-03 16:44
memberjstuardo8-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
Yao Zhifeng1-Apr-03 17:39
memberYao Zhifeng1-Apr-03 17:39 
QuestionMenuXP For Dialog Based Project? Pin
HackMan11-Feb-03 0:38
memberHackMan11-Feb-03 0:38 
AnswerRe: MenuXP For Dialog Based Project? Pin
Yao Zhifeng12-Feb-03 16:54
memberYao Zhifeng12-Feb-03 16:54 
GeneralRe: MenuXP For Dialog Based Project? Pin
Anonymous17-May-03 22:32
sussAnonymous17-May-03 22:32 
GeneralI never move to the sub menu Pin
zyz27-Jan-03 16:37
memberzyz27-Jan-03 16:37 
GeneralRe: I never move to the sub menu Pin
Yao Zhifeng28-Jan-03 15:28
memberYao Zhifeng28-Jan-03 15:28 
GeneralExcel XP color selection style Pin
PetrK18-Dec-02 22:11
memberPetrK18-Dec-02 22:11 
GeneralRe: Excel XP color selection style Pin
Yao Zhifeng18-Dec-02 22:15
memberYao Zhifeng18-Dec-02 22:15 
GeneralRe: Excel XP color selection style Pin
PetrK18-Dec-02 23:01
memberPetrK18-Dec-02 23:01 
GeneralRe: Excel XP color selection style Pin
Yao Zhifeng19-Dec-02 14:00
memberYao Zhifeng19-Dec-02 14:00 
Generalsome thing,like a bug. Pin
rambo_bb17-Dec-02 19:13
memberrambo_bb17-Dec-02 19:13 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150603.1 | Last Updated 24 Oct 2001
Article Copyright 2001 by Neil Yao
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid