CodeItBetter Programming Another VB Programming Blog

How to add a bitmap to your menu

Posted on August 21, 2009

Let's say you have menu in your form and you would like to add a bitmap to your menu.

Instructions:

  • Create a new Project
  • Add a new form to it
  • Add two picture box controls Picture1 and Picture2
  • Set the picturebox autosize Property to True
  • Add a Menubar and add Menu "File"
  • Add a sub-menu "New" to File Menu

Now, goto form's code window and add the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Option Explicit
 
Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
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
Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal b As Boolean, lpMenuItemInfo As MENUITEMINFO) As Boolean
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
 
Private Sub Form_Load()
  'Get the menu handle
  hMenu& = GetMenu(Me.hwnd)
  'Get the handle of the first submenu
  hSubMenu& = GetSubMenu(hMenu&, 0)
  'Get the menu Id of the first entry
  hID& = GetMenuItemID(hSubMenu&, 0)
  'Add the bitmap (one for checked and another one for unchecked state of the menu)
  Call SetMenuItemBitmaps(hMenu&, hID&, MF_BITMAP, Picture1.Picture, Picture2.Picture)
End Sub

You can add two pictures to your menu one for Checked state and another one for Unchecked state of that menu.

Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

Trackbacks are disabled.