Click here to Skip to main content
13,793,024 members
Click here to Skip to main content


51 bookmarked
Posted 1 Feb 2002
Licenced CPOL

Menu Bitmaps from Minimal Source Code

, 1 Feb 2002
Add bitmaps to your menus easily and with very little source code.
// Copyright (c) 2002 by Warren Gardner (
// This code is free for personal and commercial use, providing this 
// notice remains intact in the source files and all eventual changes are
// clearly marked with comments. You must obtain the author's consent before 
// you can include this code in a software library. If this source code is 
// used in a commercial application please send me an e-mail so I can check 
// out your product.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
// You can send me bug reports, bug fixes, enhancements, etc. or post them 
// to the codeproject:

#if !defined __BITMAPMENU_H__
#define __BITMAPMENU_H__

// Ignore long label warning when using the STL
#pragma warning( disable : 4786)

#include <set>

// Class: BitmapMenu<T_FrameWnd>
// This class draws tool bar buttons onto menus.

template <class T_FrameWnd>
class BitmapMenu : public T_FrameWnd
	virtual ~BitmapMenu();

	// Add and remove menu bitmaps by adding and removing tool bars.
	void AddToolBar(CToolBar *toolBar) { m_toolBars.insert(toolBar); }
	void RemoveToolBar(CToolBar *toolBar) { m_toolBars.erase(toolBar); }

	// Menu message handlers.
	afx_msg void OnInitMenuPopup(CMenu *pPopupMenu, UINT nIndex, 
								 BOOL bSysMenu);
	afx_msg void OnMeasureItem(int nIDCtl, 
							   LPMEASUREITEMSTRUCT lpMeasureItemStruct);
	afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);

	// Given a menu item ID, get the corresponding image list and image ID.
	void GetImage(UINT menuID, int &imageID, CImageList *&imageList);

	// Draw a raised button.
	void DrawButton(CImageList *imageList, CDC *dc, CSize &buttonSize);

	// Draw a disabled menu item by converting a color bitmap to monochrome.
	void DrawDisabled(CImageList *imageList, CDC *dc, int imageID, 
					  CPoint &position, CSize &size);

	// Menu bitmaps are retrieved from this set of tool bars.
	std::set<CToolBar*> m_toolBars;

	// Number of pixels between the start of the text area 
	// and the first character of menu text.
	const UINT m_textSpace;

#endif // __BITMAPMENU_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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

You may also be interested in...

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