いくつかのページを偶然見つけた後、外部アプリケーションのテキストボックスからコンテンツを取得する方法を見つけました。このコードは他の人にも役立つと思うので、結果は次のとおりです。
Module modApi
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function SendMessageTimeoutString Lib "user32.dll" Alias "SendMessageTimeoutA" (ByVal hwnd As IntPtr, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As String, ByVal fuFlags As Long, ByVal uTimeout As Long, ByVal lpdwResult As Long) As Long
Friend Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal funcCallBack As funcCallBackChild, ByVal lParam As IntPtr) As Boolean
Public Delegate Function funcCallBackChild(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Public TextBoxStrings As ArrayList = New ArrayList
Public Sub ParseWindowControls(ByVal WindowTitle As String)
Dim hWnd As IntPtr = FindWindow(vbNullString, WindowTitle)
TextBoxStrings.Clear()
If hWnd Then
Dim MyCallBack As New funcCallBackChild(AddressOf EnumChildWindowsProc)
EnumChildWindows(hWnd, MyCallBack, IntPtr.Zero)
Else
MsgBox("Could not find window!", vbOKOnly + vbExclamation, "Error")
End If
End Sub
Private Function EnumChildWindowsProc(ByVal hWndParent As IntPtr, ByVal lParam As IntPtr) As Boolean
Dim Buffer As String = Space(256)
Dim Retval As Long = GetClassName(hWndParent, Buffer, Len(Buffer))
If Left(Buffer, Retval) = "WindowsForms10.EDIT.app.0.218f99c" Then
TextBoxStrings.Add(GetText(hWndParent))
End If
EnumChildWindowsProc = True
End Function
Private Function GetText(ByVal hwnd As IntPtr) As String
Dim sText As String = Space(1024)
If SendMessageTimeoutString(hwnd, &HD, 1024, sText, &H2, 1000, 0) <> 0 Then
GetText = Left(sText, InStr(sText, vbNullChar) - 1)
Exit Function
End If
GetText = ""
End Function
End Module
サブにウィンドウタイトルを渡すことができますParseWindowControls()
。潜水艦は要求されたウィンドウを見つけようとします。ウィンドウが見つかると、このアプリケーションで見つかったすべてのコントロールの収集が開始されます。コールバックは、見つかったコントロールが私の仕様(テキストボックス)を満たしているかどうかを調べ、テキストを配列リストに格納します。後で、リクエセットテキストボックスのインデックスを知って、それを配列リストから取得する必要があります。それでおしまい。これが他の人にも役立つことを願っています。