Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Put bitmaps in menus


The following code demonstrates how To add a bitmap To a form menu item.

Option Explicit

'NOTE: Do not declare the picure variables private to routines.
'They must be declared privately in the form:
Private oIconUp As IPictureDisp, oIconDown As IPictureDisp

Private Declare Function GetMenu Lib "user32" ( ByVal hwnd As Long ) As Long
Private Declare Function GetSubMenu Lib "user32" ( ByVal hMenu As Long , ByVal nPos As Long ) As Long
Private Declare Function GetMenuItemID Lib "user32" ( ByVal hMenu As Long , ByVal nPos As Long ) As Long
Private Declare Function SetMenuItemBitmaps Lib "user32" ( ByVal hMenu As Long , ByVal nPosition As Long , ByVal wFlags As Long , ByVal hBitmapUnchecked As Long , ByVal hBitmapChecked As Long ) As Long
Private Declare Function DrawMenuBar Lib "user32" ( ByVal hwnd As Long ) As Long


'Purpose     :  Adds a bitmap to a form menu item (Max Bitmap size 13*13)
'Inputs      :  frmMenu                         The form containing the menu item.
'               lSubItem                        The zero based index of the sub menu.
'               lMenuItem                       The zero based index of the menu item to add the bitmap to.
'Outputs     :  Returns True if succeeded in adding the bitmap
'Author      :  Andrew Baker
'Date        :  03/09/2000 13:58
'Notes       :  If this function is called using variables to store
'               the bitmaps in (rather than picture controls), then
'               the variables must be declared privately to the form
'               (in the forms declaration section).
'Revisions   :

Function FormMenuSetIcon(frmMenu As Form, lSubItem As Long , lMenuItem As Long , oPicChecked As IPictureDisp, oPicUnchecked As IPictureDisp) As Boolean
    Const MF_BYCOMMAND = &H0&

    Dim lhwndMenu As Long , lhwndSubMenu As Long , lhwndMenuItem As Long

    'Get the menu handle
   lhwndMenu = GetMenu(frmMenu.hwnd)
    'Get the handle of the submenu
   lhwndSubMenu = GetSubMenu(lhwndMenu, lSubItem)
    'Get the handle of the menu item
   lhwndMenuItem = GetMenuItemID(lhwndSubMenu, lMenuItem)
   
    If lhwndMenuItem Then
        'The form has a sub menu, add the picture/icon
       FormMenuSetIcon = Cbool (SetMenuItemBitmaps(lhwndMenu, lhwndMenuItem, MF_BYCOMMAND, oPicChecked.Handle, oPicUnchecked.Handle))
        If FormMenuSetIcon Then
            'Added successfully, repaint the menu
            Call DrawMenuBar(lhwndMenu)
        End If
    End If
End Function

'Demonstration routine.
'Place the following routine in a form with a menu containing at
'least one sub menus and two sub items.

Sub Test()
    'Load two bitmaps for checked and unchecked menu images.
    Set oIconUp = LoadPicture( "C:\Calendar.bmp" )
    Set oIconDown = LoadPicture( "C:\Camcord.bmp" )
    'Add the bitmap to the first item on the first sub menu
   Debug. Print FormMenuSetIcon( Me , 0&, 0&, oIconUp, oIconDown)
    'Add the bitmap to the second item on the first sub menu
   Debug. Print FormMenuSetIcon( Me , 0&, 1&, oIconUp, oIconDown)
End Sub