Question

How do I convert a handle acquired from a form/control's Handle property, to a IWin32Window^ ?

Was it helpful?

Solution

Control.FromHandle

(That gets you the Control object, which implements the IWin32Window interface.)

Eg.

IntPtr myWindowHandle = IntPtr(someVal);
IWin32Window^ w = Control::FromHandle(myWindowHandle);

Note that this relies on the handle being "acquired from a form/control's Handle property." You cannot use this technique with an arbitrary Win32 window handle.

OTHER TIPS

There's a simpler method that is supported directly by the .NET framework without having to create your own custom class. You can use this with any arbitrary Window handle.

Given ptrWindowHandle of type IntPtr:

using System.Windows.Forms;

NativeWindow nativeWindow = new NativeWindow();
nativeWindow.AssignHandle(ptrWindowHandle);

System.Windows.Forms.NativeWindow implements the IWin32Window interface.

This appears to be exactly what you are asking for. I've never done it myself, but it appears to be relatively straightforward:

Creating a IWin32Window from a Win32 Handle

Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top