How can I loop through all the menus in a window, even those that are not visible? GetMenu only returns handles for visible menus.
Printable View
How can I loop through all the menus in a window, even those that are not visible? GetMenu only returns handles for visible menus.
The people you asked for help do indeed post a lot...but they're not the only ones on this this board who can answer basic questions. ;)
I don't know what you're doing with the menu's, so I just added each one to a listbox. Just change the code in the If statement to do what you need.
NOTE: for this example I made sure that each menu's name started with "mnu" (i.e. mnuFile, mnuEdit). This is good coding practice and makes this example easy
Try this:
Code:Private Sub Command1_Click()
Dim mymenu As Control
'form1 is the form with the menu's you want.
'I'm sure you can figure out what to do with multiple
'forms :p
For Each mymenu In Form1.Controls
If InStr(1, mymenu.Name, "mnu") Then
List1.AddItem mymenu.Name
End If
Next
End Sub
Use the TypeName Function!
Code:Private Sub Command1_Click()
Dim mymenu As Control
'form1 is the form with the menu's you want.
'I'm sure you can figure out what to do with multiple
'forms
For Each mymenu In Form1.Controls
If strComp(TypeName(mymenu), "Menu") = 0 Then
List1.AddItem mymenu.Name
End If
Next
End Sub
SORRY GUYS!!!! These menus are not on my form. I need to access them via API.
Again, Megatron or Matthew, PLEASE HELP!!!
my, arent we getting cocky?
I suspected as much :P
Oh well, I don't know. I bow before the superior knowledge of the vbforum's elite.
Perhaps you should have (*ahem*) give that information in your first post...:rolleyes:
O, LORDS Megatron and Matthew, I bow before thee (plural?). I offer thee all the memory my humble machine has to give. I pledge with all of my processing speed to grant you the bandwidth you request. O please, help. Grant me this assistance. Do not deny me this one time!!!!
:'( OH I POUR MY HEART OUT :'(
. .>-
\/ /
/-|--
/\
/ \
no shi't eh?
some people just get on yer nerves
nice people trying to help him and all they get is cockyness
and when they answer the question properly, he gets all cocky about that its not what he asked..
ok enough about bitchin
well, I thought that mentioning the GetMenu() API function was enough sorry :pQuote:
Originally posted by Acoustic
Perhaps you should have (*ahem*) give that information in your first post...:rolleyes:
I really apologize. I never mean bad. I am just a little impatient as my manager needs this ASAP,Quote:
Originally posted by kovan
no shi't eh?
some people just get on yer nerves
nice people trying to help him and all they get is cockyness
and when they answer the question properly, he gets all cocky about that its not what he asked..
ok enough about bitchin
Try this:
Code:Private Declare Function GetMenu Lib "user32" (ByVal _
hWnd As Long) As Long
Private Declare Function FindWindowEx Lib "user32" _
Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As _
Long, ByVal lpsz1 As String, ByVal lpsz2 As String) 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 ListMenuStructure(ByVal hWnd As Long, ByRef oList As ListBox)
Dim lMenu As Long
lMenu = GetMenu(hWnd)
oList.Clear
If lMenu Then RecurseMenu lMenu, oList, 1
End Sub
Private Sub RecurseMenu(ByVal lMenu As Long, ByRef oList As ListBox, ByVal iLevel As Long)
Dim lItems As Long
Dim lItem As Long
Dim sCaption As String
lItems = GetMenuItemCount(lMenu)
If lItems Then
For lItem = 0 To lItems - 1
sCaption = Space$(255)
sCaption = Left$(sCaption, GetMenuString(lMenu, lItem, ByVal sCaption, 255, MF_BYPOSITION))
oList.AddItem String((iLevel - 1) * 2, "-") & sCaption
If GetSubMenu(lMenu, lItem) Then
RecurseMenu GetSubMenu(lMenu, lItem), oList, iLevel + 1
End If
Next
End If
End Sub
Private Sub Command1_Click()
ListMenuStructure FindWindowEx(0, 0, "NotePad", vbNullString), List1
End Sub
By the way, I believe that's Aaron Young's code :rolleyes:.
I am deeply humbled as I bow before thee, my master. I will try it, thanx.
:'( sorry. It does not work. It only shows those menus that are visible. Maybe Ill just tell ya what Im tryin to accomplish. I need to get all of the menu items in a specific IE window (who's handle i have) and get the handles to all menus and thereby disable those I wish too. However, getmenu returns 0, so the only menu i can touch is the system menu. SO if u have a way of doing this any other way, i would be deeply greatful. thanx!