Discussion Locked

Pictures in Menu bar

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi All,
How would u add a Picture/Icons to your menus in a Visual basic Application

Vijay.

6 replies

by juanja

Add a module, and put the following code inside:

Public Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public Declare FunctionGetMenuItemID 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
Const MF_BITMAP = &H4&

Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type

Public Const MIIM_ID = &H2
Public Const MIIM_TYPE = &H10
Public Const MFT_STRING = &H0&

Then, in the form you have the menu, put the following code in the event you want (you will probably prefer form_load )

'Get the menuhandle of your app
hMenu& = GetMenu(Form1.hwnd)

'Get the handle of the first submenu
hSubMenu& = GetSubMenu(hMenu&, 0)

'Get the menuId of the first entry
hID& = GetMenuItemID(hSubMenu&, 0)'Add the bitmap
SetMenuItemBitmaps hMenu&, hID&, MF_BITMAP, _
Picture1.Picture, _
Picture1.Picture

You can add two bitmaps to a menuentry, one for the checked and one for the unchecked
state.

Hope that helps!

by juanja

I posted this question twice, because in the first one I forgot to mention the PICTURE1 picture box.

(just in case you find it to be a problem...)

by juanja

Add a module, and put the following code inside:

Public Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public Declare FunctionGetMenuItemID 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
Const MF_BITMAP = &H4&

Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type

Public Const MIIM_ID = &H2
Public Const MIIM_TYPE = &H10
Public Const MFT_STRING = &H0&

Then, in the form you have the menu, add a picture box named picture1 (default) with Autosize = true, set the picture property to a bitmap, put the following code in the event you want (you will probably prefer form_load )

'Get the menuhandle of your app
hMenu& = GetMenu(Form1.hwnd)

'Get the handle of the first submenu
hSubMenu& = GetSubMenu(hMenu&, 0)

'Get the menuId of the first entry
hID& = GetMenuItemID(hSubMenu&, 0)

'Add the bitmap
SetMenuItemBitmaps hMenu&, hID&, MF_BITMAP, _
Picture1.Picture, _
Picture1.Picture

You can add two bitmaps to a menuentry, one for the checked and one for the unchecked
state.

Hope that helps!

This discussion is closed to new replies.