I am sure we all have developed applications with menus. Right? And obviously, we all use the applications with menus. Take Office for example, or our .NET IDE. What is the difference between the menus of these applications and our menus? These applications have bitmaps in their menus. .NET does not have the facility of putting images in menus by design. How can we achieve this?
The first step is to declare the API we are going to use:
Private Declare Sub SetMenuItemBitmaps Lib "user32" (ByVal hMenu As IntPtr, ByVal nPosition As Int32, ByVal wFlags As Int32, ByVal hBitmapUnchecked As IntPtr, ByVal hBitmapChecked As IntPtr)
The name explains the use of this function: SetMenuItemBitmaps
It takes following argumets:
hMenu: Integer Pointer or Handle to the parent menu in which we want to put the image.
nPosition: Position of the menu item in parent menu.
wFlags: How the position must be taken. Value &H0 – identifier of the menu (we need another API to find this). We will use &H400 because this value tells the function to take the zero based relative position of the menu.
hBitmapUnchecked: Handle to the bitmap when menu is unchecked (menu.Checked=False).
hBitmapChecked: Handle to the bitmap when menu is checked.
To get hMenu, we use: mnuMain.MenuItems().Handle, where mnuMain is the main menu on our form.
The position of all the items within this menu can be given by integers (0,1,2,…)
To get the handle to bitmap we can use the GetHbitmap method of bitmap. This method will not appear in the member list of bitmap object when you put a ‘.’ after the object (I’m talking about intellisense here).
So, here is the code that does it all:
SetMenuItemBitmaps(mnuMain.MenuItems(0).Handle, 0, &H400&, New Bitmap("filenameunchecked.bmp").GetHbitmap(), New Bitmap("filenamechecked.bmp").GetHbitmap())
In the above code, we have taken two files – filenameunchecked.bmp and filenamechecked.bmp, one of these bitmaps will appear depending on the value of checked property of the menu in question.
Do you know what this API does? What happens when the checked property is set to True? A check mark appears before the menu and when the property is set to False, the checkmark goes. This API simply replaces the checkmark bitmap with the image we specify.
I recommend that you experiment with the code to get familiar with it. This is pretty simple and very fast (thanks to API).


[...] ByVal lpFile As String, … (ByVal hMenu As Long, ByVal un As Long, _ ByVal n1 As Long, ByVal n2 …Add bitmap to menu in vb.net coder000Add bitmap to menu in vb.net using api SetMenuItemBitmaps. … (ByVal hMenu As IntPtr, ByVal [...]