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

时间:2009-07-08 01:49:45

标签: c# wpf winforms

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

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

2 个答案:

答案 0 :(得分:12)

使用此方法:

[DllImport("user32.dll")]

private static extern int SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);

/// <summary>
/// sets the owner of a System.Windows.Forms.Form to a System.Windows.Window
/// </summary>
/// <param name="form"></param>
/// <param name="owner"></param>
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());
}

答案 1 :(得分:7)

SetParent {-1}} SetWindowLongGWL_HWDPARENT相比,[DllImport("user32.dll", SetLastError = true)] static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); 被认为“更正确”吗?

{{1}}