I need to get the handle of one of my menu items, how do I get it?
when I use the GetMenu API it returns the handle of the main menu object, and when I try to use GetSubMenu it always returns NULL.
Printable View
I need to get the handle of one of my menu items, how do I get it?
when I use the GetMenu API it returns the handle of the main menu object, and when I try to use GetSubMenu it always returns NULL.
Anybody? :(
Using functions that you specified should do the job just fine. Here is a quick example. I have a few menus with couple submenus on my form.
Code: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 GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetMenuString Lib "user32" Alias "GetMenuStringA" (ByVal hMenu As Long, ByVal wIDItem As Long, ByVal lpString As String, ByVal nMaxCount As Long, ByVal wFlag As Long) As Long
Private Const MF_BYPOSITION = &H400&
Private Sub Command1_Click()
Dim lngMenu As Long
Dim lngMenuCount As Long
Dim lngSubMenu As Long
Dim lngSubMenuCount As Long
Dim i As Long
Dim j As Long
Dim strBuffer As String
Dim strOutput As String
lngMenu = GetMenu(Me.hwnd)
Debug.Print "Main Menu hWnd: " & lngMenu
lngMenuCount = GetMenuItemCount(lngMenu)
For i = 0 To lngMenuCount - 1
strBuffer = Space(125)
GetMenuString lngMenu, i, strBuffer, Len(strBuffer), MF_BYPOSITION
strBuffer = Left(strBuffer, InStr(strBuffer, vbNullChar) - 1)
lngSubMenu = GetSubMenu(lngMenu, i)
Debug.Print "Menu " & strBuffer & " hWnd: " & lngSubMenu
lngSubMenuCount = GetMenuItemCount(lngSubMenu)
For j = 0 To lngSubMenuCount - 1
strBuffer = Space(125)
GetMenuString lngSubMenu, j, strBuffer, Len(strBuffer), MF_BYPOSITION
strBuffer = Left(strBuffer, InStr(strBuffer, vbNullChar) - 1)
Debug.Print vbTab & strBuffer
Next
Next
End Sub
Thank Serge, your example works, but mine doesn't, and it uses the same functions in the same way. :(
Can you post an example of your code?
don't ask me why I need to pop up the File menu, I just do. :pCode:'I have a form with border style 3 - Fixed dialog
'Menus on the form: File Help
' ---- ----
' Open Contents
' Save (separator)
' Close About
' (separator)
' Import
' Export
' (separator)
' Exit
'Both the File and the Help menus are invisible, but it doesn't have anything to do with it, I tries without the Invisible.
Private Sub picMenu_Click()
Dim hMainMenu As Long
Dim hMenu As Long
Dim hSubMenu As Long
hMainMenu = GetMenu(Me.hWnd)
hMenu = GetSubMenu(hMainMenu, 1)
hSubMenu = GetSubMenu(hMenu, 1)
'Other code that doesn't have to do with the menus...
PopupMenu mnuFile, 0, 0, 0
End Sub