65.9K
CodeProject is changing. Read more.
Home

Add icons to standard menus in WindowsXP and Windows Vista

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.21/5 (5 votes)

Nov 24, 2007

2 min read

viewsIcon

26262

downloadIcon

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.

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

//Process the menu

hCurrentMenu = GetMenu(hWnd);
oMenuIcon->ProcessMenu(hCurrentMenu);

The final step is an addition to your WndProc.

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