What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleAdd pictures to a popup menu
Keywordspopup, menu, picture, bitmap, image
Use the GetMenu, GetSubMenu, and SetMenuItemInfo API functions.

This program's SetMenuBitmap subroutine takes an array of numbers giving the position of the menu item to assign the bitmap. For example, the values

    Array(0, 2, 1)

assigns a bitmap to menu 0, submenu 2, item 1.

Normally you build a popup menu in the form's menus and then set its Visible property to False so it doesn't appear in the form's menus at runtime. Unfortunately when you set a menu's Visible property to False, any pictures it contains are lost.

To make the pictures visible, the program makes the menu visible and re-assigns the pictures. When the menu closes, the program hides the popup menu again.

' Put a bitmap in a menu item.
Public Sub SetMenuBitmap(ByVal frm As Form, ByVal _
    item_numbers As Variant, ByVal pic As Picture)
Dim menu_handle As Long
Dim i As Integer
Dim menu_info As MENUITEMINFO

    ' Get the menu handle.
    menu_handle = GetMenu(frm.hwnd)
    For i = LBound(item_numbers) To UBound(item_numbers) - 1
        menu_handle = GetSubMenu(menu_handle, _
    Next i

    ' Initialize the menu information.
    With menu_info
        .cbSize = Len(menu_info)
        .fMask = MIIM_TYPE
        .fType = MFT_BITMAP
        .dwTypeData = pic
    End With

    ' Assign the picture.
    SetMenuItemInfo menu_handle, _
        item_numbers(UBound(item_numbers)), _
        True, menu_info
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As _
    Integer, X As Single, Y As Single)
    If Button = vbRightButton Then
        ' Make the menu pictures.
        mnuPopup.Visible = True
        SetMenuBitmap Me, Array(0, 1, 0), imgStop.Picture
        SetMenuBitmap Me, Array(0, 1, 1), imgYield.Picture
        SetMenuBitmap Me, Array(0, 1, 2), imgCaution.Picture

        ' Display the popup menu.
        PopupMenu mnuPopup

        ' Hide the popup menu again.
        mnuPopup.Visible = False
    End If
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.