Click here to Skip to main content
15,894,896 members
Articles / Desktop Programming / MFC

ClassLib, A C++ class library

Rate me:
Please Sign up or sign in to vote.
4.80/5 (32 votes)
25 May 2005CPOL8 min read 402.3K   11.5K   141  
C++ class library.
#ifndef _BITMAPMENU_H_
#define _BITMAPMENU_H_
//
// bitmapmenu.h
//
// (C) Copyright 2000 Jan van den Baard.
//     All Rights Reserved.
//

#include "menu.h"
#include "../gdi/brush.h"
#include "../collectors/simplearray.h"
#include "../strings/string.h"
#include "../coords/rect.h"

// Just in case...
#ifndef ODS_NOACCEL
#define ODS_NOACCEL         0x0100
#endif

// An array of these can be used to pass 
// to the SetItemBitmap() function.
struct StrItemBitmap
{
	int		m_iBitmap;	// Bitmap index in the image list.
	WORD		m_wID;		// Item ID or index.
};

#define SBM_END	( WORD )-1	// Set in "m_wID" to terminate the array.

// Structure which is used to store the
// data required to render the items. A
// pointer to this structure is stored
// in the item data field.
class ClsBitmapMenu;
struct ItemData
{
	ClsBitmapMenu  *m_pOwner;
	BOOL		m_bSeparator;
	BOOL		m_bToplevel;
	int		m_iBitmap;
	ClsString	m_strItemName;
	UINT		m_wID;
};

// Forward declarations.
class ClsDC;

// A class providing ownerdraw routines for menus.
class ClsBitmapMenu : public ClsMenu
{
	// No copy constructor.
	_NO_COPY( ClsBitmapMenu );
public:
	// Constructor.
	ClsBitmapMenu();

	// Virtual destructor.
	virtual ~ClsBitmapMenu();

	// Interface.
	virtual BOOL Destroy();
	static void DeleteTempObjects();
	HIMAGELIST SetImageList( HIMAGELIST hImages );
	inline HIMAGELIST GetImageList() { return m_hImageList; }
	inline BOOL& IsPopupMenu() { return ( BOOL& )m_bIsPopup; }
	BOOL ConvertMenus();
	BOOL SetItemBitmap( UINT nItemID, int iBitmap, BOOL bByPos = FALSE );
	BOOL SetItemBitmap( StrItemBitmap *pItemBitmaps, BOOL bByPos = FALSE );
	BOOL GetItemText( UINT nItemID, ClsString& str, BOOL bByPos = FALSE );
	BOOL SetItemText( UINT nItemID, LPCTSTR pszName, BOOL bByPos = FALSE );
	virtual BOOL DeleteMenu( UINT uPosition, BOOL bByPos = FALSE );
	ClsBitmapMenu *GetSubMenu( int nPos );
	inline void IconFrame( BOOL bFrame = TRUE ) { m_bIconFrame = bFrame; }
	inline void DrawMenuShadow( BOOL bShadow = TRUE ) { m_bDrawShadow = bShadow; }

protected:
	// Overidables.
	virtual LRESULT OnReflectedMeasureItem( LPMEASUREITEMSTRUCT pmis, BOOL& bNotifyParent );
	virtual LRESULT OnReflectedDrawItem( LPDRAWITEMSTRUCT pdis, BOOL& bNotifyParent );
	virtual BOOL DoFrameRendering();
	virtual void OnChangeWindowPos( LPWINDOWPOS pWindowPos );
	virtual void OnMeasureFrame( LPRECT pRect );
	virtual void OnDrawFrame( HDC hDC, LPCRECT pRect, LPCRECT pRectSrc ) ;
	virtual void OnReflectedExitMenuLoop( ClsWindow *pWindow, BOOL bShortcut );
	virtual void OnReflectedUnInitMenuPopup( ClsWindow *pWindow, HMENU hPopup, LPARAM lParam );
	virtual void OnReflectedInitMenuPopup( ClsWindow *pWindow, HMENU hPopup, int nIndex, BOOL bWindowMenu );
	
	// Helpers.
	BOOL ConvertMenusHelper( HMENU hMenu = NULL );
	void SplitCaption( LPCTSTR pszCaption, ClsString& sLeft, ClsString& sRight );
	void RenderItemText( LPDRAWITEMSTRUCT pdis, ItemData *pData );
	void RenderRadioBullet( ClsDC *pDC, ClsRect& rcRect, COLORREF crColor );
	void RenderCheckmark( ClsDC *pDC, ClsRect& rcRect, COLORREF crFgColor, COLORREF crBgColor, DWORD dwItemState, LPMENUITEMINFO pmii );
	void DestroyItemData( HMENU hMenu = NULL );
	void RemoveFromArray( ItemData *pData );
	BOOL IsItemInUse( ItemData *pData, HMENU hMenu = NULL );
	void GarbageCollect();

	// Data.
	int				m_cxBitmap;
	int				m_cyBitmap;
	HIMAGELIST			m_hImageList;
	BOOL				m_bIsSubMenu;
	BOOL				m_bIconFrame;
	BOOL				m_bConverted;
	BOOL				m_bIsPopup;
	BOOL				m_bDrawShadow;
	ClsSimpleArray< ItemData *>	m_ItemArray;
	ItemData		        m_SeparatorData;
};

#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)


Written By
Software Developer (Senior)
Netherlands Netherlands
I have been programming for a hobby since 1985. I have started programming on the C= 64. After that I migrated to the C= Amiga which I traded in for a PC back in 1997 I believe. Back in 2000 I decided to lose a hobby and start developing software for a living.

Currently I am working mainly in developing software for building security and access control systems.

Comments and Discussions