DestroyMenu Function

Declare Function DestroyMenu Lib "user32.dll" (ByVal hMenu As Long) As Long

Platforms

Description & Usage

DestroyMenu destroys a menu resource. This menu can be either a "regular" menu (i.e., a menu bar) or a popup menu. Menus should be destroyed when no longer needed in order to free resources. However, it is not necessary to call DestroyMenu to destroy a menu that is assigned as a window's menu (or any submenus of that menu). Those menus are automatically destroyed when their window closes.

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 to be destroyed.

Example

' 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 CreatePopupMenu Lib "user32.dll" () As Long
Public Declare Function DestroyMenu 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_ID = &H2
Public Const MIIM_TYPE = &H10
Public Const MFT_SEPARATOR = &H800
Public Const MFT_STRING = &H0
Public Const MFS_DEFAULT = &H1000
Public Const MFS_ENABLED = &H0
Public 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
Public Type RECT
	left As Long
	top As Long
	right As Long
	bottom As Long
End Type
Public Type TPMPARAMS
	cbSize As Long
	rcExclude As RECT
End Type
Public Declare Function TrackPopupMenuEx Lib "user32.dll" (ByVal hMenu As Long, ByVal _
	fuFlags As Long, ByVal x As Long, ByVal y As Long, ByVal hWnd As Long, lptpm As _
	TPMPARAMS) As Long
Public Const TPM_LEFTALIGN = &H0
Public Const TPM_TOPALIGN = &H0
Public Const TPM_NONOTIFY = &H80
Public Const TPM_RETURNCMD = &H100
Public Const TPM_LEFTBUTTON = &H0
Public Type POINT_TYPE
	x As Long
	y As Long
End Type
Public Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINT_TYPE) As Long
Public Declare Function SetRectEmpty Lib "user32.dll" (lpRect As RECT) As Long

' When the user clicks button Command1, have a very simple popup menu appear.  The
' menu only has two options, divided by a separator bar.  The menu is created when
' needed and is destroyed after its use.

' The following application-defined constants are used to name the menu item
' identifiers used by this example.  They are not actually part of the API; instead, they are
' used just to eliminate "magic numbers."
Private Const ID_ABOUT = 101
Private Const ID_SEPARATOR = 102
Private Const ID_EXIT = 103

Private Sub Command1_Click()
	Dim hPopupMenu As Long    ' handle to the popup menu to display
	Dim mii As MENUITEMINFO   ' describes menu items to add
	Dim tpm As TPMPARAMS      ' identifies the exclusion rectangle
	Dim curpos As POINT_TYPE  ' holds the current mouse coordinates
	Dim menusel As Long       ' ID of what the user selected in the popup menu
	Dim retval As Long        ' generic return value
	
	' Create the popup menu that will be displayed.
	hPopupMenu = CreatePopupMenu()
	' Add the menu's first item: "About This Problem..."
	With mii
		' The size of this structure.
		.cbSize = Len(mii)
		' Which elements of the structure to use.
		.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
		' The type of item: a string.
		.fType = MFT_STRING
		' This item is currently enabled and is the default item.
		.fState = MFS_ENABLED Or MFS_DEFAULT
		' Assign this item an item identifier.
		.wID = ID_ABOUT
		' Display the following text for the item.
		.dwTypeData = "&About This Example..."
		.cch = Len(.dwTypeData)
	End With
	retval = InsertMenuItem(hPopupMenu, 0, 1, mii)
	' Add the second item: a separator bar.
	With mii
		.fType = MFT_SEPARATOR
		.fState = MFS_ENABLED
		.wID = ID_SEPARATOR
	End With
	retval = InsertMenuItem(hPopupMenu, 1, 1, mii)
	' Add the final item: "Exit".
	With mii
		.fType= MFT_STRING
		.wID = ID_EXIT
		.dwTypeData = "E&xit"
		.cch = Len(.dwTypeData)
	End With
	retval = InsertMenuItem(hPopupMenu, 2, 1, mii)
	
	' Determine where the mouse cursor currently is, in order to have
	' the popup menu appear at that point.
	retval = GetCursorPos(curpos)
	
	' Make the exclusion rectangle empty because there's no need for it here.
	With tpm
		' Size of the structure.
		.cbSize = Len(tpm)
		' Make the exclusion rectangle empty.
		retval = SetRectEmpty(.rcExclude)
	End With
	
	' Display the popup menu at the mouse cursor.  Instead of sending messages
	' to window Form1, have the function merely return the ID of the user's selection.
	menusel = TrackPopupMenuEx(hPopupMenu, TPM_TOPALIGN Or TPM_LEFTALIGN Or TPM_NONOTIFY _
		Or TPM_RETURNCMD Or TPM_LEFTBUTTON, curpos.x, curpos.y, Form1.hWnd, tpm)
	
	' Before acting upon the user's selection, destroy the popup menu now.
	retval = DestroyMenu(hPopupMenu)
	Select Case menusel
	Case ID_ABOUT
		' Use the Visual Basic MsgBox function to display a short message in a dialog
		' box.  Using the MessageBox API function isn't necessary.
		retval = MsgBox("This example demonstrates how to use the API to display " & _
			 "a pop-up menu.", vbOkOnly Or vbInformation, "Windows API Guide")
	Case ID_EXIT
		' End this program by closing and unloading Form1.
		Unload Form1
	End Select
End Sub

See Also

CreatePopupMenu

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/d/destroymenu.html