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

11

22

I would like to make my application such that it can maximize to full screen means it hide the windows task bar and the title bar as well. And it should triggered by a button.

I am trying to develop the my application window like this. enter image description here

Add my code snippet below

 <controls:MetroWindow x:Class="EDUI.MainWindow"
            xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:local="clr-namespace:EDiscoveryCore;assembly=EDiscoveryCore"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="eDi"  BorderBrush="SkyBlue" BorderThickness="2" Height="999" Width="1071" WindowState="Maximized" x:Name="MainWindows">
Finicking answered 26/8, 2014 at 6:28 Comment(0)
F
14

Try this:

<Window ShowTitleBar="False" IgnoreTaskbarOnMaximize="True">
Finicking answered 26/8, 2014 at 8:4 Comment(4)
To use IgnoreTaskbarOnMaximize you need to be using MahApps, see mahapps.comParochialism
ShowInTaskbar="False"Halsted
Is ShowTitleBar a real property? It's giving me an error, and apparently the correct way of hiding the title bar is WindowStyle="None"Makings
@Makings ShowTitleBar is a property from MahApps.MetroWindowFolketing
A
25

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

<Window ...    
 WindowStyle="None"   
 WindowState="Maximized">
Applicable answered 26/8, 2014 at 6:53 Comment(0)
M
21

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

  <Window ...    
    ResizeMode="NoResize" WindowState="Maximized">
Monica answered 26/8, 2014 at 7:0 Comment(4)
ResizeMode="NoResize" was the missing piece for me with Windows 10 WPF. Thank you @MonicaEagan
Worked for me also. Thank you!Lambent
This also was solution for WPF on windows 10. Without that it works fine on win 7 but not on win 8 and win 10. I am using WindowState="Maximized" WindowStyle="None" ResizeMode="NoResize"Rosemari
I already had WindowStyle=None and WindowState=Maximized (in this order!) which worked in Windows 7 and Windows 8.1 without a problem. But as soon as my application opened a dialog window, the taskbar appeared topmost on Windows 10 - and it didn't went away, except when switching to a different window and back to my application. ResizeMode=NoResize solved this!Menell
N
19

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;
        }
    }
Nelidanelie answered 5/1, 2016 at 15:22 Comment(2)
That did it for me. Thanks a lot!!Veator
This worked on 2nd monitor. Primary monitor was already working with WindowStyle=None and WindowState=MaximizedLayfield
F
14

Try this:

<Window ShowTitleBar="False" IgnoreTaskbarOnMaximize="True">
Finicking answered 26/8, 2014 at 8:4 Comment(4)
To use IgnoreTaskbarOnMaximize you need to be using MahApps, see mahapps.comParochialism
ShowInTaskbar="False"Halsted
Is ShowTitleBar a real property? It's giving me an error, and apparently the correct way of hiding the title bar is WindowStyle="None"Makings
@Makings ShowTitleBar is a property from MahApps.MetroWindowFolketing
U
4

You just need to set the WindowStyle to none:

<Window ...
    WindowStyle="None">
Uriisa answered 26/8, 2014 at 6:30 Comment(0)
S
2

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)

enter image description here

Selenite answered 11/11, 2017 at 23:18 Comment(0)
C
1

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);
            }
        }
    }
Consignor answered 6/4, 2017 at 7:17 Comment(0)
C
1

None of the above worked for me. Even with the Windows API the taskbar does hide but there is still the space after the window is maximized. What worked is to not set the Maximized property, get the size of the desktop and set these:

Top = 0;
Left = 0;
Width = width_of_your_desktop;
Height = height_of_your_desktop;

Don't even need to set the Topmost! To get the screen size you can either use the value from SystemParameters.PrimaryScreenHeight and PrimaryScreenWidth, or if you want to get the screen where the window is currently on, use GetMonitorFromWindow from below:

[StructLayout(LayoutKind.Sequential)]
private struct MonitorInfo
{
    public uint cbSize;
    public Rect2 rcMonitor;
    public Rect2 rcWork;
    public uint dwFlags;
}

[StructLayout(LayoutKind.Sequential)]
private struct Rect2
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}

private const int MONITOR_DEFAULTTONULL = 0;
private const int MONITOR_DEFAULTTOPRIMARY = 1;
private const int MONITOR_DEFAULTTONEAREST = 2;
[DllImport("user32.dll")]
private static extern IntPtr MonitorFromWindow(IntPtr hwnd, int flags);
[DllImport("user32.dll")]
private static extern bool GetMonitorInfo(IntPtr hwnd, ref MonitorInfo mInfo);

public static Rect GetMonitorFromWindow(Window win) {
    var mi = new MonitorInfo();
    mi.cbSize = (uint)Marshal.SizeOf(mi);
    var hwmon = MonitorFromWindow(new System.Windows.Interop.WindowInteropHelper(win).EnsureHandle(), MONITOR_DEFAULTTONULL);
    if (hwmon != null && GetMonitorInfo(hwmon, ref mi)) {
        //convert to device-independent vaues
        var mon = mi.rcMonitor;
        Point realp1;
        Point realp2;
        var trans = PresentationSource.FromVisual(win).CompositionTarget.TransformFromDevice;
        realp1 = trans.Transform(new Point(mon.left, mon.top));
        realp2 = trans.Transform(new Point(mon.right, mon.bottom));
        return new Rect(realp1, realp2);
    }
    else
        throw new Exception("Failed to get monitor info.");
}
Cham answered 12/2, 2020 at 18:40 Comment(0)
N
0

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();
    }
Norm answered 15/9, 2018 at 10:14 Comment(0)
C
0

A complete workable Solution

 private void BtnFullScreen_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        this.Topmost = false;
        if (WindowState != WindowState.Maximized)
        {
            WindowState = WindowState.Maximized;
            WindowStyle = WindowStyle.None;

            btnFullScreen.Source = this.FindResource("FullScreenIn") as System.Windows.Media.ImageSource;
             
            this.ResizeMode = ResizeMode.NoResize;
            this.Visibility = Visibility.Collapsed;
            this.Topmost = true;
           
            //// re-show the window after changing style
            this.Visibility = Visibility.Visible;
            MaxHeight = SystemParameters.VirtualScreenHeight; MaxWidth = SystemParameters.VirtualScreenWidth;
        }
        else
        {
            WindowState = WindowState.Normal;
            WindowStyle = WindowStyle.SingleBorderWindow;
            ResizeMode = ResizeMode.CanResize;

            //WindowStartupLocation = WindowStartupLocation.CenterScreen;
            Top = 0.0;
            double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
            double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
            double windowWidth = this.Width;
            double windowHeight = this.Height;
            this.Left = (screenWidth / 2) - (windowWidth / 2);
            btnFullScreen.Source = this.FindResource("FullScreenOut") as System.Windows.Media.ImageSource;
             
        }
    }
Caduceus answered 4/8, 2021 at 6:49 Comment(0)
B
0

If, like me, you are using a custom window style/chrome and other answers did not work, this may help you:

private WindowState windowStateBeforeFullScreen = WindowState.Normal;
private double widthBeforeFullScreen = 0;
private double heightBeforeFullScreen = 0;
private double leftBeforeFullScreen = 0;
private double topBeforeFullScreen = 0;
private bool _fullscreenMode = false;

public bool FullScreenMode
{
    get => _fullscreenMode;
    set
    {
        if (value)
        {
            if (_fullscreenMode == false)
            {
                windowStateBeforeFullScreen = WindowState;
                widthBeforeFullScreen = Width;
                heightBeforeFullScreen = Height;
                leftBeforeFullScreen = Left;
                topBeforeFullScreen = Top;
            }

            // The following 6 lines are the important part
            ResizeMode = ResizeMode.NoResize;

            Width = SystemParameters.PrimaryScreenWidth;
            Height = SystemParameters.PrimaryScreenHeight;
            Left = 0;
            Top = 0;

            WindowState = WindowState.Normal;


            // If you are using custom minimize/maximize/restore/close buttons, 
            // now would be a good time to hide the custom maximize/restore button
            // RestoreButton.Visibility = Visibility.Hidden;
        }
        else // restore everything back to like it was before
        {
            ResizeMode = ResizeMode.CanResize;

            // RestoreButton.Visibility = Visibility.Visible;

            if (_fullscreenMode == true)
            {
                WindowState = windowStateBeforeFullScreen;
                Width = widthBeforeFullScreen;
                Height = heightBeforeFullScreen;
                Left = leftBeforeFullScreen;
                Top = topBeforeFullScreen;
            }
        }

        _fullscreenMode = value;
    }
}

There is a main flaw, as the window will always use the primary screen. This is fine when there is only one, but otherwise you will need to offset the Width, Height, Top and Left properties accordingly.


Side Note: If you had to reimplement the drag mechanism with DragMove() in your custom Window, make sure to not do it when FullscreenMode == true.

e.g.:

private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed && FullScreenMode == false)
    {
        if (e.ClickCount == 2) // Double click on title bar
            WindowState = (WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
        else
            DragMove();
    }
}
Blunderbuss answered 26/7, 2022 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.