GetMenuItemInfo Function

Declare Function GetMenuItemInfo Lib "user32.dll" Alias "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal uItem As Long, ByVal fByPosition As Long, lpmii As MENUITEMINFO) As Long

Platforms

Description & Usage

GetMenuItemInfo retrieves information about an item on a menu. The type of information retrieved is determined by the flags specified by the fMask data member of the structure passed as the lpmii parameter.

Return Value

If successful, the function returns a non-zero value. If an error occured, the function returns 0 (use GetLastError to get the error code).

Visual Basic-Specific Issues

None.

Parameters

hMenu
A handle to the menu that contains the item to get information about.
uItem
Identifies the menu item to get information about. This could be either a position or a menu item identifier, depending on fByPosition.
fByPosition
If this is a non-zero value, uItem indicates the item by using its zero-based position. (For example, the first item in the menu has a position of 0.) If this is zero, then uItem is the unique menu item identifier of the item.
lpmii
Receives information about the menu item. Before calling GetMenuItemInfo, the cbSize and fMask data members of this structure must be initialized to specify what information about the item to retrieve. If getting the text of the menu item, the dwTypeData and cch data members must also be initialized to receive the string. (See the example below for a demonstration of this.)

Example

Before running this example, use the Menu Editor utility to create a small menu system on Form1. It doesn't matter what the menus look like, but some sort of menus are necessary.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Declare Function GetMenu Lib "user32.dll" (ByVal hWnd As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As Long) 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_SUBMENU = &H4
Public Const MIIM_TYPE = &H10
Public Const MFT_SEPARATOR = &H800
Public Const MFS_CHECKED = &H8
Public Declare Function GetMenuItemInfo Lib "user32.dll" Alias "GetMenuItemInfoA" (ByVal _
	hMenu As Long, ByVal uItem As Long, ByVal fByPosition As Long, lpmii As _
	MENUITEMINFO) As Long

' When button Command1 is pressed, output the structure of the entire menu system
' of Form1 to the Debug window.  The entire menu heirarchy is displayed, and any items
' that are checked are identified.  A recursive subroutine is used to output the contents
' of each individual menu, calling itself whenever a submenu is found.

' *** Place the following code inside a module. ***
' This function performs the recursive output of the menu structure.
Public Sub IterateThroughItems(ByVal hMenu As Long, ByVal level As Long)
	' hMenu is a handle to the menu to output
	' level is the level of recursion, used to indent submenu items
	Dim itemcount As Long    ' the number of items in the specified menu
	Dim c As Long            ' loop counter variable
	Dim mii As MENUITEMINFO  ' receives information about each item
	Dim retval As Long       ' return value
	
	' Count the number of items in the menu passed to this subroutine.
	itemcount = GetMenuItemCount(hMenu)
	
	' Loop through the items, getting information about each one.	
	With mii
		.cbSize = Len(mii)
		.fMask = MIIM_STATE Or MIIM_TYPE Or MIIM_SUBMENU
		For c = 0 To itemcount - 1
			' Make room in the string buffer.
			.dwTypeData = Space(256)
			.cch = 256
			' Get information about the item.
			retval = GetMenuItemInfo(hMenu, c, 1, mii)
			' Output a line of information about this item.
			If mii.fType = MFT_SEPARATOR Then
				' This is a separator bar.
				Debug.Print "   " & String(3 * level, ".") & "-----"
			Else
				' This is a text item.
				' If this is checked, display (X) in the margin.
				Debug.Print IIf(.fState And MFS_CHECKED, "(X)", "   ");
				' Display the text of the item.
				Debug.Print String(3 * level, ".") & Left(.dwTypeData, .cch)
				' If this item opens a submenu, display its contents.
				If .hSubMenu <> 0 Then
					IterateThroughItems .hSubMenu, level + 1
				End If
			End If
		Next c
	End With
End Sub

' *** Place the following code inside Form1. ***
' When Command1 is clicked, output the entire contents of Form1's menu system.
Private Sub Command1_Click()
	Dim hMenu As Long  ' handle to the menu bar of Form1
	
	' Get a handle to Form1's menu bar.
	hMenu = GetMenu(Form1.hWnd)
	' Use the above function to output its contents.
	IterateThroughItems hMenu, 0
End Sub

See Also

SetMenuItemInfo

Category

Menus

Back to the Function list.
Back to the Reference section.


Last Modified: June 4, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getmenuiteminfo.html