Simple Bitmapped Menus

Sometimes applications use dynamically created toolbars. Why do I need to create toolbar resources for menus with bitmaps? (See Iuri Apollonio article)

I propose to use standard image lists for storing menu bitmaps. The advantage of my class is that bitmap and toolbar images can be differentiated by size, view, etc. This Class supports SDI and MDI interfaces as well as popup menus. If you see any bugs please report.

With the CBitmapMenu class all you need is to:

1. Add CBitmapMenu class instance to CMainFrame class:

CBitmapMenu m_menu;

2. Add a CImageList object. For example, static member for class CMainFrame:

CImageList m_imglist;

At this point you have in MainFrm.h:

#include "BitmapMenu.h"

class CMainFrame : public CMDIFrameWnd
{
    ...
    public :
       CBitmapMenu m_menu;
       static CImageList m_imglist;
    ...
};

And in MainFrm.cpp this:

#include "MainFrm.h"

CImageList CMainFrame :: m_imglist;

3. Add a bitmap resource to the project with N images. One image you must add for checkmark.

4. Add an array of CItemBitmap objects (to the MainFrm.cpp file for example) for the menu items you want to be displayed with bitmaps:

CItemBitmap g_ibs[ ] =
{  
  CItemBitmap(ID_FILE_NEW, &CMainFrame :: m_imglist, 0),
  CItemBitmap(ID_FILE_OPEN, &Camp;MainFrame :: m_imglist, 1),
  CItemBitmap(ID_HELP_CONTENTS, &CMainFrame :: m_imglist, 19),
  CItemBitmap(-1, NULL, 0, &CMainFrame :: m_imglist, 27)
};

CItemBitmap class has 2 contstructors:

CItemBitmap( UINT uID,
             CImageList *pilNormal,
             UINT uPosNormal);
CItemBitmap( UINT uID,
             CImageList *pilNormal,
             UINT uPosNormal,
CImageList *pilChecked, UINT uPosChecked);

where

uID - menu item ID,
pilNormal - CImageList object address with a bitmap for "normal" menu item state,
pilChecked - ... for "checked" menu item state,
uPosNormal - image index in image list pilNormal,
uPosChecked - image index in image list pilChecked.

Last CItemBitmap object in array used for storing checkmark image information. This image CBitmapMenu class use for drawing checked menu item(s).

5. In CMainFrame :: OnCreate( ) handler function do the following:

m_menu.Initialize(IDR_MAINFRAME, this);
m_menu.AddBitmaps( g_ibs,
                   sizeof(g_ibs) / sizeof(CItemBitmap));

6. Add WM_MEASUREITEM, WM_DRAWITEM and WM_INITMENUPOPUP message handlers to the CMainFrame class.

Add next items in CMainFrame class message map:

ON_WM_MEASUREITEM( )
ON_WM_DRAWITEM( )
ON_WM_INITMENUPOPUP( )

And add this functions to the CMainFrame class:

void CMainFrame :: OnMeasureItem(int nIDCtl, 
                                  LPMEASUREITEMSTRUCT lpMIS)
{  if(lpMIS->CtlType == ODT_MENU)
     m_menu.MeasureItem( lpMIS );
   else
     CMDIFrameWnd :: OnMeasureItem(nIDCtl, lpMIS);
}

void CMainFrame :: OnDrawItem(int nIDCtl,
                              LPDRAWITEMSTRUCT lpDIS)
{
  if(lpDIS->CtlType == ODT_MENU)
     m_menu.DrawItem( lpDIS );
  else
     CMDIFrameWnd :: OnDrawItem(nIDCtl, lpDIS);
}

void CMainFrame :: OnInitMenuPopup(CMenu *pPopup,
                                   UINT nIndex,
                                   BOOL bSysMenu)
{
  CMDIFrameWnd :: OnInitMenuPopup( pPopup,
                                   nIndex,
                                   bSysMenu);

  if( !bSysMenu )
     CBitmapMenu :: Synchronize( pPopup );
}

6. Optional. If you want to use a bitmapped popup menu in your view class, do so in thismanner:

CMenu menu; VERIFY( menu.LoadMenu( SOME_POPUP_MENU_RESOURCE_ID ) ); CMenu *pMenu = (CMenu *) menu.GetSubMenu( 0 ); ASSERT(pMenu != NULL); pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, x, y, AfxGetMainWnd( ));

We use AfxGetMainWnd( ) in TrackPopupMenu( ) call to let main frame update the menu item's state. If you want to use 'this' (view) class as a parent for the popup menu then you have two problems:

  1. You need to manually check/gray/enable/disable menu items.
  2. You need to add the next three message handlers for proper drawing of bitmapped menus.

I don't give you a solution for the first problem. For the second problem a solution is to add handlers for the same messages as in CMainFrame class:

ON_WM_MEASUREITEM( )
ON_WM_DRAWITEM( )
ON_WM_INITMENUPOPUP( )

Handlers bodies:

void CMyEditView :: OnMeasureItem(int nIDCtl,
                                  LPMEASUREITEMSTRUCT lpMIS)
{
   if(lpMIS->CtlType == ODT_MENU)
      ((CMainFrame *) AfxGetMainWnd( ))->
                  m_menu.MeasureItem( lpMIS );
   else
      CEditView :: OnMeasureItem(nIDCtl, lpMIS);
}

void CMyEditView :: OnDrawItem(int nIDCtl,
                               LPDRAWITEMSTRUCT lpDIS)
{
   if(lpDIS->CtlType == ODT_MENU)
     ((CMainFrame *) AfxGetMainWnd( ))->
                         m_menu.DrawItem( lpDIS );
   else
     CEditView :: OnDrawItem(nIDCtl, lpDIS);
}

void CMyEditView :: OnInitMenuPopup(CMenu *pPopup,
                                    UINT nIndex,
                                    BOOL bSysMenu) 
{
   CEditView :: OnInitMenuPopup(pPopup,
                                nIndex,
                                bSysMenu);

   if( !bSysMenu )
      CBitmapMenu :: Synchronize( pPopup );
}

That is the end. You should now have bitmapped menus!

Downloads

Download demo project - 27 Kb


Comments

  • Download link is broken

    Posted by frankyboy1 on 01/07/2009 05:53am

    The demo project containing the CBitmapMenu class cannot be loaded. (Article not found.)

    Reply
  • Not showing bitmaps in Windows NT.

    Posted by Legacy on 10/22/2002 12:00am

    Originally posted by: SRaju

    Hi,
    
    

    I downloaded the project and executed the exe on windows NT, but i am not able to see bitmaps along with menuitems.
    Able to see in Windows 2000.

    if anyone has solution, please let me know.

    Thanks
    SRaju

    Reply
  • Find a Bug!

    Posted by Legacy on 07/14/2002 12:00am

    Originally posted by: Heo, Sang Gu

    1. I found a bug if a sub-popup menu have above 7 items.

    2 Please answer me rapidly.

    Reply
  • Bug! With dynamenu...

    Posted by Legacy on 06/21/2002 12:00am

    Originally posted by: Viper Craft

    When you trying modify your menu... oopss...

    Reply
  • cannot be properly executed in Win95

    Posted by Legacy on 05/02/2002 12:00am

    Originally posted by: ??

    I run this demo in Win95, and I got a generic menu with no bitmap on it.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • A majority of organizations are operating under the assumption that their network has already been compromised, or will be, according to a survey conducted by the SANS Institute. With many high profile breaches in 2013 occurring on endpoints, interest in improving endpoint security is top-of-mind for many information security professionals. The full results of the inaugural SANS Endpoint Security Survey are summarized in this white paper to help information security professionals track trends in endpoint …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds