How to set window owner to unmanaged window

I want to set the owner form to the window without changes. I have an unmanaged window handle. How can I configure this unmanaged window as the owner window of 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() 
+1
source share
2 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, be sure to call ReleaseHandle when you're done with it.

+6
source
 public ManagedWindow ConvertToManaged(IntPtr hWnd) { return new ManagedWindow(hWnd); } public class ManagedWindow : IWin32Window { IntPtr _handle; public IntPtr Handle { get { return _handle; } } public ManagedWindow(IntPtr handle) { _handle = handle; } } 
+2
source

Source: https://habr.com/ru/post/926981/


All Articles