Results 1 to 10 of 10

Thread: Gurus, I need help...

  1. #1
    Guest

    Unhappy

    I need to know how to control the menu system of another application from my application. So far, I have gotten this feedback from Matthew Gates, but it only shows how to get the menu system from an application (in this case, Notepad):

    Code:
    'Code by Aaron Young
    
    Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
    Public 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&
    
    Public 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

    Now I need to know how to control this menu from my application.

    Thanks,

    Simon Bingier

  2. #2
    Guest
    What do you mean by 'control' it?

  3. #3
    Guest
    I know exactly what he means.

    Code:
    Public Declare Function FindWindow Lib "user32" Alias _
    "FindWindowA" (ByVal lpClassName As String, ByVal _
    lpWindowName As String) As Long
    Public Declare Function SendMessageLong& Lib "user32" Alias _
    "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, ByVal lParam 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
    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
    
    Public Const WM_COMMAND = &H111
    
    Private Sub Command1_Click()
    Dim notepad As Long
    notepad = FindWindow("notepad", vbNullString)
    Dim TheWindow As Long
    Dim aMenu As Long
    Dim mCount As Long
    Dim LookFor As Long
    Dim sMenu As Long
    Dim sCount As Long
    Dim LookSub As Long
    Dim sID As Long
    Dim sString As String
    
    TheWindow = notepad
    aMenu& = GetMenu(TheWindow)
    mCount& = GetMenuItemCount(aMenu&)
    For LookFor& = 0& To mCount& - 1
        sMenu& = GetSubMenu(aMenu&, LookFor&)
        sCount& = GetMenuItemCount(sMenu&)
        For LookSub& = 0 To sCount& - 1
            sID& = GetMenuItemID(sMenu&, LookSub&)
            sString$ = String$(100, " ")
            Call GetMenuString(sMenu&, sID&, sString$, 100&, 1&)
            If InStr(LCase(sString$), LCase("&Find...")) Then
                Call SendMessageLong(TheWindow, WM_COMMAND, sID&, 0&)
                Exit Sub
            End If
        Next LookSub&
    Next LookFor&
    End Sub
    Open Notepad and watch what it does.

    [Edited by Matthew Gates on 09-07-2000 at 09:53 PM]

  4. #4
    Guest
    Whoa, that's some pretty advanced code, but it works! Thank you very very much!!

    I'm trying to figure a few things out...

    When the FindWindow API searches for Notepad, how do you know what the name of the program is that the API is supposed to search for.

    In other words, in FindWindow("Notepad" , how do I know what to replace the "Notepad" with in my application. Is this the name of the .exe or is this the name that is in the caption of the application?


    Thanks,

    Simon Bingier

  5. #5
    Guest
    You can find the window by it's caption.

    Code:
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    h = FindWindow(vbNullString, "Untitled - Notepad")
    
    If h <> 0 Then
    MsgBox "Found!": Exit Sub
    Else
    MsgBox "Window was not found!", 16
    End If
    This will find a window by it's class.

    Code:
    notepad = FindWindow("notepad", vbNullString)
    
    If h <> 0 Then
    MsgBox "Found!": Exit Sub
    Else
    MsgBox "Window was not found!", 16
    End If

  6. #6
    Guest

    Smile

    Ok, I'm going to experiment with this for a while...


    Thank you Matthew... you are truly a guru!


    -Simon Bingier

  7. #7
    Guest
    Ok, I have another question now...

    What does the sString$ = String$(100, " ") in the Command1_Click event stand for?

    I don't understand what the 100 and the " " mean.

  8. #8
    Guest
    I just tried the code on Windows calculator and it doesn't work. Then I tried it on Internet Explorer and it doesn't work either. Notepad seems to be the only program that it works on.

  9. #9
    Guest
    I just sent you something, in which you can find window and do exactly as I did.

    Hope that helps.

  10. #10
    Guest
    Ok, thank you

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