Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Cool Owner Drawn Menus with Bitmaps - Version 3.03

, 28 Apr 2002 CPOL
This class implements an owner drawn menu class that mimics the menu style used in XP, Office and Visual C++
bcdialogmenu301.zip
BCDialogMenu301
BCDialogMenu.clw
BCDialogMenu.dsp
BCDialogMenu.dsw
BCDialogMenu.plg
BCDialogMenu.positions
BCMenu.startup
Release
BCDialogMenu.exe
res
BCDialogMenu.ico
Toolbar.bmp
toolbar1.bmp
bcdialogmenu303.zip
BCDialogMenu303
BCDialogMenu.clw
BCDialogMenu.dsp
BCDialogMenu.dsw
BCDialogMenu.opt
BCDialogMenu.plg
BCDialogMenu.positions
BCMenu.startup
Release
BCDialogMenu.exe
res
BCDialogMenu.ico
Toolbar.bmp
toolbar1.bmp
bcmenu25.zip
mymenu.aps
MYMENU.CLW
mymenu.dsp
MYMENU.DSW
MYMENU.opt
mymenu.plg
Release
mymenu.exe
RES
BMP00003.BMP
BMP00006.BMP
EDIT_COP.BMP
FILENEW.BMP
MYMENU.ICO
mymenuDoc.ico
NET.BMP
Toolbar.bmp
toolbar1.bmp
ZOOM.BMP
bcmenu261.zip
MYMENU.CLW
mymenu.dsp
MYMENU.DSW
Mymenu.opt
BMP00003.BMP
BMP00006.BMP
EDIT_COP.BMP
FILENEW.BMP
MYMENU.ICO
mymenuDoc.ico
NET.BMP
Toolbar.bmp
toolbar1.bmp
ZOOM.BMP
bcmenu300.zip
BCMenu
mymenu.aps
MYMENU.CLW
mymenu.dsp
MYMENU.DSW
MYMENU.OPT
mymenu.plg
MYMENU.positions
Mymenu2
Release
mymenu.exe
RES
Backup Archive.ico
BMP00003.BMP
BMP00006.BMP
EDIT_COP.BMP
FILENEW.BMP
MYMENU.ICO
mymenuDoc.ico
NET.BMP
test16.bmp
Toolbar.bmp
toolbar1.bmp
zoom.bmp
bcmenu301.zip
BCMenu301
MYMENU.CLW
mymenu.dsp
MYMENU.DSW
mymenu.plg
MYMENU.positions
Mymenu2
Release
mymenu.exe
RES
Backup Archive.ico
BMP00003.BMP
BMP00006.BMP
Copy of Toolbar.bmp
EDIT_COP.BMP
FILENEW.BMP
MYMENU.ICO
mymenuDoc.ico
NET.BMP
test16.bmp
Toolbar.bmp
toolbar1.bmp
zoom.bmp
bcmenu303.zip
BCMenu303
BCMenu.startup
mymenu.aps
MYMENU.CLW
mymenu.dsp
MYMENU.DSW
MYMENU.opt
mymenu.plg
MYMENU.positions
Mymenu2
Release
mymenu.exe
RES
Backup Archive.ico
BMP00003.BMP
BMP00006.BMP
Copy of Toolbar.bmp
EDIT_COP.BMP
FILENEW.BMP
MYMENU.ICO
mymenuDoc.ico
net.bmp
New.bmp
test16.bmp
Title.bmp
Toolbar.bmp
toolbar1.bmp
zoom.bmp
//*************************************************************************
// BCMenu.h : header file
// Version : 2.5
// Date : October 18, 1999
// Author : Brent Corkum
// 
// Portions of code supplied by:
// Ben Ashley,Girish Bharadwaj,Jean-Edouard Lachand-Robert,
// Robert Edward Caldecott,Kenny Goers,Leonardo Zide,
// Stefan Kuhr, Reiner Jung
//
// Bug Fixes:
// Stefan Kuhr,Martin Vladic,Kim Yoo Chul
//
// You are free to use/modify this code but leave this header intact.
//

#ifndef BCMenu_H
#define BCMenu_H

#include <afxtempl.h>

// BCMenuData class. Fill this class structure to define a single menu item:

class BCMenuData
{
wchar_t *m_szMenuText;
public:
BCMenuData () {menuIconNormal=-1;xoffset=-1;bitmap=NULL;
               nFlags=0;nID=0;syncflag=0;m_szMenuText=NULL;};
void SetAnsiString(LPCSTR szAnsiString);
void SetWideString(const wchar_t *szWideString);
const wchar_t *GetWideString(void) {return m_szMenuText;};
~BCMenuData ();
CString GetString(void);//returns the menu text in ANSI or UNICODE
int xoffset;
int menuIconNormal;
UINT nFlags,nID,syncflag;
CImageList *bitmap;
};

//struct CMenuItemInfo : public MENUITEMINFO {
struct CMenuItemInfo : public 
//MENUITEMINFO 
#ifndef UNICODE   //SK: this fixes warning C4097: typedef-name 'MENUITEMINFO' used as synonym for class-name 'tagMENUITEMINFOA'
tagMENUITEMINFOA
#else
tagMENUITEMINFOW
#endif
    {
	CMenuItemInfo()
	{ memset(this, 0, sizeof(MENUITEMINFO));
	  cbSize = sizeof(MENUITEMINFO);
	}
};




typedef enum {Normal,TextOnly} HIGHLIGHTSTYLE;

#ifndef UNICODE
#define AppendMenu AppendMenuA
#define InsertMenu InsertMenuA
#define InsertODMenu InsertODMenuA
#define AppendODMenu AppendODMenuA
#define AppendODPopupMenu AppendODPopupMenuA
#define ModifyODMenu ModifyODMenuA
#else
#define AppendMenu AppendMenuW
#define InsertMenu InsertMenuW
#define InsertODMenu InsertODMenuW
#define AppendODMenu AppendODMenuW
#define ModifyODMenu ModifyODMenuW
#define AppendODPopupMenu AppendODPopupMenuW
#endif


class BCMenu : public CMenu  // Derived from CMenu
{
// Construction
public:
BCMenu(); 

// Attributes
protected:
CTypedPtrArray<CPtrArray, BCMenuData*> m_MenuList;  // Stores list of menu items 

// When loading an owner-drawn menu using a Resource, BCMenu must keep track of
// the popup menu's that it creates. Warning, this list *MUST* be destroyed
// last item first :)
CTypedPtrArray<CPtrArray, HMENU>  m_SubMenus;  // Stores list of sub-menus 

// Stores a list of all BCMenu's ever created 
static CTypedPtrArray<CPtrArray, HMENU>  m_AllSubMenus;

// Operations
public: 
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCustomMenu)
//}}AFX_VIRTUAL 
// Implementation
public:
  static BOOL IsNewShell(void);
  void SetBitmapBackground(COLORREF color);
  void SetDisableOldStyle(void);
  void UnSetDisableOldStyle(void);
  BOOL GetDisableOldStyle(void);
  void UnSetBitmapBackground(void);
  BOOL AddBitmapToImageList(CImageList *list,UINT nResourceID);
  BOOL LoadFromToolBar(UINT nID,UINT nToolBar,int& xoffset);
  void InsertSpaces(void);
  static LRESULT FindKeyboardShortcut(UINT nChar,UINT nFlags,CMenu *pMenu);
  static void UpdateMenu(CMenu *pmenu);
  static BOOL IsMenu(CMenu *submenu);
  static BOOL IsMenu(HMENU submenu);
  void DrawCheckMark(CDC *pDC,int x,int y,COLORREF color);
  void DrawRadioDot(CDC *pDC,int x,int y,COLORREF color);
  BCMenu *FindMenuOption(int nId,int& nLoc);
  BCMenuData *FindMenuOption(wchar_t *lpstrText);
  BOOL GetMenuText(UINT id,CString &string,UINT nFlags = MF_BYPOSITION );
  CImageList *checkmaps;
  BOOL checkmapsshare;
  int m_selectcheck;
  int m_unselectcheck;
  void LoadCheckmarkBitmap(int unselect,int select);
  void DitherBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, 
                 int nHeight, HBITMAP hbm, int nXSrc, int nYSrc);
  void DitherBlt2(CDC *drawdc, int nXDest, int nYDest, int nWidth, 
                  int nHeight, CBitmap &bmp, int nXSrc, int nYSrc);
  static HBITMAP LoadSysColorBitmap(int nResourceId);
  BOOL GetBitmapFromImageList(CDC* pDC,CImageList *imglist,int nIndex,CBitmap &bmp);

virtual ~BCMenu();  // Virtual Destructor 
// Drawing: 
virtual void DrawItem( LPDRAWITEMSTRUCT);  // Draw an item
virtual void MeasureItem( LPMEASUREITEMSTRUCT );  // Measure an item

// Customizing:

void SetIconSize (int, int);  // Set icon size

BOOL AppendODMenuA(LPCSTR lpstrText, 
UINT nFlags = MF_OWNERDRAW,
UINT nID = 0,
int nIconNormal = -1);  

BOOL AppendODMenuW(wchar_t *lpstrText, 
UINT nFlags = MF_OWNERDRAW,
UINT nID = 0,
int nIconNormal = -1);  

BOOL AppendODMenuA(LPCSTR lpstrText, 
UINT nFlags,
UINT nID,
CImageList *il,
int xoffset);

BOOL AppendODMenuW(wchar_t *lpstrText, 
UINT nFlags,
UINT nID,
CImageList *il,
int xoffset);

BOOL InsertODMenuA(UINT nPosition,
LPCSTR lpstrText, 
UINT nFlags = MF_OWNERDRAW,
UINT nID = 0,
int nIconNormal = -1); 

BOOL InsertODMenuW(UINT nPosition,
wchar_t *lpstrText, 
UINT nFlags = MF_OWNERDRAW,
UINT nID = 0,
int nIconNormal = -1);  

BOOL InsertODMenuA(UINT nPosition,
LPCSTR lpstrText, 
UINT nFlags,
UINT nID,
CImageList *il,
int xoffset);

BOOL InsertODMenuW(UINT nPosition,
wchar_t *lpstrText, 
UINT nFlags,
UINT nID,
CImageList *il,
int xoffset);


BOOL ModifyODMenuA(const char *lpstrText,UINT nID=0,int nIconNormal=-1);
BOOL ModifyODMenuA(const char *lpstrText,UINT nID,CImageList *il,int xoffset);
BOOL ModifyODMenuA(const char *lpstrText,UINT nID,CBitmap *bmp);
BOOL ModifyODMenuA(const char *lpstrText,const char *OptionText,int nIconNormal);
BOOL ModifyODMenuW(wchar_t *lpstrText,UINT nID=0,int nIconNormal=-1);
BOOL ModifyODMenuW(wchar_t *lpstrText,UINT nID,CImageList *il,int xoffset);
BOOL ModifyODMenuW(wchar_t *lpstrText,UINT nID,CBitmap *bmp);
BOOL ModifyODMenuW(wchar_t *lpstrText,wchar_t *OptionText,int nIconNormal);
BCMenuData *NewODMenu(UINT pos,UINT nFlags,UINT nID,CString string);
void SynchronizeMenu(void);
void BCMenu::InitializeMenuList(int value);
void BCMenu::DeleteMenuList(void);
BCMenuData *BCMenu::FindMenuList(UINT nID);
virtual BOOL LoadMenu(LPCTSTR lpszResourceName);  // Load a menu
virtual BOOL LoadMenu(int nResource);  // ... 
void AddFromToolBar(CToolBar* pToolBar, int nResourceID);
BOOL Draw3DCheckmark(CDC *dc, const CRect& rc,BOOL bSelected,
                     HBITMAP hbmCheck);
BOOL LoadToolbar(UINT nToolBar);
BOOL LoadToolbars(const UINT *arID,int n);

// new overrides for dynamic menu's
BOOL	RemoveMenu(UINT uiId,UINT nFlags);
BOOL	DeleteMenu(UINT uiId,UINT nFlags);
BOOL  AppendMenuA(UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,int nIconNormal=-1);
BOOL  AppendMenuA(UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,CImageList *il,int xoffset);
BOOL  AppendMenuA(UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,CBitmap *bmp);
BOOL  AppendMenuW(UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,int nIconNormal=-1);
BOOL  AppendMenuW(UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,CImageList *il,int xoffset);
BOOL  AppendMenuW(UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,CBitmap *bmp);
BOOL  InsertMenuA(UINT nPosition,UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,int nIconNormal=-1);
BOOL  InsertMenuA(UINT nPosition,UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,CImageList *il,int xoffset);
BOOL  InsertMenuA(UINT nPosition,UINT nFlags,UINT nIDNewItem,const char *lpszNewItem,CBitmap *bmp);
BOOL  InsertMenuW(UINT nPosition,UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,int nIconNormal=-1);
BOOL  InsertMenuW(UINT nPosition,UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,CImageList *il,int xoffset);
BOOL  InsertMenuW(UINT nPosition,UINT nFlags,UINT nIDNewItem,wchar_t *lpszNewItem,CBitmap *bmp);
BCMenu* AppendODPopupMenuA(LPCSTR lpstrText);
BCMenu* AppendODPopupMenuW(wchar_t *lpstrText);

// Destoying:

virtual BOOL DestroyMenu();

// Generated message map functions
protected:
int m_iconX,m_iconY;
COLORREF m_bitmapBackground;
BOOL m_bitmapBackgroundFlag;
BOOL disable_old_style;
}; 

#endif

//*************************************************************************

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

Brent Corkum
Web Developer
Canada Canada
I'm the senior software development manager for Rocscience Inc., a company specializing in geomechanics software. I have a PH.D in Civil Engineering, and have been programming since 1978. I've used more computers and languages then 2000 characters lets me list. Besides programming, I enjoy golfing, watching my 3 kids play sports, spending time with my wife, and watching plenty of hockey and football.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411022.1 | Last Updated 28 Apr 2002
Article Copyright 1999 by Brent Corkum
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid