2

我开发了一个应用程序,我可以将程序分配给特定的监视器,这样当我停靠我的笔记本电脑时,我的所有打开的窗口都会被推送到我的主监视器上,我可以按下全局热键Alt+d以及所有已被分配给监视器被推送到他们分配的监视器。我还有 3 个其他热键Alt+ 1Alt+2Alt+ 3,它们将焦点窗口移动到相应的监视器。我注意到当我回到我的办公桌并在我的笔记本电脑处于睡眠状态时对接它然后重新登录到 Windows 时,代码中返回的监视器工作区域与显示设置中显示的内容不匹配。

这是 Windows 显示的监视器布局。红色是我的代码显示的内容。

监控分配

如果我取消停靠然后停靠所有匹配项。

这是我将当前聚焦的窗口移动到特定监视器的代码。
Alt+1发送 0,Alt+2发送 1,Alt+3发送 2:

[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);      

[Flags]
public enum SpecialWindowHandles
{
    /// <summary>
    ///     Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.
    /// </summary>
    HWND_BOTTOM = 1            
}
[Flags]
public enum SetWindowPosFlags : uint
{
    /// <summary>
    ///     Retains the current size (ignores the cx and cy parameters).
    /// </summary>
    SWP_NOSIZE = 0x0001,

    /// <summary>
    ///     Retains the current Z order (ignores the hWndInsertAfter parameter).
    /// </summary>
    SWP_NOZORDER = 0x0004
}

private struct WINDOWPLACEMENT
{
    public int length;
    public int flags;
    public int showCmd;
    public System.Drawing.Point ptMinPosition;
    public System.Drawing.Point ptMaxPosition;
    public System.Drawing.Rectangle rcNormalPosition;
}

/// <summary>
/// Moves the user process retaining start to the provided monitor
/// </summary>
/// <param name="monitor"></param>
public void DockWindow(int monitor)
{
    var screens = Screen.AllScreens.Count();
    if (monitor == 1 && screens < 2 || monitor == 2 && screens < 3) // Prevent moving widnow to monitor that doesn't exist
        return;
    var hwnd = GetForegroundWindow(); // Gets the handle for the focused window
    var screenLocation = Screen.AllScreens[monitor].WorkingArea; // Gets the working area of the windows associated monitor
    var placement = new WINDOWPLACEMENT();
    GetWindowPlacement(hwnd, ref placement); // Gest window placement info
    switch (placement.showCmd)
    {
        case 3: // Maximized
            ShowWindow(hwnd, 9); // Switch to regular window
            SetWindowPos(hwnd, (IntPtr)SpecialWindowHandles.HWND_BOTTOM, screenLocation.X + 100, screenLocation.Y + 100, 0, 0, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOSIZE); // Move window
            ShowWindow(hwnd, 3); // Maximize window
            break;
        default: // Regular window
            SetWindowPos(hwnd, (IntPtr)SpecialWindowHandles.HWND_BOTTOM, screenLocation.X + 100, screenLocation.Y + 100, 0 ,0, SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOSIZE); // Move window
            break;
    }
}

更新
我做了更多的调试,并Screen.AllScreens根据我的计算机在对接时的状态返回不同的数组:

看一下这个。

任何想法为什么会发生这种情况?

4

0 回答 0