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

Menu Bitmaps from Minimal Source Code

By , 1 Feb 2002
 

A menu with bitmaps

Introduction

This article describes a method of adding bitmaps to your menus. The method is very easy to use, for example adding all the bitmaps on a tool bar is accomplished by a single function call. The demo project includes a class called BitmapMenu that can be used to add bitmap menus to your own projects. This class is made up of a small amount of source code, making it easy to understand and maintain.

There are two good related articles on the code project about adding bitmaps to menus. They are:

These are both excellent articles with lots of good accompanying code. Brent Corkum's code in particular produces nice looking bitmaps, and it has been extensively tested and updated. So of course you are thinking: "Why do I need to bother with your article?"

The main disadvantage of the Corkum method is the size of the code. Something as simple as placing bitmaps on menus requires a significant amount of code which must be maintained by you (or you have to hope that Mr. Corkum is nice enough to keep updating his classes). For this reason I chose to use the much more compact Denisov method. Nikolay Denisov's article addresses much more than putting bitmaps on menus, and the bitmap code is intertwined with his other code, so it is difficult to use independently. Therefore, I developed a class based on the Denisov method that is modular. The result is that both my method and the Corkum method are easy to setup, but there is a significant difference in the resulting amount of source code. The approximate lines of source code required by the two methods are:

  • Corkum method: 2,350 lines
  • This method: 250 lines

Unlike Corkum's work, this code has not been well tested. It has only been tested under Windows 98, and will not work under Windows 95/NT.

To use this code download the demo project. The demo has a second tool bar which demonstrates two of the class member functions: AddToolBar and RemoveToolBar. To add bitmap menus to your own project follow these 5 steps:

  1. Add the following files to your project:
    BitmapMenu.cpp, BitmapMenu.h, winuser2.h
    
  2. In MainFrm.h - Add this line to the top of the file:
    #include "BitmapMenu.h"
    
  3. In MainFrm.h - Inherit your main frame window from BitmapMenu rather than from CFrameWnd or CMDIFrameWnd.
    class CMainFrame : public BitmapMenu<CFrameWnd>
    
  4. In MainFrm.cpp - Add three message handlers between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP.
    ON_WM_INITMENUPOPUP()
    ON_WM_MEASUREITEM()
    ON_WM_DRAWITEM()
    
  5. In MainFrm.cpp - At the end of the CMainFrame::OnCreate() function add each tool bar that you want to appear on the menu:
    AddToolBar(&m_wndToolBar);
    

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Warren Gardner

United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionImage dimensions Pinmemberandrewtruckle4-Nov-09 22:02 
Hello
 
This is a great class. Small code set for simple functionality.
 
My problem is that my toolbar uses a larger set of images and therefore the menu now looks silly because it has expanded to accomodate the images.
 
What I need is a way to force the images down to the smaller usual dimensions and then it will be fine.
 
How can I do this?
 
Andrew
QuestionHow to add bitmaps for menu items without using the app resource. i.e. using external bmp file at run time. PinmemberbrijMos12-Aug-08 21:28 
Please tell me the way to set the menu items bitmaps from external files i.e. not using the resource of the application.
 
Please give me a sample code to do dis......
GeneralThank you very much PinmemberYogesh P. Dhakad17-Jul-07 4:50 
Dear Warren Gardner,
 
Thanks for giving a small and consise code. With this minimal code, I could successful get my menus to display 24 Bit Full Color icons on the menus (I had previously got them working on the toolbars)
 
Thanks also to;
Brent Corkum
Nikolay Denisov
 
Best regards and all the best for the future,
Yogesh Dhakad
QuestionHow to change checkmark style? PinmemberLZL101023-Aug-06 20:08 
this class is very easy to use,but how to change checkmark style?
 
Any help will definitely be apprecicated.

GeneralTo show or hide the toolbar PinmemberHanney Wang13-Sep-04 16:18 
The second toolbar always remain visible either click "Add Toolbar" or "Remove Toolbar"; I added some codes to complete the show or hide the toolbar, it also can prevent the srange appears after calling CToolBar::ShowWindow(SW_HIDE).
 
void CMainFrame::OnViewAddtoolbar()
{
AddToolBar(&m_wndToolBar2);
// These 2 lines added to show toolbar
ShowControlBar(&m_wndToolBar2, TRUE, FALSE);
RecalcLayout();
}
 
void CMainFrame::OnViewRemovetoolbar()
{
RemoveToolBar(&m_wndToolBar2);
// These 2 lines added to hide toolbar
ShowControlBar(&m_wndToolBar2, FALSE, FALSE);
RecalcLayout();
}
 
Smile | :)
 
Regards,
Hanney
GeneralFix to solve crashes on Windows NT/95 PinmemberPEK8-Sep-04 8:59 
If you run an application with BitmapMenu on Windows NT/95 the application will crash when the menu is drawn (it will at least do this on NT). The simple solution to this is to disable bitmaps when running on this system (so a classic menu without bitmaps will be used). The following code will do this. It should be placed in BitmapMenu:OnInitMenuPopup after the base class is called:
 
//Version check will only be run once per instance
  //(updating from Win95/NT to other system without rebooting
  //is not very likely :-).
  static int allowBitmapMenu = -1;
 
  if(allowBitmapMenu == -1)
  {
    allowBitmapMenu = 1;
 
    OSVERSIONINFO version;
    version.dwOSVersionInfoSize = sizeof(version);
    GetVersionEx(&version);
 
    //Running NT/95 ?
    if(  version.dwMajorVersion <= 4 && version.dwMinorVersion <= 0 )
    {
      if(version.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
      {
        TRACE(_T("Running Windows 95. No bitmaps in menus.\n"));
        allowBitmapMenu = 0;
      }
      else if(version.dwPlatformId == VER_PLATFORM_WIN32_NT)
      {
        TRACE(_T("Running Windows NT. No bitmaps in menus.\n"));
        allowBitmapMenu = 0;
      }
 
    }
  }
 
  //Bitmaps are not allowed
  if(allowBitmapMenu == 0)
    return;
 
I have tested this code in 95 and NT (no bitmaps), 98 and XP and works as expected.

QuestionHelp - Context Menu? PinmemberJayant_Mukherjee8-Jul-04 18:53 
How to use this Menu as a Context Menu in a SDI/MDI application's View.Confused | :confused:
 
Jayant Mukherjee
GeneralMenu Bitmaps from Hidden Toolbars Pinmembervjplusplus21-Mar-04 23:09 
Bitmaps are displayed for menu items which have corresponding toolbar items.
 
I want to display bitmaps for some menu items which do not have a correspoding tool bar item. Hence I created a hidden toolbar to show the menu bitmaps for such menu items. It works in the release mode but crashes is debug mode before the menu items are displayed, Debug Assertion failure happens due to line 160 in bitmapmenu.cpp. When I comment this ASSERT statement, the menu items are shown (broken display) only if I move the my mouse cursor over them.
 
Any help to solve this problem will be appreciated.OMG | :OMG:
GeneralRe: Menu Bitmaps from Hidden Toolbars PinmemberWarren Gardner29-Mar-04 5:24 
GeneralStudent from Poland need help with menu Pinmemberhaiaw22-May-03 23:08 
Hi, in my project, i have simle menu inserted from Microsoft Visual C++ 6 (Insert->Resource->Menu) and i would add to some positions somie icons or bitmaps (now only some small picture next to the one position in my popup menu) I am the begginer and it's too coplicated for me so if somebody could help me please??? You could add only one icon (it's important to do this in the simplest way for me) and i would learn it from the basis. I began my project, it's very simple, and even my teacher doesn't know how to put icon in menu. Our base is "Programming in Windows" - Petzold. My project need to be finished and with such icons i'll get a higher mark. I WOULD LIKE TO SEND IT TO SOMEODY, WHO COULD ADD ONLY A PIECE OF SOURCE TO MY MENU. fenders@interia.pl PLEASEEEEEE
QuestionWin32 SDK Example? Pinmemberajax197124-Jan-03 4:04 
Great example! This is exactly the stuff that I was looking for, but unfortunately I don't use MFC. I don't suppose you know how to get this work using the pure win32 SDK/API? Thanks.
AnswerRe: Win32 SDK Example? PinmemberWarren Gardner4-Feb-03 10:41 
GeneralRe: Win32 SDK Example? PinmemberTimWallace16-Jun-03 4:13 
GeneralRe: Win32 SDK Example? PinmemberHallsoft27-Aug-04 0:12 
GeneralChange checkmarks style Pinmembercaranthol17-Jan-03 6:51 
Hi, Warren.
 
I would like to know how can I change the style of the checkmarks, which appear together de menu item. For example, how can I put them inside a rectangle.
 
Thank you.
GeneralRe: Change checkmarks style PinmemberWarren Gardner4-Feb-03 10:35 
GeneralCompile error PinmemberLinas23-May-02 22:19 
I like this BmpMenu, but after including
#include "BitmapMenu.h" I get many errors:
 
c:\..\vc98\include\new(35) : error C2061: syntax error : identifier 'THIS_FILE'
c:\..\vc98\include\new(35) : error C2091: function returns function
c:\..vc98\include\new(35) : error C2809: 'operator new' has no formal parameters
c:\..\vc98\include\new(37) : error C2556: 'void *(__cdecl *__cdecl operator new(void))(unsigned int,const struct std::nothrow_t &)' : overloaded function differs only by return type from 'void *(__cdecl *__cdecl op
erator new(void))(unsigned int)'
c:\..\vc98\include\new(35) : see declaration of 'new'
..
..
c:\..\vc98\include\memory(20) : error C2954: template definitions cannot nest
 
I'm using VC++ 6.0
Maybe someone knows that to do?
GeneralRe: Compile error PinmemberWarren Gardner28-May-02 7:54 
Generalthe BitmapMenu project doesn't work Pinmembersohi4-May-02 4:50 
HI, first thanx to you, I have a poject to present to my teacher about the Bitmap menu, i did all steps that you said but i get 6 errors which says :
'messageMap' : cannot access protected member declared in class 'CMDIFrameWnd'
Can you please tell me what should i do?Blush | :O Blush | :O
 
Sohi
GeneralRe: the BitmapMenu project doesn't work PinmemberWarren Gardner15-May-02 8:28 
GeneralRe: the BitmapMenu project doesn't work Pinmembercaranthol2-Jan-03 0:44 
GeneralWhen the toolbar can be customized... Pinmemberwuwuli4-Apr-02 19:57 
Very nice code! I have converted it to a plain win32 API version. A problem:
When the toolbar can be customized, some icons on toolbar may be hided. The code cannot assign bitmaps to menu items whose correspondance ID is hidden on current toolbar...
GeneralRe: When the toolbar can be customized... PinmemberWarren Gardner10-Apr-02 4:26 
GeneralRe: When the toolbar can be customized... PinmemberSiuming10-Apr-02 23:45 
GeneralRe: When the toolbar can be customized... PinmemberWarren Gardner19-Apr-02 16:43 
QuestionIs there any way to show menu items with different colors and /or fonts? PinmemberGhasem Karimi26-Mar-02 2:15 
Is there any wauy to show menu items with different colors and /or fonts?
 
I need to change menu items color to another color (like WTL)menus, actually I don't know where (in your nice code)to write my OnDrawItem function.
will u help me?Confused | :confused:
AnswerRe: Is there any way to show menu items with different colors and /or fonts? PinmemberWarren Gardner10-Apr-02 4:15 
GeneralA few problems PinmemberNeville Franks3-Feb-02 22:17 
Hi Warren,
Thanks for the contribution to CP. I've noticed a few problems. First it isn't using the menu colors I have setup in Control Panel. And second when I move over a menu item which is disabled and has a bitmap the selection goes over the bitmap when it shouldn't. This is just with your demo app. I am running WinXP but I doubt whether that's relevant.
 
Neville Franks, Author of ED for Windows. www.getsoft.com
GeneralRe: A few problems PinmemberWarren Gardner4-Feb-02 12:23 
GeneralRe: A few problems PinmemberNeville Franks6-Feb-02 1:24 
GeneralGood peace of code, but... PinmemberJean-Michel LE FOL3-Feb-02 6:54 
It's very simple to use. I tested it on Win2000 and NT4.
Cry | :(( On this last system, it can't work because of the dynamic calls to GetMenuInfoWin50 and SetMenuInfoWin50. They are not available.
Do you have an idea to replace ?
GeneralRe: Good peace of code, but... PinmemberWarren Gardner4-Feb-02 15:00 
GeneralRe: Good peace of code, but... PinmemberPierre C12-Feb-02 4:12 
GeneralAbout dialog. Pinmemberdsteuz2-Feb-02 18:24 
How to set this menu to dialog based application?
GeneralRe: About dialog. PinmemberWarren Gardner4-Feb-02 14:28 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 2 Feb 2002
Article Copyright 2002 by Warren Gardner
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid