如果通过VB读取另一个应用程序窗口显示的信息
我想通过VB编写一个程序,目的是读取另一个应用程序窗口各部分显示的信息。因为应用程序没有提供数据接口,也没有导出功能,目前都是通过手工抄录的方式获取上面的数据,这样太麻烦...
我想通过VB编写一个程序,目的是读取另一个应用程序窗口各部分显示的信息。因为应用程序没有提供数据接口,也没有导出功能,目前都是通过手工抄录的方式获取上面的数据,这样太麻烦了。哪位高手能给我提供一点思路,谢谢!
展开
2个回答
展开全部
给你个例子,用VB获取记事本的文本框内容,要求记事本的窗口标题是“无标题 - 记事本”,不是的话要改下代码。
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Sub Command1_Click()
Dim lHwnd As Long
Dim lChildHwnd As Long
Dim strRet As String
Dim n As Long
Dim bArr() As Byte, bArr2() As Byte
lHwnd = FindWindow(vbNullString, "无标题 - 记事本")
If lHwnd > 0 Then
lChildHwnd = FindWindowEx(lHwnd, 0&, "Edit", vbNullString)
If lChildHwnd > 0 Then
' 获取文本长度
n = SendMessage(lChildHwnd, WM_GETTEXTLENGTH, 0, 0)
If n > 0 Then
' 设置缓冲区
ReDim bArr(n + 1) As Byte
ReDim bArr2(n - 1) As Byte
Call RtlMoveMemory(bArr(0), n, 2)
'发送 WM_GETTEXT 消息
Call SendMessage(lChildHwnd, WM_GETTEXT, n + 1, bArr(0))
Call RtlMoveMemory(bArr2(0), bArr(0), n)
' 得到文本
strRet = StrConv(bArr2, vbUnicode)
MsgBox strRet
End If
End If
End If
End Sub
其中记事本的文本框的类名是"Edit",如果不知道你想获取的第三方程序界面里面,控件的类名,可以通过SPY++获取,如图所示,把十字靶拖到控件上面,可以显示类名
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询