1

A question has been asked here but none of the solutions provided worked with .NET 4.0 / Win7/8.1. The answer revolves around not setting MaximizeBox to false but the following snippet shows it doesn't work (i.e. the form covers the entire screen anyway (tested on Win7 and 8.1 with ClassicShell). I need this to work across multiple screens and setting MaximumSize doesn't work very well: when the form isn't maximized, user should be allowed to have the form width spanning two monitors. There's also no BeforeMaximize event to hook into. So you can't simply set MaximumSize in the OnMove event.

public TestForm()
{
    InitializeComponent();

    FormBorderStyle = FormBorderStyle.None;
    WindowState = FormWindowState.Maximized;
}

VS2013 seems to be able to do just that without covering the taskbar.

EDIT: Setting MaximizedBounds (as answered by Hans Passant) doesn't work in secondary screen where the secondary screen is larger than the primary. (note: the following is a modified version of Hans' answer as his simply did not work in secondary screen)

e.g.

protected override void OnLocationChanged(EventArgs e)
{
    var workArea = Screen.FromControl(this).WorkingArea;
    MaximizedBounds = new Rectangle(0, 0, workArea.Width, workArea.Height);
    Debug.WriteLine(MaximizedBounds);

    base.OnLocationChanged(e);
}

// Button click event (hit when form maximized):
WinApi.RECT rect;
WinApi.GetWindowRect(Handle, out rect);
Debug.WriteLine(rect);

Output:

OnLocationChanged: {X=0,Y=0,Width=1920,Height=1040}

ButtonClick when form is maximized: {Left=1366,Top=-216,Right=3840,Bottom=876}

This works out to be:

Maximized Width = 3840 - 1366 = 2474

Maximized Height = 876 + 216 = 1092

Where did the framework get those numbers from?

Community
  • 1
  • 1
Zach Saw
  • 4,308
  • 3
  • 33
  • 49

6 Answers6

4

Only add this to OnResize event of the form

this.MaximizedBounds = Screen.GetWorkingArea(this);
2

If your borderless window cover the taskbar you can do it:

this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Maximized;
this.MaximumSize = this.Size;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Normal;

This do it like this: On load maximizes window with border and sets the maximum size to the maximized with border - it isn't covers taskbar. After this it removes border and returns window to normal state. Add this snippet to load section. Sorry for possible errors, I'm from Poland :)

ry sieł
  • 61
  • 1
  • 6
2

late to this party - anyway - this works for me:

Screen screen = Screen.FromControl(this);
int x = screen.WorkingArea.X - screen.Bounds.X;
int y = screen.WorkingArea.Y - screen.Bounds.Y;
this.MaximizedBounds = new Rectangle(x, y,
    screen.WorkingArea.Width, screen.WorkingArea.Height);
this.MaximumSize = screen.WorkingArea.Size;
this.WindowState = FormWindowState.Maximized;
maj_o
  • 21
  • 2
2

I'm 6 years late but for anyone else experiencing this problem, these two lines should be placed in the Form.Load method:

System.Drawing.Rectangle rect = Screen.GetWorkingArea(this);
this.MaximizedBounds = Screen.GetWorkingArea(this);
Krokodil
  • 1,165
  • 5
  • 19
0

Very late coming to this but in .net 4.7.2 I just used

Rectangle wa = Screen.FromControl(this).WorkingArea;
this.MaximumSize = new Size(wa.Width, wa.Height);
WindowState = FormWindowState.Maximized;

The clues are all in the previous posts, but this seems more succinct.

Mr Lister
  • 3
  • 4
  • It works pretty good for the height, but my width is a little short when I maximize so there is a gap where I can see the desktop. – Baddack Sep 24 '20 at 18:16
0

I am VERY late for the party, but based on the solution suggested by ry sieł, I finally fixed my problem:

  • Inside OnLoad event:
this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Maximized;
  • Inside OnShow event:
this.MaximumSize = this.Size;
this.FormBorderStyle = FormBorderStyle.None;
Gabic
  • 484
  • 1
  • 6
  • 15