Make my wpf application Full Screen (Cover taskbar and title bar of window)

心已入冬 提交于 2019-11-28 08:28:26

You need to set the WindowStyle to none as well as WindowState to Maximized

<Window ...    
 WindowStyle="None"   
 WindowState="Maximized">

You need to set the ResizeMode to NoResize and WindowState to Maximized

  <Window ...    
    ResizeMode="NoResize" WindowState="Maximized">
Sowvik Roy

Try this:

<Window ShowTitleBar="False" IgnoreTaskbarOnMaximize="True">

If the taskbar doesn't disappear, it may help to change the Window visibility before and after changing window style, like this:

    private void MainWindow_StateChanged(object sender, EventArgs e) {
        if (this.WindowState == WindowState.Maximized) {
            // hide the window before changing window style
            this.Visibility = Visibility.Collapsed;
            this.Topmost = true;
            this.WindowStyle = WindowStyle.None;
            this.ResizeMode = ResizeMode.NoResize;
            // re-show the window after changing style
            this.Visibility = Visibility.Visible;
        }
        else {
            this.Topmost = false;
            this.WindowStyle = WindowStyle.SingleBorderWindow;
            this.ResizeMode = ResizeMode.CanResize;
        }
    }

You just need to set the WindowStyle to none:

<Window ...
    WindowStyle="None">

I had this issue with the taskbar staying on top of my window. The current solution i use is setting the window as Topmost for a short time, then setting it back to false (i want my window to work nice with Alt+Tab)

private Timer t;
public void OnLoad()
    {
        var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
        StartTopmostTimer(window);
    }

    private void StartTopmostTimer(Window window)
    {
        t = new Timer(o => SetTopMostFalse(window), null, 1000, Timeout.Infinite);
    }

    private void SetTopMostFalse(Window window)
    {
        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
        {
            window.Topmost = false;
        }));
        t.Dispose();
    }

I also use this code to switch between full screen and window mode:

public void SwitchFullScreen()
    {
        var window = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);

        if (window != null)
        {
            if (window.WindowStyle == WindowStyle.None)
            {
                window.WindowStyle = WindowStyle.SingleBorderWindow;
                window.WindowState = state;
            }
            else
            {
                state = window.WindowState;
                window.WindowStyle = WindowStyle.None;
                window.WindowState = WindowState.Maximized;
                window.Topmost = true;
                StartTopmostTimer(window);
            }
        }
    }

Step 1: Write class with static methods Hide() and Show() for taskbar

public class Taskbar
{
    [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;

    protected static int Handle
    {
        get
        {
            return FindWindow("Shell_TrayWnd", "");
        }
    }

    private Taskbar()
    {
        // hide ctor
    }

    public static void Show()
    {
        ShowWindow(Handle, SW_SHOW);
    }

    public static void Hide()
    {
        ShowWindow(Handle, SW_HIDE);
    }
}

Step 2: Connect to window Closing event to get taskbar back when window will close with Alt+F4

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    Taskbar.Show();
}

Hide taskbar and show fullscreen:

Taskbar.Hide();
WindowStyle = WindowStyle.None;
WindowState = WindowState.Maximized;
ResizeMode = ResizeMode.NoResize;
Width = System.Windows.SystemParameters.PrimaryScreenWidth;
Height = System.Windows.SystemParameters.PrimaryScreenHeight;
Topmost = true;
Left = 0;
Top = 0;

Show taskbar and run in window

Taskbar.Show();
WindowStyle = WindowStyle.SingleBorderWindow;
WindowState = WindowState.Normal;
ResizeMode = ResizeMode.CanResize;
Width = System.Windows.SystemParameters.WorkArea.Width-100;
Height = System.Windows.SystemParameters.WorkArea.Height-100;
Topmost = false;
Left = 0;
Top = 0;

Tested on Windows 10 1703 (Creators Update)

Simply hook this event handler to the Loaded event of the form, works fine.
Do not apply this stuff in the constructor of the form (which won't work for me).

    private void aWindow_Loaded(object sender, RoutedEventArgs e)
    {
        MaxHeight = SystemParameters.FullPrimaryScreenHeight;
        MaxWidth = SystemParameters.FullPrimaryScreenWidth;
        Width = SystemParameters.FullPrimaryScreenWidth;
        Height = SystemParameters.FullPrimaryScreenHeight;
        WindowState = WindowState.Maximized;
        ResizeMode = ResizeMode.NoResize;
        Left = 0;
        Top = 0;
        Topmost = true;
        ShowInTaskbar = false;

        //throw new NotImplementedException();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!