Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C++
Article

Add icons to standard menus in WindowsXP and Windows Vista

Rate me:
Please Sign up or sign in to vote.
3.21/5 (5 votes)
24 Nov 20072 min read 26K   635   14   2
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


Written By
Netherlands Netherlands
I am a developing software as a hobby for over 10 years now. The focus have been on imaging applications and card games.

Comments and Discussions

 
GeneralVista transparency issue Pin
Pacestar Coder8-Dec-07 11:42
Pacestar Coder8-Dec-07 11:42 
GeneralThanks, great code. Pin
donald1284-Dec-07 22:36
donald1284-Dec-07 22:36 

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.