Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF full screen on maximize

Tags:

c#

wpf

fullscreen

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?

like image 657
Martin Buberl Avatar asked Feb 08 '11 22:02

Martin Buberl


2 Answers

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!

like image 129
itowlson Avatar answered Sep 22 '22 01:09

itowlson


try this

Topmost="True" and WindowState="Maximized"

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

like image 21
Uday Phadke Avatar answered Sep 19 '22 01:09

Uday Phadke