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

  • The rapid evolution of enterprise storage technologies, combined with external forces, like the explosion of big data, can cause Linux® and server administrators to play catch-up when it comes to storage. Running a bunch of monolithic storage devices and proprietary, disconnected technologies forces administrators to spend valuable time creating and managing complex solutions. To reduce complexity and enable rapid deployment of new technologies and applications, server administrators need a single open …

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds