Results 1 to 15 of 15

Thread: GetMenu (Megatron or Matthew, please help!!!)

  1. #1
    spetnik
    Guest

    GetMenu (Megatron or Matthew, please help!!!)

    How can I loop through all the menus in a window, even those that are not visible? GetMenu only returns handles for visible menus.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Denver
    Posts
    265
    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

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  4. #4
    spetnik
    Guest
    SORRY GUYS!!!! These menus are not on my form. I need to access them via API.
    Again, Megatron or Matthew, PLEASE HELP!!!

  5. #5

  6. #6
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    I suspected as much :P
    Oh well, I don't know. I bow before the superior knowledge of the vbforum's elite.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  7. #7
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Denver
    Posts
    265
    Perhaps you should have (*ahem*) give that information in your first post...

  8. #8
    spetnik
    Guest

    Cool

    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 :'(

    . .>-
    \/ /
    /-|--
    /\
    / \

  9. #9
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    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

  10. #10
    spetnik
    Guest
    Originally posted by Acoustic
    Perhaps you should have (*ahem*) give that information in your first post...
    well, I thought that mentioning the GetMenu() API function was enough sorry

  11. #11
    spetnik
    Guest
    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
    I really apologize. I never mean bad. I am just a little impatient as my manager needs this ASAP,

  12. #12
    Matthew Gates
    Guest
    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

  13. #13
    Matthew Gates
    Guest
    By the way, I believe that's Aaron Young's code .

  14. #14
    spetnik
    Guest
    I am deeply humbled as I bow before thee, my master. I will try it, thanx.

  15. #15
    spetnik
    Guest
    :'( 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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width