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?