MenuSelect




Option Explicit

#If Win32 Then
Declare Function GetMenuState Lib "user32" _
(ByVal hMenu As Long, ByVal wID As Long, _
ByVal wFlags As Long) As Long
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 GetMenuItemID Lib "user32" _
(ByVal hMenu As Long, ByVal nPos As Long) As Long
#Else
Declare Function GetMenuState Lib "user" (ByVal hMenu As Long, _
ByVal wID As Long, ByVal wFlags As Long) As Long
Declare Function GetMenu Lib "user" (ByVal hWnd As Long) _
As Long
Declare Function GetSubMenu Lib "user" (ByVal hMenu As Long, _
ByVal nPos As Long) As Long
Declare Function GetMenuItemID Lib "user" (ByVal hMenu As Long, _
ByVal nPos As Long) As Long
#End If

Public Function MenuSelected(ByVal hWnd As Long, _
ByVal TopMenu As Integer, ByVal SubMenu As Integer) As Long
' Define explicit variables

Dim hMenu&, hSubMenu&, wID&
' Get the handle of your form's menubar

hMenu& = GetMenu(hWnd)
' Get the handle of the specified top-level menu (e.g File)

hSubMenu& = GetSubMenu(hMenu&, TopMenu)
' Get the handle of the specified sub-level menu (e.g New)

wID& = GetMenuItemID(hSubMenu&, SubMenu)
' Return True if menu is selected, else return false

MenuSelected = GetMenuState(hMenu&, wID&, &H8000&)
' Make sure the correct value is being returned

If MenuSelected > 1 And MenuSelected < 128 Then MenuSelected = 0
End Function










( menuselect.html )- by Paolo Puglisi - Modifica del 17/12/2023