1

经过长时间的搜索和尝试,我终于成功地将所有屏幕从第二屏幕移动到主屏幕。

[DllImport("user32.dll", SetLastError = true)]
    internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
    [DllImport("user32.dll")]
    public static extern bool GetWindowRect(IntPtr hwnd, ref Rectangle rectangle);

private void putAllWindowsOnPrimairyScreen(){

int procesWidth, procesHeight;

    foreach (Process p in Process.GetProcesses())
    {
       IntPtr id = p.MainWindowHandle;
       Rectangle rect = new Rectangle();
       GetWindowRect(id, ref rect);
       if (!string.IsNullOrEmpty(p.MainWindowTitle))
       {
          if (rect.Height > Screen.PrimaryScreen.Bounds.Height) procesHeight = Screen.PrimaryScreen.Bounds.Height;
          else procesHeight = rect.Height;
          if (rect.Width > Screen.PrimaryScreen.Bounds.Width) procesWidth = Screen.PrimaryScreen.Bounds.Width;
          else procesWidth = rect.Width;
          System.Console.WriteLine(p.MainModule.FileVersionInfo.ProductName);
          MoveWindow(id, 0, 0, procesWidth, procesHeight, true);
        }
    }
}

但是,屏幕,在我的情况下是 Firefox,不会停留在主屏幕......我可以打开它一次,第二次尝试,它恢复到第二个屏幕。

我怎样才能让它留在主屏幕上?

提前致谢!

4

1 回答 1

0

经过长时间的搜索、跟踪和错误,我通过编辑代码解决了这个问题:

[DllImport("user32.dll")]
        private static extern bool GetWindowRect(IntPtr hwnd, ref Rectangle rectangle);
[DllImport("user32.dll", SetLastError = true)]
            internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
            public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
    public const uint WM_SYSCOMMAND = 0x0112;
    public const int SC_MINIMIZE = 0xF020;
    public const int SC_NEXTWINDOW = 0xF040;
    public const int SW_FORCEMINIMIZE = 11;
    public const int SW_HIDE = 0;
    public const int SW_MAXIMIZE = 3;
    public const int SW_MINIMIZE = 6;
    public const int SW_RESTORE = 9;
    public const int SW_SHOW = 5;
    public const int SW_SHOWDEFAULT = 10;
    public const int SW_SHOWMAXIMIZED = 3;
    public const int SW_SHOWMINIMIZED = 2;
    public const int SW_SHOWMINNOCATIVE = 7;
    public const int SW_SHOWNA = 8;
    public const int SW_SHOWNOACTIVATE = 4;
    public const int SW_SHOWNORMAL = 1;

    [StructLayout(LayoutKind.Sequential)]
    public struct WINDOWRECTANGLE
    {
        public int Left;        // x position of upper-left corner
        public int Top;         // y position of upper-left corner
        public int Right;       // x position of lower-right corner
        public int Bottom;      // y position of lower-right corner
    }

    /// <summary>
    /// Puts all the active screens on the primairy screen.
    /// </summary>
    public static void putAllWindowsOnPrimairyScreen()
    {
        foreach (Process p in Process.GetProcesses())
        {
            int procesWidth, procesHeigth, defaultProcesWidth, defaultProcesHeigth;
            IntPtr id = p.MainWindowHandle;
            if (!string.IsNullOrEmpty(p.MainWindowTitle))
            {
                WINDOWRECTANGLE rect = new WINDOWRECTANGLE();
                WINDOWRECTANGLE defaultrect = new WINDOWRECTANGLE();
                GetWindowRect(id, ref rect);
                ShowWindow(id, SW_SHOWNOACTIVATE);
                GetWindowRect(id, ref defaultrect);
                if (rect.Left > Screen.PrimaryScreen.Bounds.Width || rect.Left < 0 || rect.Right > Screen.PrimaryScreen.Bounds.Width || rect.Right < 0 || rect.Top > Screen.PrimaryScreen.Bounds.Height || rect.Top < 0 || rect.Bottom > Screen.PrimaryScreen.Bounds.Height || rect.Bottom < 0)
                {
                    procesWidth = rect.Right - rect.Left;
                    procesHeigth = rect.Bottom - rect.Top;
                    defaultProcesWidth = defaultrect.Right - defaultrect.Left;
                    defaultProcesHeigth = defaultrect.Bottom - defaultrect.Top;
                    PostMessage(p.Handle, WM_SYSCOMMAND, SC_NEXTWINDOW, 0);
                    MoveWindow(id, 0, 0, defaultProcesWidth, defaultProcesHeigth, true);
                    p.Refresh();
                    if (procesHeigth >= Screen.PrimaryScreen.Bounds.Height || procesWidth >= Screen.PrimaryScreen.Bounds.Width) ShowWindow(id, SW_MAXIMIZE);
                }
            }
        }
    }

因此,可以将外部窗口强制到另一个屏幕并让它停留在那里。复制并粘贴此代码完成后,只需激活该方法putAllWindowsOnPrimairyScreen()

于 2013-01-23T10:18:18.697 回答