如何将System.Windows.Window设置为System.Windows.Forms.Form的所有者?
在我搜索了一段时间后才意识到我已经在我的一个utils类中得到了答案,我决定将答案放在stackoverflow上。希望有人觉得这很有用。
答案 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}} SetWindowLong
与GWL_HWDPARENT
相比,[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
被认为“更正确”吗?
{{1}}