如何获取输入框的文本,或焦点所在控件(在另一应用程序上)的句柄?查询历史都显示“无法找到网页”,或者答案是获取的鼠标所在的控件的句柄。但我想要的是光标所在控件的句柄。谢谢!

解决方案 »

  1.   

    '网页的不大好使,一般应用程序的输入框都可以
    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 SubPrivate Sub Timer1_Timer()
      Post
    End Sub
      

  2.   

    首先你要找到另一个应用程序的类名和窗口标题,这个很简单,你一定能找到,可以用(SPY++)找。然后你可以在程序中这么写:
    Dim ParentHwnd,ChildHwnd as longParentHwnd = 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
      

  3.   

    谢谢两位!讲得很详细。因为我用了 yefanqiu(叶帆) 的,所以多得点。其实,分对你们也无所谓吧。