Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
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 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&
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
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 fails, the return value is zero. To get extended error information, use the GetLastError
function.
' In a form
Option Explicit
'Create the following menu on this form:
' mnuFile
' mnuFileItem1
' mnuFileItem2
' mnuFileBar1
' mnuFileItem3
' mnuFileItem4
' mnuFileItem5
' mnuFileItem6
'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...
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.
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
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
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 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
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
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).
Const MF_BITMAP = 4
Const MF_CHECKED = 8
mnuBitmap1.Checked = True
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
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 fails, the return value is zero. To get extended error information, call GetLastError.
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 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
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 fails, the return value is zero. To get extended error information, call GetLastError.
' 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.
' 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
' 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)
' 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
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
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.
mnuBitmap1.Checked = True
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&
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 fails, the return value is zero. To get extended error information, use the GetLastError
function.
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
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