Click here to Skip to main content
11,712,454 members (77,562 online)
Click here to Skip to main content

Menu Bitmaps from Minimal Source Code

, 1 Feb 2002 CPOL 200.4K 2.9K 51
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Warren Gardner
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionImage dimensions Pin
andrewtruckle4-Nov-09 22:02
memberandrewtruckle4-Nov-09 22:02 
QuestionHow to add bitmaps for menu items without using the app resource. i.e. using external bmp file at run time. Pin
brijMos12-Aug-08 21:28
memberbrijMos12-Aug-08 21:28 
GeneralThank you very much Pin
Yogesh P. Dhakad17-Jul-07 4:50
memberYogesh P. Dhakad17-Jul-07 4:50 
QuestionHow to change checkmark style? Pin
LZL101023-Aug-06 20:08
memberLZL101023-Aug-06 20:08 
GeneralTo show or hide the toolbar Pin
Hanney Wang13-Sep-04 16:18
memberHanney Wang13-Sep-04 16:18 
GeneralFix to solve crashes on Windows NT/95 Pin
PEK8-Sep-04 8:59
memberPEK8-Sep-04 8:59 
QuestionHelp - Context Menu? Pin
Jayant_Mukherjee8-Jul-04 18:53
memberJayant_Mukherjee8-Jul-04 18:53 
GeneralMenu Bitmaps from Hidden Toolbars Pin
vjplusplus21-Mar-04 23:09
membervjplusplus21-Mar-04 23:09 
GeneralRe: Menu Bitmaps from Hidden Toolbars Pin
Warren Gardner29-Mar-04 5:24
memberWarren Gardner29-Mar-04 5:24 
I wasn't able to reproduce this problem. I took the example code included in this article and removed the WS_VISIBLE style from the second toolbar (line 65 in MainFrm.cpp). It worked fine in debug mode.

GeneralStudent from Poland need help with menu Pin
haiaw22-May-03 23:08
memberhaiaw22-May-03 23:08 
QuestionWin32 SDK Example? Pin
ajax197124-Jan-03 4:04
memberajax197124-Jan-03 4:04 
AnswerRe: Win32 SDK Example? Pin
Warren Gardner4-Feb-03 10:41
memberWarren Gardner4-Feb-03 10:41 
GeneralRe: Win32 SDK Example? Pin
TimWallace16-Jun-03 4:13
memberTimWallace16-Jun-03 4:13 
GeneralRe: Win32 SDK Example? Pin
Hallsoft27-Aug-04 0:12
memberHallsoft27-Aug-04 0:12 
GeneralChange checkmarks style Pin
caranthol17-Jan-03 6:51
membercaranthol17-Jan-03 6:51 
GeneralRe: Change checkmarks style Pin
Warren Gardner4-Feb-03 10:35
memberWarren Gardner4-Feb-03 10:35 
GeneralCompile error Pin
Linas23-May-02 22:19
memberLinas23-May-02 22:19 
GeneralRe: Compile error Pin
Warren Gardner28-May-02 7:54
memberWarren Gardner28-May-02 7:54 
Generalthe BitmapMenu project doesn't work Pin
sohi4-May-02 4:50
membersohi4-May-02 4:50 
GeneralRe: the BitmapMenu project doesn't work Pin
Warren Gardner15-May-02 8:28
memberWarren Gardner15-May-02 8:28 
GeneralRe: the BitmapMenu project doesn't work Pin
caranthol2-Jan-03 0:44
membercaranthol2-Jan-03 0:44 
GeneralWhen the toolbar can be customized... Pin
wuwuli4-Apr-02 19:57
memberwuwuli4-Apr-02 19:57 
GeneralRe: When the toolbar can be customized... Pin
Warren Gardner10-Apr-02 4:26
memberWarren Gardner10-Apr-02 4:26 
GeneralRe: When the toolbar can be customized... Pin
Siuming10-Apr-02 23:45
memberSiuming10-Apr-02 23:45 
GeneralRe: When the toolbar can be customized... Pin
Warren Gardner19-Apr-02 16:43
memberWarren 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
memberGhasem 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
memberWarren Gardner10-Apr-02 4:15 
GeneralA few problems Pin
Neville Franks3-Feb-02 22:17
memberNeville Franks3-Feb-02 22:17 
GeneralRe: A few problems Pin
Warren Gardner4-Feb-02 12:23
memberWarren Gardner4-Feb-02 12:23 
GeneralRe: A few problems Pin
Neville Franks6-Feb-02 1:24
memberNeville Franks6-Feb-02 1:24 
GeneralGood peace of code, but... Pin
Jean-Michel LE FOL3-Feb-02 6:54
memberJean-Michel LE FOL3-Feb-02 6:54 
GeneralRe: Good peace of code, but... Pin
Warren Gardner4-Feb-02 15:00
memberWarren Gardner4-Feb-02 15:00 
GeneralRe: Good peace of code, but... Pin
Pierre C12-Feb-02 4:12
memberPierre C12-Feb-02 4:12 
GeneralAbout dialog. Pin
dsteuz2-Feb-02 18:24
memberdsteuz2-Feb-02 18:24 
GeneralRe: About dialog. Pin
Warren Gardner4-Feb-02 14:28
memberWarren Gardner4-Feb-02 14:28 

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
Web03 | 2.8.150819.1 | Last Updated 2 Feb 2002
Article Copyright 2002 by Warren Gardner
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid