【问题标题】:SetWindowPlacement doesn't restore to the same monitor when maximizedSetWindowPlacement 最大化时不会恢复到同一个监视器
【发布时间】:2014-08-21 07:52:26
【问题描述】:

我有一个 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;
    }
}

【问题讨论】:

    标签: c# winforms winapi


    【解决方案1】:

    由于我不知道的原因,我不得不删除为表单设置的默认 WindowStateMaximized,以便让 SetWindowPlacement 将最大化的窗口恢复到正确的监视器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多