Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Add icons to standard menus in WindowsXP and Windows Vista

3.21/5 (5 votes)
24 Nov 20072 min read 1   639  
Add icons to standard menus in WindowsXP and Windows Vista
Screenshot - MenuIcon1.png Screenshot - MenuIcon2.png

Introduction

This project simplifies the use of icons in menus in WindowsXP and Windows Vista C++ projects. The project consist of a small class called 'CMenuIcon' which can be incorporated quickly in any Windows C++ project.

Background

There a lot of examples on the Internet of how icons can be added to menus. However, most example are based on ownerdrawn menus. The disadvantage of this approach is that with every new version of Windows the style of the menus will look outdated quickly. Also for a small application a class for an ownerdrawn menu is a bit overkill. These were my reasons to search for an easy-to-use method with a small code base.

I found on the internet two examples which could do that, however these examples were WindowsXP or Windows Vista only. My class 'CMenuIcon' combines these to examples into one class.

Using the code

The class can be incorporated in three steps into your C++ project.

The first step is to create an instance of the CMenuIcon class and add combinations of a command-id and a icon (bitmap). This can be done in the initialisation steps of your application. It is advised to use only one instance of the CMenuIcon class, so you have global combinations of commands and icons.

C++
//Create an instance of the CMenuIcon class
oMenuIcon = new CMenuIcon(hInstance);

//Add the icon-command combinations
oMenuIcon->AddMenuCommandIcon(ID_FILE_NEW, IDB_NEW, RGB(255,0,255));
oMenuIcon->AddMenuCommandIcon(ID_FILE_OPEN, IDB_OPEN, RGB(255,0,255));
oMenuIcon->AddMenuCommandIcon(ID_FILE_SAVE, IDB_SAVE, RGB(255,0,255));

The second step is processing the menu. The menu has to be processed because the icons has to be associated with it. You don't have to make any changes to your menu resources. The class will handle this.

C++
//Process the menu
hCurrentMenu = GetMenu(hWnd);
oMenuIcon->ProcessMenu(hCurrentMenu);

The final step is an addition to your WndProc.

C++
//This is an addition for ownerdrawing icons in WindowsXP
if (oMenuIcon->WndProc(hWnd, message, wParam, lParam, iReturn)) return iReturn;

Points of Interest

See this class as a proof-of-concept. There are a lot of possible enhancement. For instance; my class uses GDI+ to convert an RGB bitmap to a ARGB bitmap to be used in Windows Vista. This can be done a lot quicker and certainly cleaner.

History

Version 1.0: Initial release.

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