Search code examples
c#wpffullscreen

WPF: How to achieve an automatic full screen that not hides the taskbar, with no resize and no window style?


How to achieve an automatic full screen that not hides the taskbar, with no resize and no window style?

I tried to use the following code, but it doesn't work right, as shown in the image below.

XAML:

<Window x:Class="WPF_Test_Environment.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        WindowState="Maximized" ResizeMode="NoResize" WindowStyle="None">
    <DockPanel>
        <Button DockPanel.Dock="Top" Height="50">Top</Button>
        <Button DockPanel.Dock="Bottom" Height="50">Bottom</Button>
        <Button DockPanel.Dock="Left" Width="50">Left</Button>
        <Button DockPanel.Dock="Right" Width="50">Right</Button>
        <Button Width="50" Height="50">Center</Button>
    </DockPanel>
</Window>

Code-Behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        MinHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
        MinWidth = SystemParameters.MaximizedPrimaryScreenWidth;
    }
}

Here is the result: Result

As you can see, the "Bottom" button is partially underneath the taskbar, and I want it to be entirely above it. So, ideally, it would look as shown in the following image, but without the border on the top: enter image description here


Solution

  • It can be done with calls to unmanaged code. Check this article to see how to do it. Basically, just remove your width and height settings from code-behind, implement the WindowSizing class from the article and call it from SourceInitialized event of the window.

    Edit

    The other solution would be to add reference to Windows Forms and use:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // Set the width and height values to the values of working area
        this.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
        this.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
        // Move the window to top left corner of the screen
        this.Left = 0;
        this.Top = 0;   
    }
    

    Just make sure to remove WindowState="Maximized" from your window.

    Not sure if any of these is elegant though.