【发布时间】:2009-07-08 01:49:45
【问题描述】:
如何将 System.Windows.Window 设置为 System.Windows.Forms.Form 的所有者?
在我搜索了一段时间后才意识到我已经在我的一个实用程序类中找到了答案,我决定将答案放在 stackoverflow 上。希望有人觉得这很有用。
【问题讨论】:
如何将 System.Windows.Window 设置为 System.Windows.Forms.Form 的所有者?
在我搜索了一段时间后才意识到我已经在我的一个实用程序类中找到了答案,我决定将答案放在 stackoverflow 上。希望有人觉得这很有用。
【问题讨论】:
使用这个方法:
[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());
}
【讨论】:
SetParent 不被认为比SetWindowLong 和GWL_HWDPARENT (-8)“更正确”吗?
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
【讨论】: