I'm trying to make a WPF app that loads fullscreen, and have the F11 key toggle between fullscreen and windowed.
With the following code, it first appears on the screen properly in fullscreen mode. The toggle pulls it back down to a regular window.
Then the subsequent toggle it almost goes into fullscreen mode but seems shifted up by ~10 pixels, so half the taskbar is visible. I'm able to reproduce this in a new WPF project with an empty main window.
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
this.WindowState = WindowState.Maximized;
this.WindowStyle = WindowStyle.None;
this.ResizeMode = ResizeMode.NoResize;
this.Topmost = true;
this.PreviewKeyDown +=
(s, e) => {
if (e.Key == Key.F11) {
if (this.WindowStyle == WindowStyle.None) {
this.WindowState = WindowState.Normal;
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.ResizeMode = ResizeMode.CanResize;
this.Topmost = false;
} else {
this.WindowState = WindowState.Maximized;
this.WindowStyle = WindowStyle.None;
this.ResizeMode = ResizeMode.NoResize;
this.Topmost = true;
}
}
};
}
}
Is this a bug in the framework? I can't imagine it would have gone unnoticed until now but I've no idea what I'm doing wrong. These are the properties that are supposed to do the job, and they're almost working but not quite. I've tried messing around with other Window
settings but no luck. Any ideas?
According to this WPF Discussion, the order of setting the WindowStyle
and WindowState
matters. You should set the WindowStyle
before the WindowState
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.ResizeMode = ResizeMode.NoResize;
this.WindowStyle = WindowStyle.ToolWindow;
this.WindowState = WindowState.Maximized;
this.Topmost = true;
this.PreviewKeyDown +=
(s, e) =>
{
if (e.Key == Key.F11)
{
if (this.WindowStyle != WindowStyle.SingleBorderWindow)
{
this.ResizeMode = ResizeMode.CanResize;
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.WindowState = WindowState.Normal;
this.Topmost = false;
}
else
{
this.ResizeMode = ResizeMode.NoResize;
this.WindowStyle = WindowStyle.ToolWindow;
this.WindowState = WindowState.Maximized;
this.Topmost = true;
}
}
};
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With