FindWindow FindWindowEx

34.2k views Asked by At

I have written a program that is to find a box in another program and set focus to it. Once this is done it will sendkeys and save to this box.

I am using Findwindow and FindwindowEx to locate the box, but I have a bit of an issue. enter image description here if you notice the windows are the same all the way down to the first TPanel. Now after that there are 3Tpanel Classes. After 3Tpanel Classes there are multiple TttgEdit Classes.

How do I teach the program which Classes I want to select? Here is my code thus far.

Delcare

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long

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

Source

    Dim hWnd As IntPtr = FindWindow("TRunprgForm", Nothing)
    If hWnd.Equals(IntPtr.Zero) Then
        Return
    End If
    cb1.Checked = True
    '--------------------instert here
    Dim hWndChild1 As IntPtr = _
    FindWindowEx(hWnd, IntPtr.Zero, "TmisinvForm", Nothing)
    If hWndChild1.Equals(IntPtr.Zero) Then
        Return
    End If

    Dim hWndChild2 As IntPtr = _
    FindWindowEx(hWndChild1, IntPtr.Zero, "TScrollBox", Nothing)
    If hWndChild2.Equals(IntPtr.Zero) Then
        Return
    End If

    Dim hWndChild3 As IntPtr = _
    FindWindowEx(hWndChild2, IntPtr.Zero, "TPageControl", Nothing)
    If hWndChild3.Equals(IntPtr.Zero) Then
        Return
    End If

    Dim hWndChild4 As IntPtr = _
    FindWindowEx(hWndChild3, IntPtr.Zero, "TTabSheet", Nothing)
    If hWndChild4.Equals(IntPtr.Zero) Then
        Return
    End If

    Dim hWndChild5 As IntPtr = _
    FindWindowEx(hWndChild4, IntPtr.Zero, "TttgCenterPanel", Nothing)
    If hWndChild5.Equals(IntPtr.Zero) Then
        Return
    End If

    Dim hWndChild6 As IntPtr = _
    FindWindowEx(hWndChild5, IntPtr.Zero, "TPanel", Nothing)
    If hWndChild6.Equals(IntPtr.Zero) Then
        Return
    End If

    Dim hWndEdit As IntPtr = _
    FindWindowEx(hWndChild6, IntPtr.Zero, "TttgDBEdit", Nothing)
    If hWndEdit.Equals(IntPtr.Zero) Then
        Return
    End If

    SetForegroundWindow(hWndEdit)

The numbers on the left hand side, hWnd, They change every time the screen is closed and opened, so I cant use them as a static number. Any help would be awesome.

1

There are 1 answers

0
Ove On BEST ANSWER

It looks like you want the second TPanel under the TttgCenterPanel.

In order to do that, you can find the first TPanel (you already did this), and after that, find the TPanel that is a descendant of TttgCenterPanel, and comes after the first TPanel. You need to pass hwndChild5 into the hwndChildAfter of FindWindowEx`.

' .... all the stuff you did before

Dim hWndChild5 As IntPtr = _
FindWindowEx(hWndChild4, IntPtr.Zero, "TttgCenterPanel", Nothing)
If hWndChild5.Equals(IntPtr.Zero) Then
    Return
End If

Dim hWndChild6 As IntPtr = _
FindWindowEx(hWndChild5, IntPtr.Zero, "TPanel", Nothing)
If hWndChild6.Equals(IntPtr.Zero) Then
    Return
End If

Dim hWndChild6Second As IntPtr = _
FindWindowEx(hWndChild5, hWndChild6, "TPanel", Nothing)
If hWndChild6Second.Equals(IntPtr.Zero) Then
    Return
End If

Dim hWndEdit As IntPtr = _
FindWindowEx(hWndChild6Second, IntPtr.Zero, "TttgDBEdit", Nothing)
If hWndEdit.Equals(IntPtr.Zero) Then
    Return
End If

SetForegroundWindow(hWndEdit)

From the MSDN documentation of FindWindowEx:

hwndChildAfter [in, optional]

Type: HWND

A handle to a child window. The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window.

If hwndChildAfter is NULL, the search begins with the first child window of hwndParent.

This approach will work if you are trying to find the second TPanel. If they are in random order each time, this will fail.