如何从System.Windows.Forms.Form中获取HWND句柄 [英] How to get a HWND handle out of a System.Windows.Forms.Form
问题描述
给出表格
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屋!