FindWindow FindWindowEx

I wrote a program that should find a field in another program and set focus on it. Once this is done, he will send the keys and save them in this field.

I use Findwindow and FindwindowEx to find the field, but I have a problem. enter image description here if you notice that the windows are the same up to the first TPanel. Now after that there are 3Tpanel classes. After the 3Tpanel classes, there are several TttgEdit classes.

How can I teach the program which classes I want to choose? Here is my code so 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 

A 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 are hWnd. They change every time the screen is closed and open, so I cannot use them as a static number. Any help would be awesome.

+1
source share
1 answer

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

To do this, you can find the first TPanel (you already did it), and then find the TPanel, which is a descendant of the TttgCenterPanel , and appears after the first TPanel. You need to pass hwndChild5 to 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 for FindWindowEx :

hwndChildAfter [in, optional]

Type: HWND

Handle to the child window. The search starts from the next child window in order Z. The child window should be the direct child window of hwndParent, and not just the child.

If hwndChildAfter is NULL, the search starts from the first child window of hwndParent.

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

+2
source

Source: https://habr.com/ru/post/1259764/


All Articles