Click here to Skip to main content
11,578,524 members (58,402 online)
Click here to Skip to main content
Add your own
alternative version

Menu Bitmaps from Minimal Source Code

, 1 Feb 2002 CPOL 199.8K 2.9K 51
Add bitmaps to your menus easily and with very little source code.
bmpmenu_demo.zip
BmpMenu
BmpMenu.dsw
BmpMenu.dsp
res
BmpMenuDoc.ico
BmpMenu.ico
Toolbar.bmp
toolbar1.bmp
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2002 by Warren Gardner (warren555@hotmail.com).
//
// 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: http://www.codeproject.com.
//
///////////////////////////////////////////////////////////////////////////////


#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
{
protected:
	BitmapMenu();
	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);

private:
	// 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.

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...

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