by James Bihary (1 Submission)
Category: Internet/HTML
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Wed 17th February 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Add Images to Menu Items (Along with the Text)
API Declarations
Public Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public 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
Public Const MF_BITMAP = &H4
imlImageList As ImageList, lImagePos As Long)
' Add the selected bitmap to the given menu item.
'
' hMenu - the menu handle for the menu you want to modify.
' lSubMenuPos - the position of the submenu (0 based).
' lMenuItemPos - the position of the menu item (0 based).
' imlImageList - the imagelist from which you will get the images.
' lImagePos - the position in the image list where the image is stored.
Dim hSubMenu As Long
Dim lMenuID As Long
Dim lRet As Long
On Error GoTo AddImageToMenuItem_Error
' Get the submenu handle.
hSubMenu = GetSubMenu(hMenu, lSubMenuPos)
' Get the menu item id.
lMenuID = GetMenuItemID(hSubMenu, lMenuItemPos)
' Make the call that puts the Bitmap in.
lRet = SetMenuItemBitmaps(hMenu, lMenuID, MF_BITMAP, imlImageList.ListImages(lImagePos).Picture, imlImageList.ListImages(lImagePos).Picture)
Exit Sub
AddImageToMenuItem_Error:
' Appropriate error handling.
End Sub
Private Sub AddMenuBitmapsPrototype()
Dim hMenu As Long
hMenu = GetMenu(Me.hwnd)
Call AddImageToMenuItem(hMenu, 0, 0, imlMainToolBarImageList, 1)
End Sub
Images should be 13x13 or smaller to fit properly into the menu.
This takes images from an ImageList control, but, of course, you can modify
this to use any control that has a Picture property.
Can be modified to take a Key instead of a position for the images in the ImageList.