11

I basically want to have my WPF window to go in full screen mode, when F11 is pressed or the maximize button in the right top corner of the window is pressed.

While the following works like a charm for pressing F11:

private void Window_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.F11)
    {
        WindowStyle = WindowStyle.None;
        WindowState = WindowState.Maximized;
        ResizeMode = ResizeMode.NoResize;
    }
}

This will still displays the Windows taskbar (tested with Windows 7):

protected override void OnStateChanged(EventArgs e)
{
    if (WindowState == WindowState.Maximized)
    {
        WindowStyle = WindowStyle.None;
        WindowState = WindowState.Maximized;
        ResizeMode = ResizeMode.NoResize;
    }
    base.OnStateChanged(e);
}

What am I missing here? Or can I do it even more elegant?

DefenestrationDay
  • 3,712
  • 2
  • 33
  • 61
Martin Buberl
  • 45,844
  • 25
  • 100
  • 144

9 Answers9

22

WPF seems to be making the decision about whether to go full-screen or respect the taskbar based on the WindowStyle at the time of maximisation. So a kludgy but effective solution is to switch the window back to non-maximised, set the WindowStyle, and then set the window back to maximised again:

private bool _inStateChange;

protected override void OnStateChanged(EventArgs e)
{
  if (WindowState == WindowState.Maximized && !_inStateChange)
  {
    _inStateChange = true;
    WindowState = WindowState.Normal;
    WindowStyle = WindowStyle.None;
    WindowState = WindowState.Maximized;
    ResizeMode = ResizeMode.NoResize;
    _inStateChange = false;
  }
  base.OnStateChanged(e);
}

Although the code is obviously ugly, the transition to Normal and then back to Maximized doesn't seem to make the user experience any worse. On my display, I noticed flicker with both the F11 code and the kludge maximise, but not noticeably worse on the kludge maximise. But your mileage may vary!

itowlson
  • 73,686
  • 17
  • 161
  • 157
  • 5
    *knock knock* - Who's there? - A semaphore from '70. Nice one though, it works. +1 – Filip Ekberg Feb 08 '11 at 22:48
  • This one works just fine. Even it's ugly, it feels still better then implementing `WindowInteropHelper` or some dll imports for something basic like fullscreen. I have to think about it, generally nice idea! – Martin Buberl Feb 08 '11 at 22:54
  • 1
    In fact this creates an ugly visible effect in Window XP, because both WindowState changes have a visual representation in Windows XP, which is a blue title-bar going up and down, giving a drunk window appeal with this technique. – cprcrack Aug 18 '11 at 11:14
4

try this

Topmost="True" and WindowState="Maximized"

you can see your window will cover all screen and hide all with windows taskbar

M A
  • 71,713
  • 13
  • 134
  • 174
3

Another solution that worked for me:

You can set the MaxHeight property of that window to SystemParameters.MaximizedPrimaryScreenHeight using the constructor.

public MainWindow()
{
    InitializeComponent();
    this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
}

Warning: This might not work on extended desktop.

Source: Maximize window with WindowState Problem (application will hide windows taskbar)

Community
  • 1
  • 1
J Pollack
  • 2,788
  • 3
  • 29
  • 43
3

You need to set the Window.Topmost property.

Edit

Check this blog post Maximizing window (with WindowStyle=None) considering Taskbar

Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
2

If you happen to be using WindowChrome to create a custom chrome experience, you'll need to set the GlassFrameThickness to something other than 0 (at least that was the last thing I needed to do to get the TaskBar to be hidden behind the window). That is in addition to the steps provided in the accepted answer.

Garrett
  • 41
  • 4
1

If there is still someone that need a smooth full screen of course tested only on windows 10! In windows 10 minimized do less flickering if you maintain this code order!

    public bool IsFullscreen = false;
    public WindowState lastWindowState;
    private void player_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (IsFullscreen)
        {
            this.WindowStyle = WindowStyle.SingleBorderWindow;
            this.WindowState = lastWindowState;
            IsFullscreen = false;

        }
        else
        {
            lastWindowState = this.WindowState;

            this.WindowStyle = WindowStyle.None;
            if (this.WindowState == WindowState.Maximized)
                this.WindowState = WindowState.Minimized;
            this.WindowState = WindowState.Maximized;
            IsFullscreen = true;
        }
    }
MozzieMD
  • 355
  • 2
  • 12
  • Thanks, works great on Windows XP and Windows 7 as well. – dbostream Oct 06 '15 at 13:11
  • A problem with this answer is that it uses MouseDoubleClick event while the question ask how to change the behavior of Maximize button. While this will work, it would be better if the event used in this example changed so that it will address the question. – 絢瀬絵里 Jun 15 '16 at 17:19
1

In my case, minimizing and maximizing will make the fullscreen size to be a little bit bigger than the screen, so the alternative I found is to temporarily set the Visibility to Collapsed then back to Visible afterwards to force redraw.

Visibility = Visibility.Collapsed;
WindowStyle = WindowStyle.None;
WindowState = WindowState.Maximized;
ResizeMode = ResizeMode.NoResize;
Visibility = Visibility.Visible;
kurakura88
  • 2,185
  • 2
  • 12
  • 18
0

I have found easy way to achieve fullscreen in WPF:

    private double LastHeight, LastWidth;
    private System.Windows.WindowState LastState;

    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.F11)
        {
            if (WindowStyle != WindowStyle.None)
            {
                LastHeight = Height;
                LastWidth = Width;
                LastState = WindowState;

                Topmost = true;
                Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
                Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
                Top = 0;
                Left = 0;
                WindowState = System.Windows.WindowState.Normal;
                WindowStyle = WindowStyle.None;
                ResizeMode = System.Windows.ResizeMode.NoResize;
            }
            else
            {
                WindowStyle = WindowStyle.SingleBorderWindow;
                WindowState = LastState; ;
                ResizeMode = ResizeMode.CanResizeWithGrip;
                Topmost = false;
                Width = LastWidth;
                Height = LastHeight;
            }
        }
    }

This works good in Windows 7 with fixed Taskbar.

sasha_gud
  • 1,635
  • 13
  • 18
  • This way does not work when you are running the program on a dual screen machine because the program is always maximized on the left screen. – André Apr 08 '14 at 12:46
0

You can hide the taskbar if you import user32.dll...

[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);

private const int SW_HIDE = 0;
private const int SW_SHOW = 1;

Usage:

int hwnd = FindWindow("Shell_TrayWnd","");
ShowWindow(hwnd,SW_HIDE);
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108