SHARE
Facebook X Pinterest WhatsApp

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 […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Jul 25, 2002
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

Recommended for you...

Video Game Careers Overview
CodeGuru Staff
Sep 18, 2022
Dealing with non-CLS Exceptions in .NET
Hannes DuPreez
Aug 5, 2022
Online Courses to Learn Video Game Development
Ronnie Payne
Jul 8, 2022
Best Online Courses to Learn C++
CodeGuru Staff
Jun 25, 2022
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.