Home Search Contact us About us
Title Simple Menu Bitmaps / Icons
Summary Many modern applications show bitmaps / icons next to items in the drop down menu such File->Open folders. This article shows how to perform the simplist form of this.
Contributor John McTainsh
Published 20-May-2001
Last updated 20-May-2001
Page rating   62% for 9 votes Useless Brilliant

Introduction

Adding icons / bitmaps to the side of the application menu can make add some class to an ordinary display. This note explains how to do the most simple form of menu icon display. This form enables display of icons next to the menu items however it does not display the more modern method of buttons next to the menu items. It is also limited by colour in that it is not able to display white.

Both Code Guru and Code Project contain examples of using owner draw methods to perform the more advanced types with buttons and able to display white.

Definitions

In the CMainFrame class definition in Mainframe.h, add a CBitmap object of each menu icon you wish to display.

//Attrubutes
private:
    CBitmap     m_bmpMenuFileNew;                   //File new bitmap (Checked and unchecked)
    CBitmap     m_bmpMenuFileOpen;                  //File open bitmap (Checked and unchecked)

Implementation

Attach the menu and icon once the menu has been created using the following code. First load the CBitmap object with a Bitmap resource. Next attach it to the menu.

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    //Load the bitmap resource
    ASSERT( m_bmpMenuFileNew.LoadBitmap( IDB_MENU_NEW ) );
    ASSERT( m_bmpMenuFileOpen.LoadBitmap( IDB_MENU_OPEN ) );

    //Access the application menu
    CMenu* pMenu = GetMenu();
    ASSERT( pMenu );

    //Assign the icon
    ASSERT( pMenu->SetMenuItemBitmaps( ID_FILE_NEW, MF_BYCOMMAND, 
                &m_bmpMenuFileNew, &m_bmpMenuFileNew ) );
    ASSERT( pMenu->SetMenuItemBitmaps( ID_FILE_OPEN, MF_BYCOMMAND, 
                &m_bmpMenuFileOpen, &m_bmpMenuFileOpen ) );

    return 0;
}

Don't forget to free the bitmaps when done with them.

CMainFrame::~CMainFrame()
{
    if( m_bmpMenuFileNew.m_hObject )
        m_bmpMenuFileNew.DeleteObject();
    if( m_bmpMenuFileOpen.m_hObject )
        m_bmpMenuFileOpen.DeleteObject();
}
Comments Date
Home Search Contact us About us