如何获取输入框的文本,或焦点所在控件(在另一应用程序上)的句柄?查询历史都显示“无法找到网页”

youok99 2003-09-07 04:50:19
如何获取输入框的文本,或焦点所在控件(在另一应用程序上)的句柄?查询历史都显示“无法找到网页”,或者答案是获取的鼠标所在的控件的句柄。但我想要的是光标所在控件的句柄。谢谢!
...全文
248 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
youok99 2003-09-09
  • 打赏
  • 举报
回复
谢谢两位!讲得很详细。因为我用了 yefanqiu(叶帆) 的,所以多得点。其实,分对你们也无所谓吧。
Shikari 2003-09-08
  • 打赏
  • 举报
回复
首先你要找到另一个应用程序的类名和窗口标题,这个很简单,你一定能找到,可以用(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
刘洪峰AIoT 2003-09-07
  • 打赏
  • 举报
回复
'网页的不大好使,一般应用程序的输入框都可以
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



mlngThrdID = GetCurrentThreadId() '获得当前窗体的线程ID

lngThrdID = GetWindowThreadProcessId(hwnd, vbNull) '获得活动窗体的线程ID

n = AttachThreadInput(lngThrdID, mlngThrdID, True) '附加线程

mlngHwnd = GetFocus '获得光标所在的窗体句柄


If mlngHwnd <> 0 Then
Text1 = Str(hwnd) + "|" + Str(mlngHwnd) + "|" + Str(GetWindowLong(mlngHwnd, GWL_ID))
Text2 = Left(strClassName, InStr(strClassName, Chr(0)) - 1) + Str(re.Right - re.Left) + Str(re.Bottom - re.Top)


Else
Text1 = ""
Text2 = ""
End If

n = AttachThreadInput(lngThrdID, mlngThrdID, False) '取消附加线程

End Sub

Private Sub Timer1_Timer()
Post
End Sub
youok99 2003-09-07
  • 打赏
  • 举报
回复
我并不是要破文本框的密码,只是要监控其他应用程序在文本框输入的供应商编码,所以不能用鼠标的方法获得。

1,488

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧