Click here to Skip to main content
Click here to Skip to main content

Add icons to standard menus in WindowsXP and Windows Vista

, 24 Nov 2007
Rate this:
Please Sign up or sign in to vote.
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.

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

rjlmmaas

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 PinmemberPacestar Coder8-Dec-07 12:42 
GeneralThanks, great code. Pinmemberdonald1284-Dec-07 23:36 

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.150123.1 | Last Updated 24 Nov 2007
Article Copyright 2007 by rjlmmaas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid