如何将Wpf窗口设置为Winforms表单的所有者

如何将System.Windows.Window设置为System.Windows.Forms.Form的所有者?

在我搜索了一段时间后才意识到我已经在我的一个utils类中得到了答案,我决定将答案放在stackoverflow上。 希望有人发现这很有用。

是否SetParent被认为比使用GWL_HWDPARENT (-8)的SetWindowLong “更正确”?

 [DllImport("user32.dll", SetLastError = true)] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 

使用此方法:

 [DllImport("user32.dll")] private static extern int SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong); ///  /// sets the owner of a System.Windows.Forms.Form to a System.Windows.Window ///  ///  ///  public static void SetOwner(System.Windows.Forms.Form form, System.Windows.Window owner) { WindowInteropHelper helper = new WindowInteropHelper(owner); SetWindowLong(new HandleRef(form, form.Handle), -8, helper.Handle.ToInt32()); }