Search code examples
c#monitormaximizemultiple-monitorsborderless

Maximizing borderless form with multiple monitors c#


I'm currently making a borderless form with a Doubleclick event to maximize form. But I realized that the form wouldn't maximize on the two other screens, only my main middle. So my code is currently:

private void Form1_DoubleClick(object sender, EventArgs e)
{
    if ((this.Height == Screen.PrimaryScreen.WorkingArea.Height) && (this.Width == Screen.PrimaryScreen.WorkingArea.Width))
    {
        this.Width = 534;
        this.Height = 600;
        CenterToScreen();
    }
    else
    {
        this.Height = Screen.PrimaryScreen.WorkingArea.Height;
        this.Width = Screen.PrimaryScreen.WorkingArea.Width;
        this.Location = Screen.PrimaryScreen.WorkingArea.Location;
    }  
}

It might look weird, but I use it to not cover the taskbar. I need a code like this to dock it to the side, and use it to calculate where the form should be. Looking like this: half right screen dock when I click one of those 9 buttons, it will dock the screen in different places of the screen. In corner, half of the screen or in the middle.

I tried using a code where the form would detect which screen it was on, and using that again to maximize the form on that screen, but I got a bunch of red lines, and it didn't work in the end.

I have 3 monitors.

Please help.


Solution

  • You hardcoded it to primary screen, which is the screen with the task bar. To allow other screens get the screen the form is currently on an adjust to that.

    private void Form1_DoubleClick(object sender, EventArgs e)
    {
        if ((this.Height == Screen.FromControl(this).WorkingArea.Height) && (this.Width == Screen.FromControl(this).WorkingArea.Width))
        {
            this.Width = 534;
            this.Height = 600;
            CenterToScreen();
        }
        else
        {
            this.Height = Screen.FromControl(this).WorkingArea.Height;
            this.Width = Screen.FromControl(this).WorkingArea.Width;
            this.Location = Screen.FromControl(this).WorkingArea.Location;
        }  
    }