dcsimg
 

Simple Bitmapped Menus

Thursday Jul 25th 2002 by Burchak Alexander

A bitmapped menu class wit the advantage that the bitmap and toolbar images can be differentiated by size, view, etc.

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, &CampMainFrame :: 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
Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved