0

所以,我试图捕捉一个有 2 个文本框的窗口,并向这两个文本框发送一些文本。但是两个文本框都没有标题和相同的类名“编辑”。到目前为止,我所能做的就是捕获第一个文本框,仅此而已。

下面粘贴的是我的代码。

Imports System.Runtime.InteropServices
Imports System.Text

Public Class Form1

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

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
     ByVal lParam As String) As Integer

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, _
     ByVal lpsz2 As String) As Integer

    Private Const WM_SETTEXT As Integer = &HC

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As  _
    System.EventArgs) Handles Button1.Click

        Dim hwnd As Integer
        Dim txt As Integer
        Dim text As String

            hwnd = FindWindow(vbNullString, "Description")
            If hwnd <> Nothing Then
                txt = FindWindowEx(hwnd, 0, "Edit", vbNullString)
                If txt <> Nothing Then
                    text = "00000"
                    SendMessage(txt, WM_SETTEXT, 0, text)
                End If
            End If

    End Sub

End Class
4

1 回答 1

5

使用 FindWindowEx() 的第二个参数称为“hwndChildAfter”:

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

因此,您应该能够传递第一个文本框句柄以获取下一个文本框句柄,如下所示:

txt2 = FindWindowEx(hwnd, txt, "Edit", vbNullString)
于 2013-10-25T16:33:04.667 回答