0

I have a form which I wanted it maximized. It worked perfect with the below code, but when I moved the application to a computer with two screens only one screen was covered and the other not. I wonder if there is a way to make both screens have a full form?

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

    }
Tak
  • 3,536
  • 11
  • 51
  • 93
  • Do you mean, the same Form, maximized in both screens? What if the screens have a different display area a size? What would be the size of the maximized Form? You could set the Form's Size to the combined size of the two Displays, remove the Borders... (but if the height of any of the two is different, the Dpi resolution is different...) – Jimi Feb 08 '20 at 15:20
  • @Jimi in my case both screens are identical – Tak Feb 08 '20 at 15:21
  • I mean, what the System/Framework would do? Decide that a Form can be maximized only if two (or more?) Displays are exactly the same (what *the same* could mean)? And if the resolution of one of the Display changes while the Form is maximized, what happens then? Do we show a multi-resolution Form? Do we have that tech available? – Jimi Feb 08 '20 at 15:31
  • @Jimi Yes, I want it maximised to the number of screens connected to the computer, if one screen then it will be maximized to one, if 2 then maximized to 2,...not sure what you mean by the resolution part – Tak Feb 08 '20 at 15:33
  • You can do something like this: `this.Location = new Point(0, 0); this.FormBorderStyle = FormBorderStyle.None; this.Size = new Size(int.MaxValue, int.MaxValue);`. If the left-most Display has coordinates `(0,0)` (some fine-tuning required if the Form has a StatusStrip). Otherwise, see how the VirtualScreen coordinates are defined [here](https://stackoverflow.com/a/53026765/7444103). – Jimi Feb 08 '20 at 15:46
  • @Jimi And I place this `this.Location = new Point(0, 0); this.FormBorderStyle = FormBorderStyle.None; this.Size = new Size(int.MaxValue, int.MaxValue);` in Form1_Load, right? – Tak Feb 08 '20 at 15:55
  • As the last thing in the `Load` event or in the `Shown` event. Both would do. Set the `StartPosition` to something that is not `WindowsDefaultLocation` (in the Designer) – Jimi Feb 08 '20 at 16:00
  • @Jimi What `StartPosition`? I thought to add this `this.Location = new Point(0, 0); this.FormBorderStyle = FormBorderStyle.None; this.Size = new Size(int.MaxValue, int.MaxValue);` in Form1_Load and that's it? – Tak Feb 08 '20 at 16:14
  • `StartPosition` is a property of the Form class. You can see it in the Properties panel in the Form's Designer. It controls pre-defined behaviors in relation to the Form starting position. You usually set it to `Manual` if you're setting the `Form.Location` directly. – Jimi Feb 08 '20 at 16:18
  • @Jimi so Hanan’s answer should work, right? – Tak Feb 08 '20 at 16:19
  • It depends. Read the notes about [Displays disposition and VirtualScreen](https://stackoverflow.com/a/53026765/7444103). Don't skip the part related to the application's DpiAwareness and, as already mentioned, the VirtualScreen's coordinates definition. – Jimi Feb 08 '20 at 16:21
  • Previous answer here: - https://stackoverflow.com/questions/3361211/maximize-form-on-both-screens-dual-screen-monitor – ZedLepplin Feb 17 '20 at 13:27

2 Answers2

1

Try this, assuming your screens are placed next to each other:

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);
        }
Hanan Hazani
  • 113
  • 6
0

WPF

You can use an Extension method like this and call it from 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;
 }
Clint
  • 6,011
  • 1
  • 21
  • 28