Tak
Tak

Reputation: 3616

Maximise form window in two screens C#

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;

    }

Upvotes: 0

Views: 491

Answers (2)

Clint
Clint

Reputation: 6509

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

Upvotes: 0

Hanan Hazani
Hanan Hazani

Reputation: 113

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

Upvotes: 1

Related Questions