Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Fullscreen toggle still showing part of desktop

Tags:

c#

wpf

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?

like image 622
Dax Fohl Avatar asked Dec 19 '14 21:12

Dax Fohl


1 Answers

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;
                }
             }
          };
   }
}
like image 108
SwDevMan81 Avatar answered Nov 03 '22 16:11

SwDevMan81