我有一个Windows窗体应用程序,它使用以下代码保持工具窗口的位置:
ToolWindow = new ToolViewer(TransformedResult);
ToolWindow.FormClosed += (_1, _2) =>
{
this.ToolWindowPlacement = WindowPlacement.GetPlacement(ToolWindow.Handle);
this.ToolWindow = null;
};
ToolWindow.Show();
WindowPlacement.SetPlacement(ToolWindow.Handle, ToolWindowPlacement);
除非工具窗口最大化,否则它可以正常工作。如果它最大化,窗口会一直显示在我的主显示器上,而不是在关闭时显示最大化的窗口。
WindowPlacement.cs:
// from http://blogs.msdn.com/b/davidrickard/archive/2010/03/09/saving-window-size-and-location-in-wpf-and-winforms.aspx
static class WindowPlacement
{
[DllImport("user32.dll")]
private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll")]
private static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
public static void SetPlacement(IntPtr windowHandle, WINDOWPLACEMENT placement)
{
if (placement.length == 0)
return;
placement.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
placement.flags = 0;
placement.showCmd = (placement.showCmd == SW_SHOWMINIMIZED ? SW_SHOWNORMAL : placement.showCmd);
SetWindowPlacement(windowHandle, ref placement);
}
public static WINDOWPLACEMENT GetPlacement(IntPtr windowHandle)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
GetWindowPlacement(windowHandle, out placement);
return placement;
}
}
答案 0 :(得分:2)
出于我所知的原因,我必须删除为表单设置的WindowState
的默认Maximized
,以便SetWindowPlacement
将最大化的窗口恢复到正确的监视器。