tags:

views:

668

answers:

2

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

+2  A: 

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!

Morinar
+3  A: 

Control.FromHandle

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

Eg.

IntPtr myWindowHandle = new 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.

James D
James, I edited a qualification into your answer because people coming here from a more general search on `IntPtr` and `IWin32Window` might only read the question title and not appreciate the constraint in the question detail. Hope this is okay.
itowlson
No, it's a good edit. You probably saved some hapless future programmer from hours of head-scratching wondering why he can't magically create a Control/IWin32Window object from a raw HWND. :)
James D