如何将窗口所有者设置为非托管窗口

P.B*_*key 1 c# pinvoke winforms

我想将所有者表单设置为无人窗口的表单.我有非托管窗口的句柄.如何将此非托管窗口设置为托管表单的所有者窗口?

IntPtr hWnd = GetUnmanagedWindow();//assume the handle is returned correctly
Form form = new Form();
form.Show(ConvertToManaged(hWnd));//Need an implementation for ConvertOrSomething()
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 6

执行此操作的标准方法是使用NativeWindow该类.

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

正如汉斯指出的那样,记得ReleaseHandle在完成任务时打电话.