首先你要找到另一个应用程序的类名和窗口标题,这个很简单,你一定能找到,可以用(SPY++)找。然后你可以在程序中这么写:
Dim ParentHwnd,ChildHwnd as long
ParentHwnd = FindWindow("窗口类名","窗口标题")
if ParentHwnd <> 0 then
ChildHwnd = FindWindowEx(ParentHwnd,0,"输入框类名",vbNullString)
if ChildHwnd <> 0 then
Dim MyText as string * 255
GetWindowText ChildHwnd,MyText,255
MsgBox MyText
;如果GETWINDOWTEXT没有取到文字,那么可以用SENDMESSAGE WM_GETTEXT
end if
end if
'网页的不大好使,一般应用程序的输入框都可以
Option Explicit
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetFocus Lib "user32" () As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_ID = (-12)
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Sub Post()
Dim hwnd As Long '活动窗体的句柄
Dim lngThrdID As Long '活动窗体的线程ID
Dim lngTxtLen As Long '获取的文本长度
Dim n As Long
Dim strclsName As String * 30 '窗口的类名称
Dim buf As String
Dim mlngThrdID As Long
Dim mlngHwnd As Long
Dim re As RECT
Dim strClassName As String
hwnd = GetForegroundWindow() '获得活动窗体的句柄
If hwnd = Me.hwnd Or hwnd = 0 Then Exit Sub '如果是自己则退出
GetWindowRect hwnd, re
strClassName = Space(255)
GetClassName hwnd, strClassName, 255
' If Left(strClassName, InStr(strClassName, Chr(0)) - 1) = "#32770" Then
'
' End If
'
' If re.Right - re.Left=353 And re.Bottom - re.Top=249 Then
'
' End If