Sei sulla pagina 1di 31

GetMenu

API Explanation
The GetMenu function retrieves the handle of the menu assigned to the given window.

Parameter Information
Declare Function GetMenu Lib "user32" Alias "GetMenu" (ByVal hwnd As Long) As Long

· hWnd
Identifies the window whose menu handle is retrieved.

If the function succeeds, the return value is the handle of the menu. If the given window has no
menu, the return value is NULL. If the window is a child window, the return value is undefined.

'Download the full source+pictures+.. at


http://www.allapi.net/php/dlman/dm.cgi?id=bitmenu.zip&action=download
Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByV
al nPos As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPos As Long) As Long
Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal h
Menu As Long, ByVal nPosition _ As Long, ByVal wFlags As Long, ByVal wIDNewIt
em AsLong, ByVal lpString As Any) As Long
Const MF_BITMAP = 4
Const MF_CHECKED = 8

Private Sub Form_Load()


Dim hMenu As Long, hSubMenu As Long, lngID As Long

'Get the handle of the form's menu


hMenu = GetMenu(Me.hWnd)
'Get the handle of the form's submenu
hSubMenu = GetSubMenu(hMenu, 0)

'Change first item (index=0)


picBitmaps(0).Picture = picBitmaps(0).Image
lngID = GetMenuItemID(hSubMenu, 0)
Call ModifyMenu(hMenu, lngID, MF_BITMAP, lngID, CLng(picBitmaps(0).Picture))

'Change second item (index=1)


picBitmaps(1).Picture = picBitmaps(1).Image
lngID = GetMenuItemID(hSubMenu, 1)
Call ModifyMenu(hMenu, lngID, MF_BITMAP, lngID, CLng(picBitmaps(1).Picture))

'Change third item (index=2)


picBitmaps(2).Picture = picBitmaps(2).Image
lngID = GetMenuItemID(hSubMenu, 2)
Call ModifyMenu(hMenu, lngID, MF_BITMAP, lngID, CLng(picBitmaps(2).Picture))
mnuBitmap1.Checked = True
End Sub

Private Sub mnuBitmap1_Click()


mnuBitmap1.Checked = Not mnuBitmap1.Checked
End Sub

Private Sub mnuBitmapEnd_Click()


End
End Sub

AppendMenu

API Explanation
The AppendMenu function appends a new item to the end of the specified menu bar, drop-down
menu, submenu, or shortcut menu. You can use this function to specify the content, appearance, and
behavior of the menu item.

Parameter Information
Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags
As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long

· hMenu
Identifies the menu bar, drop-down menu, submenu, or shortcut menu to be changed.

· uFlags
Specifies flags to control the appearance and behavior of the new menu item. This parameter can be a
combination of the values listed in the following Remarks section.

· uIDNewItem
Specifies either the identifier of the new menu item or, if the uFlags parameter is set to MF_POPUP,
the handle to the drop-down menu or submenu.

· lpNewItem
Specifies the content of the new menu item. The interpretation of lpNewItem depends on whether the
uFlags parameter includes the MF_BITMAP, MF_OWNERDRAW, or MF_STRING flag, as follows:
MF_BITMAP
Contains a bitmap handle.
MF_OWNERDRAW
Contains a 32-bit value supplied by the application that can be used to maintain additional data
related to the menu item. The value is in the itemData member of the structure pointed to by the
lparam parameter of the WM_MEASURE or WM_DRAWITEM message sent when the menu is created
or its appearance is updated.
MF_STRING
Contains a pointer to a null-terminated string.

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.
Const MF_CHECKED = &H8&
Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&

Private Type POINTAPI


x As Long
y As Long
End Type

Private Declare Function CreatePopupMenu Lib "user32" () As Long


Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, By
Val wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long
, ByValhwnd As Long, ByVal lprc As Any) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVa
l bRevert As Long) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal h
Menu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem
AsAny) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Lo
ng
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As L
ong

Dim hMenu As Long

Private Sub Form_Load()


'Create an empty popupmenu
hMenu = CreatePopupMenu()
'Append a few menu items
AppendMenu hMenu, MF_STRING, ByVal 0&, "Hello !"
AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, ByVal 0&, "Testing ..."
AppendMenu hMenu, MF_SEPARATOR, ByVal 0&, ByVal 0&
AppendMenu hMenu, MF_CHECKED, ByVal 0&, "TrackPopupMenu"
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y


As Single)
Dim Pt As POINTAPI
'Get the position of the mouse cursor
GetCursorPos Pt
If Button = 1 Then
'Show our popupmenu
TrackPopupMenu hMenu, TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
Else
'Show our form's default popup menu
TrackPopupMenu GetSystemMenu(Me.hwnd, False), TPM_LEFTALIGN, Pt.x, Pt.y, 0, M
e.hwnd, ByVal 0&
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)


'Destroy our menu
DestroyMenu hMenu
End Sub

CreatePopupMenu

API Explanation
The CreatePopupMenu function creates a drop-down menu, submenu, or shortcut menu.
The menu is initially empty.

Parameter Information
Declare Function CreatePopupMenu Lib "user32" Alias "CreatePopupMenu" () As Long

Const MF_CHECKED = &H8&


Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&

Private Type POINTAPI


x As Long
y As Long
End Type

Private Declare Function CreatePopupMenu Lib "user32" () As Long


Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, By
Val wFlags As Long, _
ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long
, ByVal lprc As Any) _
As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVa
l bRevert As Long) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal h
Menu As Long, ByVal _
wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Lo
ng
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As L
ong
Dim hMenu As Long
Private Sub Form_Load()
'create menu
hMenu = CreatePopupMenu()
'Append a few menu items
AppendMenu hMenu, MF_STRING, ByVal 0&, "Hello !"
AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, ByVal 0&, "Testing ..."
AppendMenu hMenu, MF_SEPARATOR, ByVal 0&, ByVal 0&
AppendMenu hMenu, MF_CHECKED, ByVal 0&, "TrackPopupMenu"
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y


As Single)
Dim Pt As POINTAPI
'Get the position of the mouse cursor
GetCursorPos Pt
If Button = 1 Then
'Show our popupmenu
TrackPopupMenu hMenu, TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
Else
'Show our form's default popup menu
TrackPopupMenu GetSystemMenu(Me.hwnd, False), TPM_LEFTALIGN, Pt.x, Pt.y, 0, M
e.hwnd, ByVal 0&
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)


'Destroy our menu
DestroyMenu hMenu
End Sub

GetMenuItemCount

API Explanation
Public Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long

CheckMenuRadioItem

API Explanation
The CheckMenuRadioItem function checks a specified menu item and makes it a radio item. At the
same time, the function clears all other menu items in the associated group and clears the radio-item
type flag for those items.

Parameter Information
Declare Function CheckMenuRadioItem Lib "user32" (ByVal hMenu As Long, ByVal un1 As Long, ByVal
un2 As Long, ByVal un3 As Long, ByVal un4 As Long) As Long

· hmenu
[in] Handle to the menu that contains the group of menu items.
· idFirst
[in] Identifier or position of the first menu item in the group.

· idLast
[in] Identifier or position of the last menu item in the group.

· idCheck
[in] Identifier or position of the menu item to check.

· uFlags
[in] Value specifying the meaning of idFirst, idLast, and idCheck. If this parameter is
MF_BYCOMMAND, the other parameters specify menu item identifiers. If it is MF_BYPOSITION, the
other parameters specify the menu item positions.

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, use the GetLastError
function.

'Example submitted by Abdulaziz Alfoudari (webmaster@vbparadise.com)


'Visit his homepage at http://www.vbparadise.com

' In a form
Option Explicit
'Create the following menu on this form:
' mnuFile
' mnuFileItem1
' mnuFileItem2
' mnuFileBar1
' mnuFileItem3
' mnuFileItem4
' mnuFileItem5
' mnuFileItem6

Private Sub mnuFileItem1_Click()


SetMenuRadio Me, 1, 1, 1, 2
End Sub

Private Sub mnuFileItem2_Click()


SetMenuRadio Me, 1, 2, 1, 2
End Sub

Private Sub mnuFileItem3_Click()


SetMenuRadio Me, 1, 4, 4, 7
End Sub

Private Sub mnuFileItem4_Click()


SetMenuRadio Me, 1, 5, 4, 7
End Sub
Private Sub mnuFileItem5_Click()
SetMenuRadio Me, 1, 6, 4, 7
End Sub

Private Sub mnuFileItem6_Click()


SetMenuRadio Me, 1, 7, 4, 7
End Sub

'In a module

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 CheckMenuRadioItem Lib "user32" (ByVal hMenu As Long, ByVal
un1 As Long, _
ByVal un2 As Long, ByVal un3 As Long, ByVal un4 As Long) As Long
Const MF_BYPOSITION = &H400&
Const MF_CHECKED = &H8&

'IMPORTANT: Please note that you must count the bars, don't forget the
'bars (separators). For example, if your group first menu
'item position is 3 (without counting the bars), you'd set
'[FromItem] value to 4 (note that we counted the separator)
'and [ToItem] value would be 7. 4 [first menu], 5 [second
'menu], 6 [third menu], 7 [fourth menu), and so on...

Function SetMenuRadio(frm As Form, TopMenuPosition As Integer, ItemPosition A


s Integer, FromItem As Integer, ToItem As Integer)
Dim lRet As Long
Dim lngMenu As Long
Dim lngSubMenu As Long
Dim lhWnd
lhWnd = frm.hwnd
lngMenu = GetMenu(lhWnd) '0 = error
lngSubMenu = GetSubMenu(lngMenu, TopMenuPosition - 1) '0 = error
lRet = CheckMenuRadioItem(lngSubMenu, FromItem - 1, ToItem -
1, ItemPosition - 1, MF_BYPOSITION)
End Function

TrackPopupMenuEx

API Explanation
The TrackPopupMenuEx function displays a shortcut menu at the specified location and tracks the
selection of items on the shortcut menu. The shortcut menu can appear anywhere on the screen.

Parameter Information
Declare Function TrackPopupMenuEx Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal
x As Long, ByVal y As Long, ByVal HWnd As Long, ByVal lptpm As Any) As Long

· hmenu
[in] Handle to the shortcut menu to be displayed. This handle can be obtained by calling the
CreatePopupMenu function to create a new shortcut menu or by calling the GetSubMenu function to
retrieve a handle to a submenu associated with an existing menu item.

? fuFlags
[in] Specifies function options.
Use one of the following flags to specify how the function positions the shortcut menu horizontally.
TPM_CENTERALIGN
If this flag is set, the function centers the shortcut menu horizontally relative to the coordinate
specified by the x parameter.
TPM_LEFTALIGN
If this flag is set, the function positions the shortcut menu so that its left side is aligned with the
coordinate specified by the x parameter.
TPM_RIGHTALIGN
Positions the shortcut menu so that its right side is aligned with the coordinate specified by the x
parameter.

Use one of the following flags to specify how the function positions the shortcut menu vertically.
TPM_BOTTOMALIGN
If this flag is set, the function positions the shortcut menu so that its bottom side is aligned with the
coordinate specified by the y parameter.
TPM_TOPALIGN
If this flag is set, the function positions the shortcut menu so that its top side is aligned with the
coordinate specified by the y parameter.
TPM_VCENTERALIGN
If this flag is set, the function centers the shortcut menu vertically relative to the coordinate specified
by the y parameter.

Use the following flags to determine the user selection without having to set up a parent window for
the menu.
TPM_NONOTIFY
If this flag is set, the function does not send notification messages when the user clicks on a menu
item.
TPM_RETURNCMD
If this flag is set, the function returns the menu item identifier of the user's selection in the return
value.

Use one of the following flags to specify which mouse button the shortcut menu tracks.
TPM_LEFTBUTTON
If this flag is set, the user can select menu items with only the left mouse button.
TPM_RIGHTBUTTON
If this flag is set, the user can select menu items with both the left and right mouse buttons.

Windows 98/Me, Windows 2000/XP: Use any reasonable combination of the following flags to modify
the animation of a menu. For example, by selecting a horizontal and a vertical flag you can achieve
diagonal animation.
TPM_HORNEGANIMATION
Animates the menu from left to right.
TPM_HORPOSANIMATION
Animates the menu from right to left.
TPM_NOANIMATION
Displays menu without animation.
TPM_VERNEGANIMATION
Animates the menu from bottom to top.
TPM_VERPOSANIMATION
Animates the menu from top to bottom.

For any animation to occur, the SystemParametersInfo function must set SPI_SETMENUANIMATION.
Also, all the TPM_*ANIMATION flags, except TPM_NOANIMATION, are ignored if menu fade animation
is on, See the SPI_GETMENUFADE flag in SystemParametersInfo.

Windows 98/Me, Windows 2000/XP: Use the TPM_RECURSE flag to display a menu when another
menu is already displayed. This is intended to support context menus within a menu.

Use one of the following flags to specify whether to accommodate horizontal or vertical alignment.
TPM_HORIZONTAL
If the menu cannot be shown at the specified location without overlapping the excluded rectangle, the
system tries to accommodate the requested horizontal alignment before the requested vertical
alignment.
TPM_VERTICAL
If the menu cannot be shown at the specified location without overlapping the excluded rectangle, the
system tries to accommodate the requested vertical alignment before the requested horizontal
alignment.

The excluded rectangle is a portion of the screen that the menu should not overlap; it is specified by
lptpm.

Windows XP: To have text layout from right-to-left, use TPM_LAYOUTRTL. By default, the text layout
is left-to-right.

·x
[in] Horizontal location of the shortcut menu, in screen coordinates.

·y
[in] Vertical location of the shortcut menu, in screen coordinates.

· hwnd
[in] Handle to the window that owns the shortcut menu. This window receives all messages from the
menu. The window does not receive a WM_COMMAND message from the menu until the function
returns.
If you specify TPM_NONOTIFY in the fuFlags parameter, the function does not send messages to the
window identified by hwnd. However, you still have to pass a window handle in hwnd. It can be any
window handle from your application.

· lptpm
[in] Pointer to a TPMPARAMS structure that specifies an area of the screen the menu should not
overlap. This parameter can be NULL.

If you specify TPM_RETURNCMD in the fuFlags parameter, the return value is the menu-item identifier
of the item that the user selected. If the user cancels the menu without making a selection, or if an
error occurs, then the return value is zero.

If you do not specify TPM_RETURNCMD in the fuFlags parameter, the return value is nonzero if the
function succeeds and zero if it fails. To get extended error information, call GetLastError.

Const MF_CHECKED = &H8&


Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&
Const TPM_RETURNCMD = &H100&
Const TPM_RIGHTBUTTON = &H2&

Private Type POINTAPI


x As Long
y As Long
End Type

Private Declare Function CreatePopupMenu Lib "user32" () As Long


Private Declare Function TrackPopupMenuEx Lib "user32" (ByVal hMenu As Long,
ByVal wFlags As Long, _ ByVal x As Long, ByVal y As Long, ByVal HWnd As Long,
ByVallptpm As Any) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal h
Menu As Long, _
ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Lo
ng
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Lo
ng
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As L
ong

Dim hMenu As Long

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y


As Single)
Dim Pt As POINTAPI
Dim ret As Long
hMenu = CreatePopupMenu()
AppendMenu hMenu, MF_STRING, 1, "Hello !"
AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, 2, "Testing ..."
AppendMenu hMenu, MF_SEPARATOR, 3, ByVal 0&
AppendMenu hMenu, MF_CHECKED, 4, "TrackPopupMenu"
GetCursorPos Pt
ret = TrackPopupMenuEx(hMenu, TPM_LEFTALIGN Or TPM_RETURNCMD Or _
TPM_RIGHTBUTTON, Pt.x, Pt.y, Me.HWnd, ByVal 0&)
DestroyMenu hMenu
Debug.Print ret
End Sub

SetMenuItemBitmaps

API Explanation
The SetMenuItemBitmaps function associates the specified bitmap with a menu item. Whether the
menu item is checked or unchecked, Windows displays the appropriate bitmap next to the menu item.

Parameter Information
Declare Function SetMenuItemBitmaps Lib "user32" Alias "SetMenuItemBitmaps" (ByVal hMenu As
Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal
hBitmapChecked As Long) As Long

· hMenu
Identifies the menu containing the item to receive new check-mark bitmaps.

· uPosition
Specifies the menu item to be changed, as determined by the uFlags parameter.

· uFlags
Specifies how the uPosition parameter is interpreted. The uFlags parameter must be one of the
following values.
MF_BYCOMMAND
Indicates that uPosition gives the identifier of the menu item. If neither MF_BYCOMMAND nor
MF_BYPOSITION is specified, MF_BYCOMMAND is the default flag.
MF_BYPOSITION
Indicates that uPosition gives the zero-based relative position of the menu item.

· hBitmapUnchecked
Identifies the bitmap displayed when the menu item is not checked.

· hBitmapChecked
Identifies the bitmap displayed when the menu item is checked.

'This project needs a form with a menu with at least one submenu
'It also needs a picturebox, Picture1, that contains a small b/w bitmap

Const MF_BYPOSITION = &H400&

Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPos As Long) As Long
Private Declare Function SetMenuItemBitmaps Lib "user32" (ByVal hMenu As Long
, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Lo
ng,ByVal hBitmapChecked As Long) As Long

Private Sub Form_Load()


Dim hMenu As Long, hSubMenu As Long
'get the handle of the menu
hMenu = GetMenu(Me.hwnd)
'check if there's a menu
If hMenu = 0 Then
MsgBox "This form doesn't have a menu!"
Exit Sub
End If
'get the first submenu
hSubMenu = GetSubMenu(hMenu, 0)
'check if there's a submenu
If hSubMenu = 0 Then
MsgBox "This form doesn't have a submenu!"
Exit Sub
End If
'set the menu bitmap
SetMenuItemBitmaps hSubMenu, 0, MF_BYPOSITION, Picture1.Picture, Picture1.Pic
ture
End Sub

SetMenuItemInfo
API Explanation
The SetMenuItemInfo function changes information about a menu item.

Parameter Information
Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As Long,
ByVal uItem As Long, ByVal fByPosition As Long, lpmii As MENUITEMINFO) As Long

· hMenu
Handle to the menu that contains the menu item.

· uItem
Identifier or position of the menu item to change. The meaning of this parameter depends on the
value of fByPosition.

· fByPosition
Value specifying the meaning of uItem. If this parameter is FALSE, uItem is a menu item identifier.
Otherwise, it is a menu item position.

· lpmii
Pointer to a MENUITEMINFO structure that contains information about the menu item and specifies
which menu item attributes to change.

Private Const MFT_RADIOCHECK = &H200&


Private Const MIIM_TYPE = &H10
Private Const MIIM_SUBMENU = &H4

Private 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 Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA
" (ByVal hMenu As Long, ByVal un As Long, ByVal b As Boolean, lpmii As MENUIT
EMINFO)As Long
Private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA
" (ByVal hMenu As Long, ByVal uItem As Long, ByVal fByPosition As Long, lpmii
AsMENUITEMINFO) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPos As Long) As Long

Private Sub Form_Load()


Dim hMenu As Long, hSubMenu As Long, MII As MENUITEMINFO
'get the handle of the current menu
hMenu = GetMenu(Me.hwnd)
'get the handle of the first submenu
hSubMenu = GetSubMenu(hMenu, 0)
'initialize the structure
MII.cbSize = Len(MII)
MII.fMask = MIIM_SUBMENU
'retrieve information about the menu item
GetMenuItemInfo hSubMenu, 0, True, MII
If MII.hSubMenu <> 0 Then
MsgBox "The specified menu item has a submenu."
Else
MsgBox "The specified menu item doesn't have a submenu."
End If
'display checked menu items using a radio-button mark instead of a check mark
MII.fMask = MIIM_TYPE
MII.fType = MFT_RADIOCHECK
MII.dwTypeData = mnuFileMenuItem.Caption
SetMenuItemInfo hSubMenu, 0, True, MII
End Sub

Private Sub mnuFileMenuItem_Click()


'if checked then uncheck
'if unchecked then check
mnuFileMenuItem.Checked = Not (mnuFileMenuItem.Checked)
End Sub
RemoveMenu

API Explanation
The RemoveMenu function deletes a menu item from the specified menu. If the menu item opens a
drop-down menu or submenu, RemoveMenu does not destroy the menu or its handle, allowing the
menu to be reused.

Parameter Information
Declare Function RemoveMenu Lib "user32" Alias "RemoveMenu" (ByVal hMenu As Long, ByVal
nPosition As Long, ByVal wFlags As Long) As Long

· hMenu
Identifies the menu to be changed.

· uPosition
Specifies the menu item to be deleted, as determined by the uFlags parameter.

· uFlags
Specifies how the uPosition parameter is interpreted. This parameter must be one of the following
values:
MF_BYCOMMAND
Indicates that uPosition gives the identifier of the menu item. If neither the MF_BYCOMMAND nor
MF_BYPOSITION flag is specified, the MF_BYCOMMAND flag is the default flag.
MF_BYPOSITION
Indicates that uPosition gives the zero-based relative position of the menu item.

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVa
l bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long)
As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Lon
g
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPosition As Long, ByVal wFlags As Long) As Long

Const MF_BYPOSITION = &H400&


Const MF_REMOVE = &H1000&

Private Sub Form_Load()


Dim hSysMenu As Long, nCnt As Long
' Get handle to our form's system menu
' (Restore, Maximize, Move, close etc.)
hSysMenu = GetSystemMenu(Me.hwnd, False)

If hSysMenu Then
' Get System menu's menu count
nCnt = GetMenuItemCount(hSysMenu)
If nCnt Then
' Menu count is based on 0 (0, 1, 2, 3...)
RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE ' Remove the
seperator
DrawMenuBar Me.hwnd
' Force caption bar's refresh. Disabling X button
Me.Caption = "Try to close me!"
End If
End If
End Sub

ModifyMenu

API Explanation
The ModifyMenu function changes an existing menu item. This function is used to specify the content,
appearance, and behavior of the menu item.

Parameter Information
Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal
nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As Any) As Long

· hMnu
Identifies the menu to be changed.

· uPosition
Specifies the menu item to be changed, as determined by the uFlags parameter.

· uFlags
Specifies flags that control the interpretation of the uPosition parameter and the content, appearance,
and behavior of the menu item. This parameter must be a combination of one of the following required
values and at least one of the values listed in the following Remarks section.
MF_BYCOMMAND
Indicates that the uPosition parameter gives the identifier of the menu item. The MF_BYCOMMAND
flag is the default if neither the MF_BYCOMMAND nor MF_BYPOSITION flag is specified.
MF_BYPOSITION
Indicates that the uPosition parameter gives the zero-based relative position of the menu item.

· uIDNewItem
Specifies either the identifier of the modified menu item or, if the uFlags parameter has the MF_POPUP
flag set, the handle of the drop-down menu or submenu.

· lpNewItem
Points to the content of the changed menu item. The interpretation of this parameter depends on
whether the uFlags parameter includes the MF_BITMAP, MF_OWNERDRAW, or MF_STRING flag.
MF_BITMAP
Contains a bitmap handle.
MF_OWNERDRAW
Contains a 32-bit value supplied by an application that is used to maintain additional data related to
the menu item. The value is in the itemData member of the structure pointed to by the lparam
parameter of the WM_MEASUREITEM or WM_DRAWITEM messages sent when the menu item is
created or its appearance is updated.
MF_STRING
Contains a pointer to a null-terminated string (the default).

'Download the full source+pictures+.. at


http://www.allapi.net/php/dlman/dm.cgi?id=bitmenu.zip&action=download
Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByV
al nPos As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPos As Long) As Long
Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal h
Menu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem
AsLong, ByVal lpString As Any) As Long

Const MF_BITMAP = 4
Const MF_CHECKED = 8

Private Sub Form_Load()


Dim hMenu As Long, hSubMenu As Long, lngID As Long

'Get the handle of the form's menu


hMenu = GetMenu(Me.hWnd)
'Get the handle of the form's submenu
hSubMenu = GetSubMenu(hMenu, 0)

'Change first item (index=0)


picBitmaps(0).Picture = picBitmaps(0).Image
lngID = GetMenuItemID(hSubMenu, 0)
Call ModifyMenu(hMenu, lngID, MF_BITMAP, lngID, CLng(picBitmaps(0).Picture))

'Change second item (index=1)


picBitmaps(1).Picture = picBitmaps(1).Image
lngID = GetMenuItemID(hSubMenu, 1)
Call ModifyMenu(hMenu, lngID, MF_BITMAP, lngID, CLng(picBitmaps(1).Picture))

'Change third item (index=2)


picBitmaps(2).Picture = picBitmaps(2).Image
lngID = GetMenuItemID(hSubMenu, 2)
Call ModifyMenu(hMenu, lngID, MF_BITMAP, lngID, CLng(picBitmaps(2).Picture))

mnuBitmap1.Checked = True
End Sub

Private Sub mnuBitmap1_Click()


mnuBitmap1.Checked = Not mnuBitmap1.Checked
End Sub

Private Sub mnuBitmapEnd_Click()


End
End Sub

GetSystemMenu

API Explanation
The GetSystemMenu function allows the application to access the window menu (also known as the
System menu or the Control menu) for copying and modifying.

Parameter Information
Declare Function GetSystemMenu Lib "user32" Alias "GetSystemMenu" (ByVal hwnd As Long, ByVal
bRevert As Long) As Long

· hWnd
Identifies the window that will own a copy of the window menu.

If the bRevert parameter is FALSE, the return value is the handle of a copy of the window menu. If the
bRevert parameter is TRUE, the return value is NULL.

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVa
l bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long)
As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Lon
g
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPosition As _
Long, ByVal wFlags As Long) As Long

Const MF_BYPOSITION = &H400&


Const MF_REMOVE = &H1000&

Private Sub Form_Load()


Dim hSysMenu As Long, nCnt As Long
' Get handle to our form's system menu
' (Restore, Maximize, Move, close etc.)
hSysMenu = GetSystemMenu(Me.hwnd, False)

If hSysMenu Then
' Get System menu's menu count
nCnt = GetMenuItemCount(hSysMenu)
If nCnt Then
' Menu count is based on 0 (0, 1, 2, 3...)
RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE ' Remove the
seperator
DrawMenuBar Me.hwnd
' Force caption bar's refresh. Disabling X button
Me.Caption = "Try to close me!"
End If
End If
End Sub
GetMenuItemRect

API Explanation
The GetMenuItemRect function retrieves the bounding rectangle for the specified menu item.

Parameter Information
Declare Function GetMenuItemRect Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long, ByVal
uItem As Long, lprcItem As RECT) As Long

· hWnd
[in] Handle to the window containing the menu.
Windows 98 and Windows 2000 or later: If this value is NULL and the hMenu parameter represents a
popup menu, the function will find the menu window.

· hMenu
[in] Handle to a menu.

· uItem
[in] Zero-based position of the menu item.

· lprcItem
[out] Pointer to a RECT structure that receives the bounding rectangle of the specified menu item
expressed in screen coordinates.

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

'Paste this code in a Form


'with a Menu named menu1 which has a menuitem named menu2

Private Type POINTAPI


x As Long
y As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetMenuItemRect Lib "user32" (ByVal hwnd As Long, By
Val hMenu As Long, ByVal uItem As Long, lprcItem As RECT) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPos As Long) As Long
Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
Private Const MOUSEEVENTF_MIDDLEUP = &H40
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx
As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long
)
Private Declare Function GetMessageExtraInfo Lib "user32" () As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long)
As Long
Const SM_CXSCREEN = 0 'X Size of screen
Const SM_CYSCREEN = 1 'Y Size of Screen

Private Sub Form_KeyPress(KeyAscii As Integer)


Dim mWnd As Long
mWnd = Me.hwnd
Dim hMenu As Long, hSubMenu As Long
hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
End Sub

Private Sub ScreenToAbsolute(lpPoint As POINTAPI)


lpPoint.x = lpPoint.x * (&HFFFF& / GetSystemMetrics(SM_CXSCREEN))lpPoint.y =
lpPoint.y * (&HFFFF& / GetSystemMetrics(SM_CYSCREEN))
End Sub

par Private Sub Click(p As POINTAPI)


'p.X and p.Y in absolute coordinates
'Put the mouse on the point
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE, p.x, p.y, 0, _
GetMessageExtraInfo()
'Mouse Down
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, GetMessageExtraInfo()
'Mouse Up
mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, GetMessageExtraInfo()
End Sub

Private Sub ClickMenuItem(ByVal mWnd As Long, ByVal hMenu As Long, ByVal Pos
As Long)
Dim ret As Long
par Dim r As RECT, p As POINTAPI
ret = GetMenuItemRect(mWnd, hMenu, Pos, r)
If ret = 0 Then Exit Sub
p.x = (r.Left + r.Right) / 2
p.y = (r.Top + r.Bottom) / 2
ScreenToAbsolute p
'Click on p
Click p
End Sub

Private Sub Form_Load()


Dim mWnd As Long, p As POINTAPI
mWnd = Me.hwnd
Dim hMenu As Long, hSubMenu As Long
hMenu = GetMenu(mWnd) 'Get the Menu of the Window(MenuBar)
ClickMenuItem mWnd, hMenu, 0 'Click on the first SubMenu
hSubMenu = GetSubMenu(hMenu, 0) 'Get its submenu
ClickMenuItem mWnd, hSubMenu, 0 'Click on the first MenuItem of the Submenu
p.x = &HFFFF& / 2
p.y = &HFFFF& / 2
Click p
Me.AutoRedraw = True
Me.BackColor = vbWhite
Print "Press any key"
End Sub

Private Sub menu2_Click()


MsgBox "Click"
End Sub

InsertMenuItem

API Explanation
The InsertMenuItem function inserts a new menu item at the specified position in a menu.

Parameter Information
Declare Function InsertMenuItem Lib "user32.dll" Alias "InsertMenuItemA" (ByVal hMenu As Long,
ByVal uItem _
As Long, ByVal fByPosition As Long, lpmii As MENUITEMINFO) As Long

· hMenu
Handle to the menu in which the new menu item is inserted.

· uItem
Identifier or position of the menu item before which to insert the new item. The meaning of this
parameter depends on the value of fByPosition.

· fByPosition
Value specifying the meaning of uItem. If this parameter is FALSE, uItem is a menu item identifier.
Otherwise, it is a menu item position.
· lpmii
Pointer to a MENUITEMINFO structure that contains information about the new menu item.

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

' Declarations and such needed for the example:


' (Copy them to the (declarations) section of a module.)
' There's quite a few declarations for this example, but it's worth it!

Public Declare Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As Long, B


yVal bRevert As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As Lon
g) As Long

Public 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_STATE = &H1


Public Const MIIM_ID = &H2
Public Const MIIM_TYPE = &H10
Public Const MFT_SEPARATOR = &H800
Public Const MFT_STRING = &H0
Public Const MFS_ENABLED = &H0
Public Const MFS_CHECKED = &H8

Public Declare Function InsertMenuItem Lib "user32.dll" Alias "InsertMenuItem


A" (ByVal hMenu As Long, _ ByVal uItem As Long, ByVal fByPosition As Long, lp
mii AsMENUITEMINFO) As Long
Public Declare Function SetMenuItemInfo Lib "user32.dll" Alias "SetMenuItemIn
foA" (ByVal hMenu As Long, _ ByVal uItem As Long, ByVal fByPosition As Long,
lpmiiAs MENUITEMINFO) As Long
Public Declare Function SetWindowPos Lib "user32.dll" (ByVal hWnd As Long, By
Val hWndInsertAfter As Long, _ ByVal x As Long, ByVal y As Long, ByVal cx As
Long,ByVal cy As Long, ByVal wFlags As Long) As Long

Public Const HWND_TOPMOST = -1


Public Const HWND_NOTOPMOST = -2
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1

Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA"


(ByVal hWnd As Long, _ ByVal nIndex As Long, ByVal dwNewLong As Long) As Lon
g
Public Const GWL_WNDPROC = -4
Public Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProc
A" (ByVal lpPrevWndFunc _ As Long, ByVal hWnd As Long, ByVal Msg As Long, ByV
alwParam As Long, ByVal lParam As Long) As Long
Public Const WM_SYSCOMMAND = &H112
Public Const WM_INITMENU = &H116

' Add an option to make window Form1 "Always On Top" to the bottom of its
system
' menu. A check mark appears next to this option when active. The menu item
acts as a toggle.
' Note how subclassing the window is necessary to process the two messages
needed
' to give the added system menu item its full functionality.

' *** Place the following code in a module. ***

Public pOldProc As Long ' pointer to Form1's previous window procedure


Public ontop As Boolean ' identifies if Form1 is always on top or not

' The following function acts as Form1's window procedure to process


messages.
Public Function WindowProc (ByVal hwnd As Long, ByVal uMsg As Long, ByVal wPa
ram As Long, ByVal lParam _ As Long) As Long
Dim hSysMenu As Long ' handle to Form1's system menu
Dim mii As MENUITEMINFO ' menu item information for Always On Top
Dim retval As Long ' return value

Select Case uMsg


Case WM_INITMENU
' Before displaying the system menu, make sure that the Always On Top
' option is properly checked.
hSysMenu = GetSystemMenu(hwnd, 0)
With mii
' Size of the structure.
.cbSize = Len(mii)
' Only use what needs to be changed.
.fMask = MIIM_STATE
' If Form1 is now always on top, check the item.
.fState = MFS_ENABLED Or IIf(ontop, MFS_CHECKED, 0)
End With
retval = SetMenuItemInfo(hSysMenu, 1, 0, mii)
WindowProc = 0
Case WM_SYSCOMMAND
' If Always On Top (ID = 1) was selected, change the on top/not on top
' setting of Form1 to match.
If wParam = 1 Then
' Reverse the setting and make it the current one.
ontop = Not ontop
retval = SetWindowPos(hwnd, IIf(ontop, HWND_TOPMOST, HWND_NOTOPMOST), _
0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
WindowProc = 0
Else
' Some other item was selected. Let the previous window procedure
' process it.
WindowProc = CallWindowProc(pOldProc, hwnd, uMsg, wParam, lParam)
End If
Case Else
' If this is some other message, let the previous procedure handle it.
WindowProc = CallWindowProc(pOldProc, hwnd, uMsg, wParam, lParam)
End Select
End Function

' *** Place the following code inside Form1. ***

' When Form1 loads, add Always On Top to the system menu and set up the
' new window procedure.
Private Sub Form_Load()
Dim hSysMenu As Long ' handle to the system menu
Dim count As Long ' the number of items initially on the menu
Dim mii As MENUITEMINFO ' describes a menu item to add
Dim retval As Long ' return value

' Get a handle to the system menu.


hSysMenu = GetSystemMenu(Form1.hWnd, 0)
' See how many items are currently in it.
count = GetMenuItemCount(hSysMenu)

' Add a separator bar and then Always On Top to the system menu.
With mii
' The size of the structure.
.cbSize = Len(mii)
' What parts of the structure to use.
.fMask = MIIM_ID Or MIIM_TYPE
' This is a separator.
.fType = MFT_SEPARATOR
' It has an ID of 0.
.wID = 0
End With
' Add the separator to the end of the system menu.
retval = InsertMenuItem(hSysMenu, count, 1, mii)
' Likewise, add the Always On Top command.
With mii
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
' This is a regular text item.
.fType = MFT_STRING
' The option is enabled.
.fState = MFS_ENABLED
' It has an ID of 1 (this identifies it in the window procedure).
.wID = 1
' The text to place in the menu item.
.dwTypeData = "&Always On Top"
.cch = Len(.dwTypeData)
End With
' Add this to the bottom of the system menu.
retval = InsertMenuItem(hSysMenu, count + 1, 1, mii)

' Set the custom window procedure to process Form1's messages.


ontop = False
pOldProc = SetWindowLong(Form1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub

' Before unloading, restore the default system menu and remove the
' custom window procedure.
Private Sub Form_Unload(Cancel As Integer)
Dim retval As Long ' return value

' Replace the previous window procedure to prevent crashing.


retval = SetWindowLong(Form1.hWnd, GWL_WNDPROC, pOldProc)
' Remove the modifications made to the system menu.
retval = GetSystemMenu(Form1.hWnd, 1)
End Sub

DrawMenuBar

PI Explanation
The DrawMenuBar function redraws the menu bar of the specified window. If the menu bar changes
after Windows has created the window, this function must be called to draw the changed menu bar.

Parameter Information
Declare Function DrawMenuBar Lib "user32" Alias "DrawMenuBar" (ByVal hwnd As Long) As Long

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVa
l bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long)
As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Lon
g
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPosition As Long, _
ByVal wFlags As Long) As Long

Const MF_BYPOSITION = &H400&


Const MF_REMOVE = &H1000&

Private Sub Form_Load()


Dim hSysMenu As Long, nCnt As Long
' Get handle to our form's system menu
' (Restore, Maximize, Move, close etc.)
hSysMenu = GetSystemMenu(Me.hwnd, False)

If hSysMenu Then
' Get System menu's menu count
nCnt = GetMenuItemCount(hSysMenu)
If nCnt Then
' Menu count is based on 0 (0, 1, 2, 3...)
RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE ' Remove the
seperator
DrawMenuBar Me.hwnd
' Force caption bar's refresh. Disabling X button
Me.Caption = "Try to close me!"
End If
End If

GetMenuItemID

API Explanation
The GetMenuItemID function retrieves the menu item identifier of a menu item located at
the specified position in a menu.

Parameter Information
Declare Function GetMenuItemID Lib "user32" Alias "GetMenuItemID" (ByVal hMenu As
Long, ByVal nPos As _
Long) As Long

· hMenu
Identifies the menu that contains the item whose identifier is to be retrieved.

If the function succeeds, the return value specifies the identifier of the given menu item. If
the menu item identifier is NULL or if the specified item opens a submenu, the return value
is 0xFFFFFFFF.

'Download the full source+pictures+.. at


http://www.allapi.net/php/dlman/dm.cgi?id=bitmenu.zip&action=download
Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByV
al nPos As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPos As Long) As Long
Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal h
Menu As Long, ByVal nPosition _ As Long, ByVal wFlags As Long, ByVal wIDNewIt
em AsLong, ByVal lpString As Any) As Long
Const MF_BITMAP = 4
Const MF_CHECKED = 8

Private Sub Form_Load()


Dim hMenu As Long, hSubMenu As Long, lngID As Long

'Get the handle of the form's menu


hMenu = GetMenu(Me.hWnd)
'Get the handle of the form's submenu
hSubMenu = GetSubMenu(hMenu, 0)

'Change first item (index=0)


picBitmaps(0).Picture = picBitmaps(0).Image
lngID = GetMenuItemID(hSubMenu, 0)
Call ModifyMenu(hMenu, lngID, MF_BITMAP, lngID, CLng(picBitmaps(0).Picture))

'Change second item (index=1)


picBitmaps(1).Picture = picBitmaps(1).Image
lngID = GetMenuItemID(hSubMenu, 1)
Call ModifyMenu(hMenu, lngID, MF_BITMAP, lngID, CLng(picBitmaps(1).Picture))

'Change third item (index=2)


picBitmaps(2).Picture = picBitmaps(2).Image
lngID = GetMenuItemID(hSubMenu, 2)
Call ModifyMenu(hMenu, lngID, MF_BITMAP, lngID, CLng(picBitmaps(2).Picture))

mnuBitmap1.Checked = True
End Sub

Private Sub mnuBitmap1_Click()


mnuBitmap1.Checked = Not mnuBitmap1.Checked
End Sub

Private Sub mnuBitmapEnd_Click()


End
End Sub

GetMenuItemCount

API Explanation
The GetMenuItemCount function determines the number of items in the specified menu.

Parameter Information
Declare Function GetMenuItemCount Lib "user32" Alias "GetMenuItemCount" (ByVal hMenu
As Long) As Long

· hMenu
Identifies the handle of the menu to be examined.

If the function succeeds, the return value specifies the number of items in the menu.

If the function fails, the return value is -1. To get extended error information, call
GetLastError.

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVa
l bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long)
As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Lon
g
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal
nPosition As Long, ByVal _ wFlags As Long) As Long
Const MF_BYPOSITION = &H400&
Const MF_REMOVE = &H1000&

Private Sub Form_Load()


Dim hSysMenu As Long, nCnt As Long
' Get handle to our form's system menu
' (Restore, Maximize, Move, close etc.)
hSysMenu = GetSystemMenu(Me.hwnd, False)

If hSysMenu Then
' Get System menu's menu count
nCnt = GetMenuItemCount(hSysMenu)
If nCnt Then
' Menu count is based on 0 (0, 1, 2, 3...)
RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE ' Remove the
seperator
DrawMenuBar Me.hwnd
' Force caption bar's refresh. Disabling X button
Me.Caption = "Try to close me!"
End If
End If
End Sub

GetMenuItemInfo

API Explanation
The GetMenuItemInfo function retrieves information about a menu item.

Parameter Information
Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As Long,
ByVal un As _
Long, ByVal b As Boolean, lpmii As MENUITEMINFO) As Long

· hMenu
Handle to the menu that contains the menu item.

· uItem

Identifier or position of the menu item to get information about. The meaning of this parameter
depends on the value of fByPosition.

· fByPosition
Value specifying the meaning of uItem. If this parameter is FALSE, uItem is a menu item identifier.
Otherwise, it is a menu item position.

· lpmii
Pointer to a MENUITEMINFO structure that specifies the information to retrieve and receives
information about the menu item.

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, use the GetLastError
function.

{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\


f1\froman\fcharset2 Symbol;}{\f2\fswiss MS Sans Serif;}}
{\colortbl\red0\green0\blue0;}
\deflang1044\pard\plain\f2\fs17 Private Const MFT_RADIOCHECK = &H200&
\par Private Const MIIM_TYPE = &H10
\par Private Const MIIM_SUBMENU = &H4
\par
\par Private Type MENUITEMINFO
\par cbSize As Long
\par fMask As Long
\par fType As Long
\par fState As Long
\par wID As Long
\par hSubMenu As Long
\par hbmpChecked As Long
\par hbmpUnchecked As Long
\par dwItemData As Long
\par dwTypeData As String
\par cch As Long
\par End Type
\par
\par Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Lo
ng
\par Private Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItem
InfoA" (ByVal hMenu As Long, ByVal _
\par \tab un As Long, ByVal b As Boolean, lpmii As MENUITEMINFO) As Long
\par Private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItem
InfoA" (ByVal hMenu As Long, _
\par \tab ByVal uItem As Long, ByVal fByPosition As Long, lpmii As MENUITEMIN
FO) As Long
\par Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, B
yVal nPos As Long) As Long
\par
\par Private Sub Form_Load()
\par Dim hMenu As Long, hSubMenu As Long, MII As MENUITEMINFO
\par 'get the handle of the current menu
\par hMenu = GetMenu(Me.hwnd)
\par 'get the handle of the first submenu
\par hSubMenu = GetSubMenu(hMenu, 0)
\par 'initialize the structure
\par MII.cbSize = Len(MII)
\par MII.fMask = MIIM_SUBMENU
\par 'retrieve information about the menu item
\par GetMenuItemInfo hSubMenu, 0, True, MII
\par If MII.hSubMenu <> 0 Then
\par MsgBox "The specified menu item has a submenu."
\par Else
MsgBox "The specified menu item doesn't have a submenu."
End If
'display checked menu items using a radio-button mark instead of a check mark
MII.fMask = MIIM_TYPE
MII.fType = MFT_RADIOCHECK
MII.dwTypeData = mnuFileMenuItem.Caption
SetMenuItemInfo hSubMenu, 0, True, MII
End Sub

Private Sub mnuFileMenuItem_Click()


'if checked then uncheck
'if unchecked then check
mnuFileMenuItem.Checked = Not (mnuFileMenuItem.Checked)
End Sub

DestroyMenu

API Explanation
The DestroyMenu function destroys the specified menu and frees any memory that the
menu occupies.

Parameter Information
Declare Function DestroyMenu Lib "user32" Alias "DestroyMenu" (ByVal hMenu As Long) As
Long

Const MF_CHECKED = &H8&


Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&

Private Type POINTAPI


x As Long
y As Long
End Type

Private Declare Function CreatePopupMenu Lib "user32" () As Long


Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, By
Val wFlags As Long, _
ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long
, ByVal lprc _
As Any) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVa
l bRevert As Long) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal h
Menu As Long, ByVal _
wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Lo
ng
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As L
ong
Dim hMenu As Long

Private Sub Form_Load()


'Create an empty popupmenu
hMenu = CreatePopupMenu()
'Append a few menu items
AppendMenu hMenu, MF_STRING, ByVal 0&, "Hello !"
AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, ByVal 0&, "Testing ..."
AppendMenu hMenu, MF_SEPARATOR, ByVal 0&, ByVal 0&
AppendMenu hMenu, MF_CHECKED, ByVal 0&, "TrackPopupMenu"
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y


As Single)
Dim Pt As POINTAPI
'Get the position of the mouse cursor
GetCursorPos Pt
If Button = 1 Then
'Show our popupmenu
TrackPopupMenu hMenu, TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
Else
'Show our form's default popup menu
TrackPopupMenu GetSystemMenu(Me.hwnd, False), TPM_LEFTALIGN, Pt.x, Pt.y, 0, M
e.hwnd, ByVal 0&
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)


'Destroy our menu
DestroyMenu hMenu
End Sub

EndMenu

API Explanation
The EndMenu function ends the calling thread's active menu.

Parameter Information
Declare Function EndMenu Lib "user32.dll" () As Long

'This project requires a timer (Timer1) and a menu (mnuPopUp) with one or
more menu items
Private Declare Function EndMenu Lib "user32.dll" () As Long

Private Sub Form_Load()


'set the interval of the timer to 3 seconds
Timer1.Interval = 3000
End Sub

Private Sub Form_Click()


'show the menu
PopupMenu mnuPopUp
'start the timer
Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()


'if the menu is still open, close it
EndMenu
'stop the timer
Timer1.Enabled = False
End Sub

Potrebbero piacerti anche