Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set window owner to an unmanaged window

I want to set the owner form to that of an unamanged window. I have the unmanaged window's handle. How can I set this unmanaged window to be the owner window for my managed form?

IntPtr hWnd = GetUnmanagedWindow();//assume the handle is returned correctly
Form form = new Form();
form.Show(ConvertToManaged(hWnd));//Need an implementation for ConvertOrSomething()
like image 346
P.Brian.Mackey Avatar asked Dec 06 '25 09:12

P.Brian.Mackey


1 Answers

The standard way to do this is to use the NativeWindow class.

IntPtr hWnd = GetUnmanagedWindow();//assume the handle is returned correctly
Form form = new Form();
NativeWindow nativeWindow = new NativeWindow();
nativeWindow.AssignHandle(hWnd);
form.Show(nativeWindow);

As Hans points out, remember to call ReleaseHandle when are done with it.

like image 141
David Heffernan Avatar answered Dec 08 '25 22:12

David Heffernan