GetMenu Function

Declare Function GetMenu Lib "user32.dll" (ByVal hWnd As Long) As Long

Platforms

Description & Usage

GetMenu identifies what menu is assigned to a given window. This menu appears as the window's menu bar, although programatically the menu bar is just the top menu in the menu heirarchy.

Return Value

The function returns a handle to the menu assigned to the specified window. If the window has no menu, the function returns 0. If the window specified in the parameter list is a child window, the function's return value is meaningless.

Visual Basic-Specific Issues

None.

Parameters

hWnd
A handle to the window to get the menu of.

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

GetSystemMenu

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/getmenu.html