PDA

Click to See Complete Forum and Search --> : Gurus, I need help...


Sep 7th, 2000, 07:14 PM
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 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

Sep 7th, 2000, 07:49 PM
What do you mean by 'control' it?

Sep 7th, 2000, 08:02 PM
I know exactly what he means.

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]

Sep 7th, 2000, 08:35 PM
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

Sep 7th, 2000, 08:45 PM
You can find the window by it's caption.

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.


notepad = FindWindow("notepad", vbNullString)

If h <> 0 Then
MsgBox "Found!": Exit Sub
Else
MsgBox "Window was not found!", 16
End If

Sep 7th, 2000, 08:59 PM
Ok, I'm going to experiment with this for a while...


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


-Simon Bingier

Sep 7th, 2000, 09:08 PM
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.

Sep 7th, 2000, 09:19 PM
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.

Sep 7th, 2000, 10:03 PM
I just sent you something, in which you can find window and do exactly as I did.

Hope that helps.

Sep 7th, 2000, 10:11 PM
Ok, thank you