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
The Win32 API provides no direct method for obtaining the window handle associated with a console application. However, you can obtain the window handle by calling FindWindow() . This function retrieves a window handle based on a class name or window name. Call GetConsoleTitle() to determine the current console title.
This is typically referring to an operating system Handle, and used internally. For example, Windows Forms uses an IntPtr to refer to the Control's native Window Handle (HWND).
HWND is a "handle to a window" and is part of the Win32 API . HWNDs are essentially pointers (IntPtr) with values that make them (sort of) point to a window-structure data.
I found a solution, and don't care if it's a kludge.
hWnd = static_cast<HWND>(Handle.ToPointer());
Works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With