I have a WinForms app that needs to have a custom titlebar, so I removed the native titlebar via FormBorderStyle = FormBorderStyle.None
This causes an issue where the maximized window covers the taskbar. This is mentioned in a couple other posts like this one.
The answer with the most upvotes suggests: this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
, but that doesn’t quite work for me, because the window is like half off screen.
The solution below works better:
var workingArea = Screen.FromHandle(Handle).WorkingArea;
MaximizedBounds = new Rectangle(0, 0, workingArea.Width, workingArea.Height);
On my primary monitor, it maximizes fine. On my secondary monitor, the right and bottom sides go off screen.
I added a MaximizedBoundsChanged
event handler which is showing that when I maximize the Form form.WindowState = FormWindowState.Maximized
, the form’s MaximizedBounds
is getting changed for some reason:
So I set the MaximizedBounds
to {X=0,Y=0,Width=2560,Height=1392}
which then gets changed to {X=1920,Y=-99,Width=3200,Height=1460}
(interestingly enough, 3200 – 2560 = 640 and 2560 (width of secondary monitor) – 640 = 1920 (width of primary monitor).
My code is like this:
var workingArea = Screen.FromHandle(this.Handle).WorkingArea;
var maximizedBounds = new Rectangle(0, 0, workingArea.Width, workingArea.Height);
MaximizedBounds = maximizedBounds;
ResizeEnd += (s, e) =>
{
var workingArea = Screen.FromHandle(this.Handle).WorkingArea;
var maximizedBounds = new Rectangle(0, 0, workingArea.Width, workingArea.Height);
MaximizedBounds = maximizedBounds;
};
I looked at the solution here and set dpi awareness to PerMonitorV2 in app.manifest as well as by calling this in my Main method: Application.SetHighDpiMode(HighDpiMode.PerMonitorV2);
No help.
Why does maximizing the Form change the MaximizedBounds? What’s triggering that? This doesn’t happen when I add the titlebar back (remove FormBorderStyle = FormBorderStyle.None
)
I just want a frameless Form that doesn’t cover the taskbar when maximized.