如何从System.Windows.Forms.Form中获取HWND句柄 [英] How to get a HWND handle out of a System.Windows.Forms.Form

查看:310
本文介绍了如何从System.Windows.Forms.Form中获取HWND句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出表格

System.Windows.Forms::Form Form1;

和窗口句柄

HWND hWnd;

如何根据

How can I set hWnd to the Handle property of Form1 that does truly exist as a public property that "Gets the window handle that the control is bound to. (Inherited from Control.)" according to the Microsoft documentation of System.Windows.Forms::Form? In the constructor of my Form Form1, I've tried

hWnd = this.Handle;

但是编译器抱怨:

错误C2228:'.Handle'的左侧必须具有class/struct/union类型为'MyNamespace :: Form1 ^ const',您打算改用'->'吗?

error C2228: left of '.Handle' must have class/struct/union type is 'MyNamespace::Form1 ^const ' did you intend to use '->' instead?

所以我尝试

hWnd = this->Handle;

还有

hWnd = Handle; // Since I'm in the Form

然后编译器说:

错误C2440:'=':无法从'System :: IntPtr'转换为'HWND'没有可用的用户定义转换运算符可以执行此转换,或者无法调用该运算符

error C2440: '=' : cannot convert from 'System::IntPtr' to 'HWND' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

推荐答案

我找到了一个解决方案,并且不在乎它是否太复杂了.

I found a solution, and don't care if it's a kludge.

hWnd = static_cast<HWND>(Handle.ToPointer());

工作.

这篇关于如何从System.Windows.Forms.Form中获取HWND句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆