所以,我正在尝试捕获一个有 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
使用名为"hwndChildAfter"的第二个参数来 FindWindowEx():
子窗口的句柄。搜索从下一个孩子开始 窗口按 Z 顺序排列。子窗口必须是直接子窗口 的 hwndParent,而不仅仅是一个后代窗口。
因此,您应该能够传入第一个文本框句柄以获取下一个文本框句柄,如下所示:
txt2 = FindWindowEx(hwnd, txt, "Edit", vbNullString)