Click here to Skip to main content
15,887,812 members
Articles / Desktop Programming / MFC
Article

Menu Bitmaps from Minimal Source Code

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
1 Feb 2002CPOL2 min read 235.5K   3.3K   53   35
Add bitmaps to your menus easily and with very little source code.

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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWhen the toolbar can be customized... Pin
4-Apr-02 19:57
suss4-Apr-02 19:57 
GeneralRe: When the toolbar can be customized... Pin
Warren Gardner10-Apr-02 4:26
Warren Gardner10-Apr-02 4:26 
GeneralRe: When the toolbar can be customized... Pin
nilaysoft10-Apr-02 23:45
nilaysoft10-Apr-02 23:45 
GeneralRe: When the toolbar can be customized... Pin
Warren Gardner19-Apr-02 16:43
Warren Gardner19-Apr-02 16:43 
QuestionIs there any way to show menu items with different colors and /or fonts? Pin
Ghasem Karimi26-Mar-02 2:15
Ghasem Karimi26-Mar-02 2:15 
AnswerRe: Is there any way to show menu items with different colors and /or fonts? Pin
Warren Gardner10-Apr-02 4:15
Warren Gardner10-Apr-02 4:15 
GeneralA few problems Pin
Neville Franks3-Feb-02 22:17
Neville Franks3-Feb-02 22:17 
GeneralRe: A few problems Pin
Warren Gardner4-Feb-02 12:23
Warren Gardner4-Feb-02 12:23 
Neville,

> it isn't using the menu colors I have setup in Control Panel

I tested it using a variety of appearance schemes and menu colors, and it works fine on my system (Win 98). Can you give me some more specifics about what is happening? Since I have only tested it under Win 98, it is possible that this problem is related to using it under XP. However, that seems strange since I am using standard methods of getting the colors such as: GetSysColor(COLOR_BTNSHADOW).


> when I move over a menu item which is disabled and has a bitmap
> the selection goes over the bitmap when it shouldn't

Yes, this is by TTMO design (Time To Move On Wink | ;) ). I spent quite a bit of time trying to get a different behavior, but finally got sick of working on it. Actually I don't even like the default MFC behavior, which is that disabled menu items without bitmaps get highlighted (when the mouse is on top of it). This is not how a disabled button acts. It seems to me that the correct behavior is that the entire menu item (bitmap and text) does not get highlighted when it is disabled. However, I think the only way to get that behavior would be to make the disabled items owner drawn.

I wrote some code that would highlight the disabled menu text, but not the disabled menu bitmap. It was working well, but I discovered that it would not work under all appearance schemes so I removed it. I don't really understand what is going on, but I will describe it and maybe someone can enlighten me:

When a disabled menu item is not highlighted, the bitmap area acts like a monochrome display and bitmaps that are drawn will appear in only one color (and you get that highlighted shadow for free). However, when the disabled menu item is highlighted , the bitmap area acts like a color display. If that was all there was to it that would be fine, however after writing code to deal with this I found that under a few appearance schemes the bitmap area acts like the monochrome display even when highlighted. One of these appearance schemes is "Rose". Since I could not figure out how to detect which type of display was currently being used, I could not draw the bitmap correctly in all instances.

I looked at things like current brushes, default bitmaps, etc. but could never make sense of this. If anyone knows what is going on, it would probably be pretty easy for me to update the code, since I have a version that produces an unhighlighted disabled bitmap under most appearance schemes.

Warren



GeneralRe: A few problems Pin
Neville Franks6-Feb-02 1:24
Neville Franks6-Feb-02 1:24 
GeneralGood peace of code, but... Pin
Jean-Michel LE FOL3-Feb-02 6:54
Jean-Michel LE FOL3-Feb-02 6:54 
GeneralRe: Good peace of code, but... Pin
Warren Gardner4-Feb-02 15:00
Warren Gardner4-Feb-02 15:00 
GeneralRe: Good peace of code, but... Pin
Pierre C12-Feb-02 4:12
Pierre C12-Feb-02 4:12 
GeneralAbout dialog. Pin
2-Feb-02 18:24
suss2-Feb-02 18:24 
GeneralRe: About dialog. Pin
Warren Gardner4-Feb-02 14:28
Warren Gardner4-Feb-02 14:28 

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.