vb.net FindWindow FindWindowEx

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14324403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:34:33  来源:igfitidea点击:

FindWindow FindWindowEx

vb.netfindwindowfindwindowex

提问by user1946138

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 hereif 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.

我正在使用 Findwindow 和 FindwindowEx 来定位该框,但我遇到了一些问题。 在此处输入图片说明如果您注意到窗口一直到第一个 TPanel 都是一样的。现在之后有 3Tpanel 类。在 3Tpanel Classes 之后有多个 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.

左侧的数字 hWnd 每次关闭和打开屏幕时都会发生变化,因此我不能将它们用作静态数字。任何帮助都是极好的。

采纳答案by Ove

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

看起来您想要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 hwndChild5into the hwndChildAfter ofFindWindowEx`.

为了做到这一点,您可以找到第一个 TPanel(您已经这样做了),然后找到作为 的后代的 TPanel TttgCenterPanel,并且出现在第一个 TPanel 之后。你需要传递hwndChild5hwndChildAfter ofFindWindowEx`。

' .... 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:

FindWindowExMSDN 文档

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.

hwndChildAfter [输入,可选]

类型:HWND

子窗口的句柄。搜索从 Z 顺序中的下一个子窗口开始。子窗口必须是 hwndParent 的直接子窗口,而不仅仅是后代窗口。

如果 hwndChildAfter 为 NULL,则搜索从 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.

如果您尝试查找第二个 TPanel,此方法将有效。如果它们每次都是随机顺序,这将失败。