【问题标题】:Maximise form window in two screens C#在两个屏幕中最大化表单窗口C#
【发布时间】:2020-02-08 15:03:41
【问题描述】:

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

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

    }

【问题讨论】:

  • 你的意思是,同一个表单,在两个屏幕上都最大化?如果屏幕具有不同的显示区域大小怎么办?最大化表格的大小是多少?您可以将表单的大小设置为两个显示器的组合大小,删除边框...(但如果两者的高度不同,则 Dpi 分辨率不同...)
  • @Jimi 在我的情况下两个屏幕是相同的
  • 我的意思是,系统/框架会做什么?决定只有当两个(或更多?)显示完全相同时才能最大化表单(相同可能意味着什么)?如果窗体最大化时其中一个显示器的分辨率发生变化,那会发生什么?我们是否显示多分辨率表格?我们有这种技术吗?
  • @Jimi 是的,我希望它最大化到连接到计算机的屏幕数量,如果是一个屏幕,那么它将最大化为一个,如果 2 则最大化为 2,...不确定是什么你的意思是分辨率部分
  • 你可以这样做:this.Location = new Point(0, 0); this.FormBorderStyle = FormBorderStyle.None; this.Size = new Size(int.MaxValue, int.MaxValue);。如果最左边的 Display 有坐标 (0,0)(如果 Form 有 StatusStrip,则需要进行一些微调)。否则,请查看 VirtualScreen 坐标是如何定义的 here

标签: c#


【解决方案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);
        }

【讨论】:

    【解决方案2】:

    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;
     }
    

    【讨论】:

    • 参考文献有很多错误,能否提供参考文献?
    • 您使用的是 WPF 还是 WinForms ?
    • 我正在使用 WinForms
    • @Tak,你好,只是想知道答案是否有帮助,让我知道谢谢
    • 我刚试了,不行,只有一个屏幕有表格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 2017-11-24
    相关资源
    最近更新 更多