Answered: See comment section below. Seems I can't post a proper SO answer after this question was marked a duplicate...
Background: Using Winforms and C# Visual Studio 2012, .net 4.5, testing on Win7.
Desired behavior: On "maximize" button, size the main form so that it uses the entire screen except for the task bar, and preserves the title/caption bar.
This is NOT a duplicate. This question is about why this code DOES NOT WORK PROPERLY, despite being posted as a solution in other questions. Thanks!
I coded the following form method:
private void HSBmainForm_Resize(object sender, EventArgs e)
{
// On clicking Maximize, sender is main form, but event is "Empty" so useless...
// Instead, look at window state and adjust form size as appropriate
switch(WindowState) {
case FormWindowState.Maximized: {
var workArea = Screen.FromControl(this).WorkingArea; // supposedly, screen size minus taskbars and bound toolbars, in pixels
Debug.WriteLine(workArea);
// Nope, eliminates desired caption bar and min/max/etc buttons: FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; // no border when maximized
this.MaximumSize = new System.Drawing.Size(workArea.Width, workArea.Height); // supposedly, pixels
} break;
case FormWindowState.Normal:
this.MaximumSize = new System.Drawing.Size(1600, 900); // Minimum workable size for layout of app's forms
break;
};
base.OnLocationChanged(e); // process the event in base class (actually performs the resize)
}
Unfortunately after maximizing the task bar remains visible as desired, but the main form does not use the entire expected area as shown here (lower right of main form does not go to edge of screen):
Any idea what's wrong?
Thanks in advance, Best Regards, Dave
PS: In this test, Windows screen resolution reports 1823 x 978 and workArea reports {X=79,Y=0,Width=1744,Height=963} in debug output.
PPS: The above result is after clicking Maximize the first time. Minimize always works as expected. The second and subsequent Maximize clicks work correctly and use entire screen! Any ideas? Calling the base class event processor twice doesn't make it work on the first attempt...