在两个屏幕中最大化表单窗口C#

时间:2020-02-08 15:03:41

标签: c#

我有一个想要将其最大化的表格。它与下面的代码完美配合,但是当我将应用程序移动到具有两个屏幕的计算机时,只有一个屏幕被覆盖,而另一个则没有。我想知道是否有办法使两个屏幕都具有完整的窗体?

    private void Form1_Load(object sender, EventArgs e)
    {
        this.ControlBox = false;
        this.TopMost = true;
        this.WindowState = FormWindowState.Maximized;

    }

2 个答案:

答案 0 :(得分:1)

尝试此操作,假设您的屏幕彼此并排放置:

private void Form1_Load(object sender, EventArgs e)
        {
            StartPosition = FormStartPosition.Manual;
            Location = new Point(0, 0);
            var height = Screen.AllScreens.Max(x => x.WorkingArea.Height + x.WorkingArea.Y);
            var width = Screen.AllScreens.Max(x => x.WorkingArea.Width + x.WorkingArea.X);
            Size = new Size(width, height);
        }

答案 1 :(得分:0)

WPF

您可以使用 Extension 这样的方法,并从OnLoaded

调用它
 public static void MaximizeToSecondaryMonitor(this Window window)
        {
            var secondaryScreen = System.Windows.Forms.Screen.AllScreens.Where(s => !s.Primary).FirstOrDefault();

            if (secondaryScreen != null)
            {
                if (!window.IsLoaded)
                    window.WindowStartupLocation = WindowStartupLocation.Manual;

                var workingArea = secondaryScreen.WorkingArea;
                window.Left = workingArea.Left;
                window.Top = workingArea.Top;
                window.Width = workingArea.Width;
                window.Height = workingArea.Height;
                // If window isn't loaded then maxmizing will result in the window displaying on the primary monitor
                if ( window.IsLoaded )
                    window.WindowState = WindowState.Maximized;
            }
        }

WinForms

 private void Form1_Load(object sender, EventArgs e)
 {
        this.StartPosition = FormStartPosition.Manual;
        this.WindowState = FormWindowState.Normal;
        this.FormBorderStyle = FormBorderStyle.None;
        this.Bounds = GetSecondaryScreen().Bounds;
        this.SetBounds(this.Bounds.X , this.Bounds.Y, this.Bounds.Width, this.Bounds.Height);
 }

 private Screen GetSecondaryScreen()
 {
        foreach (Screen screen in Screen.AllScreens)
        {
            if (screen != Screen.PrimaryScreen)
                return screen;
        }
        return Screen.PrimaryScreen;
 }
相关问题