How to get a HWND handle out of a System.Windows.Forms.Form

丶灬走出姿态 提交于 2019-11-29 06:21:17

问题


Given the form

System.Windows.Forms::Form Form1;

and the window handle

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;

but the compiler complains:

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

So I try

hWnd = this->Handle;

and just

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

and then the compiler says:

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


回答1:


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

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

Works.



来源:https://stackoverflow.com/questions/10621437/how-to-get-a-hwnd-handle-out-of-a-system-windows-forms-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!