代码如下,并加了一些注释
转自http://hi.baidu.com/ok100fen/blog/item/8b825a3a065522ed14cecb67.html
Option Explicit 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 FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String,ByVal lpWindowName As String) 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 Const WM_GETTEXT = &HD ''要想获得其他程序edit控件中的信息,只能通过发送WM_GETTEXT消息才能成功 Private Const WM_GETTEXTLENGTH = &HE '执行这个方法前先用Shell "calc.exe"调用起计算器,先运算上几个数据做为测试 Private Sub Command1_Click() Dim ChildHwnd As Long Dim Phwnd As Long Phwnd = FindWindow(vbNullString,"计算器") '用FindWindow函数得到计算器的句柄 If Phwnd <> 0 Then '在这里已经知道计算器里对那个编辑框的名字为"Edit" '用FindWindowEx得到计算器里Edit的句柄。 ChildHwnd = FindWindowEx(Phwnd,0&,"Edit",vbNullString) Dim str As String * 256 '这里最好设置一个足够大的数 '要想获得其他程序edit控件中的信息,只能通过发送WM_GETTEXT消息才能成功 '用SendMessage向系统发送消息从Edit里取值存到str里。 SendMessage ChildHwnd,WM_GETTEXT,100,ByVal str '100是字符串的长度 str = Trim(str) txtText1.Text = str End If End Sub