Use FindWindowEx() to find Controls?

  • Thread starter Thread starter Lars Netzel
  • Start date Start date
L

Lars Netzel

Do I use FindWindowEx() to find control ( a button.. ) within a Window or
is that only to find child Windows after using FindWindows()

best regards
/Lars
 
Lars Netzel said:
Do I use FindWindowEx() to find control ( a button.. ) within a Window or
is that only to find child Windows after using FindWindows()

Yes, 'FindWindowEx' is designed for this purpose. Buttons that are placed
on forms are child windows.
 
Thank you.. then.. how come this does not work? my FSM window have a button
with the caption "Button1"


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
lpClassName As String, ByVal lpWindowName As String) As Integer

Public Declare Function FindWindowEx Lib "user32" (ByVal hWnd1 As Long,
ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long


hWnd = FindWindow(vbNullString, "FSM")
MsgBox("FSM: " & hWnd)

MsgBox("Button1: " & FindWindowEx(hWnd, 0&, "Button1", vbNullString))

It finds the window fine.. but not the button... is the declaration okay?

Best regards
/Lars
 
Lars Netzel said:
Thank you.. then.. how come this does not work? my FSM window have a
button with the caption "Button1"


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

Public Declare Function FindWindowEx Lib "user32" (ByVal hWnd1 As Long,
ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
[...]
It finds the window fine.. but not the button... is the declaration okay?

Use these declarations and the code below (untested):

\\\
Private Declare Auto Function FindWindow Lib "user32.dll" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As IntPtr

Private Declare Auto Function FindWindowEx Lib "user32.dll" ( _
ByVal hwndParent As IntPtr, _
ByVal hwndChildAfter As IntPtr, _
ByVal lpszClass As String, _
ByVal lpszWindow As String _
) As IntPtr
..
..
..
Dim hWnd As IntPtr = FindWindow(vbNullString, "FSM")
If hWnd.Equals(IntPtr.Zero) Then
Return
End If
Dim hWndButton As IntPtr = _
FindWindowEx(hWnd, IntPtr.Zero, "BUTTON", "Button 1")
If hWndButton.Equals(IntPtr.Zero) Then
Return
End If
....
///

Note that "BUTTON" must be replaced by the Win32 window class name of the
control. This is typically "BUTTON" for C-based applications, but the class
name can differ for applications written in Classic Visual Basic and .NET.
You can use the Spy++ utility which comes with VS.NET to grab the control's
class name. The last parameter of 'FindWindowEx' expects the button's
caption. Make sure you don't forget the leading "&" character if the
caption contains an accelerator key.
 
Hi Lars,
It finds the window fine.. but not the button... is the declaration
okay?

Nope, it isn't.

You better try this instead:

~
Private Declare Auto Function FindWindow Lib "user32.dll" ( _
<MarshalAs(UnmanagedType.LPTStr), [In]()> ByVal lpClassName As
String, _
<MarshalAs(UnmanagedType.LPTStr), [In]()> ByVal lpWindowName As
String _
) As IntPtr

Private Declare Auto Function FindWindowEx Lib "user32.dll" ( _
ByVal hWndParent As IntPtr, _
ByVal hWndChildAfter As IntPtr, _
<MarshalAs(UnmanagedType.LPTStr), [In]()> ByVal lpszClass As
String, _
<MarshalAs(UnmanagedType.LPTStr), [In]()> ByVal lpszWindow As
String _
) As IntPtr
~

Roman
 
Thank you, that worked!:) Just that little Auto word missing... what does
that mean?


Herfried K. Wagner said:
Lars Netzel said:
Thank you.. then.. how come this does not work? my FSM window have a
button with the caption "Button1"


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

Public Declare Function FindWindowEx Lib "user32" (ByVal hWnd1 As Long,
ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As
Long
[...]
It finds the window fine.. but not the button... is the declaration
okay?

Use these declarations and the code below (untested):

\\\
Private Declare Auto Function FindWindow Lib "user32.dll" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As IntPtr

Private Declare Auto Function FindWindowEx Lib "user32.dll" ( _
ByVal hwndParent As IntPtr, _
ByVal hwndChildAfter As IntPtr, _
ByVal lpszClass As String, _
ByVal lpszWindow As String _
) As IntPtr
.
.
.
Dim hWnd As IntPtr = FindWindow(vbNullString, "FSM")
If hWnd.Equals(IntPtr.Zero) Then
Return
End If
Dim hWndButton As IntPtr = _
FindWindowEx(hWnd, IntPtr.Zero, "BUTTON", "Button 1")
If hWndButton.Equals(IntPtr.Zero) Then
Return
End If
...
///

Note that "BUTTON" must be replaced by the Win32 window class name of the
control. This is typically "BUTTON" for C-based applications, but the
class name can differ for applications written in Classic Visual Basic and
.NET. You can use the Spy++ utility which comes with VS.NET to grab the
control's class name. The last parameter of 'FindWindowEx' expects the
button's caption. Make sure you don't forget the leading "&" character if
the caption contains an accelerator key.
 
Lars Netzel said:
Thank you, that worked!:) Just that little Auto word missing... what does
that mean?
It lets the runtime decide whether the strings are passed as Unicode or
ANSI. On Win9X platforms they are passed as ANSI; on all other Windows
platforms they are passed as Unicode

Hope this helps,

Nick Hall
 
Nick Hall said:
It lets the runtime decide whether the strings are passed as Unicode or
ANSI. On Win9X platforms they are passed as ANSI; on all other Windows
platforms they are passed as Unicode

BTW: The documentation for the 'Declare' statement contains an explanation
too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top