-->

Borderless window application takes up more space

2020-02-28 05:05发布

问题:

I have created a borderless application in WPF, and it works pretty good. However, when I set the WindowState to full screen, the application takes up more space than my screen resolution, so there are some pixels outside the screen in all directions! (looks like some hard coded negative margins are added to hide the default border)

Any Ideas how to prevent this from happening?

My xaml:

<Window x:Class="MyApp.Shell"
    WindowStyle="None"
    BorderThickness="0"
    AllowsTransparency="True"
    Background="Transparent"
    ResizeMode="CanResizeWithGrip"
    WindowState="{Binding MainApplicationWindowState}"
    ...

Also, another problem I have seen is that the Windows toolbar / taskbar is covered in the fullsize state, so it looks like the "actual" screen height is used and not the "available" screen height, meaning screen height minus the windows toolbar / taskbar!

Anyone found a solution to these issues?

Thanks

回答1:

I solved the problem this way:

XAML:

WindowStyle="None"
Left="0"
Top="0"
Width="{Binding WPFSettings.Width}"
Height="{Binding WPFSettings.Height}">

Visual Basic:

Public Class WPFSettings
   Public ReadOnly Property Width() As Double
      Get
         Return System.Windows.SystemParameters.PrimaryScreenWidth
      End Get
   End Property

   Public ReadOnly Property Height() As Double
      Get
         Return System.Windows.SystemParameters.PrimaryScreenHeight
      End Get
   End Property
End Class

It works pretty good.



回答2:

I found this great solution

<Window WindowStyle="None" WindowStartupLocation="CenterScreen" WindowState="Maximized"
        MaxWidth="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"
        MaxHeight="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
    >
...
</Window>

But error for me still persists, windows is offset by few pixels on top and left... I tried to set Left and Top to 0 after I change window state but nothing happens.



回答3:

Try this

Width="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"
Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"


回答4:

A newer feature of .NET has solved this problem. Leave your WindowStyle="SingleBorderWindow" or "ThreeDBorderWindow" Leave ResizeMode="CanResize", then add this to the xaml inside the

<Window>
    <WindowChrome.WindowChrome>
        <WindowChrome GlassFrameThickness="0" CornerRadius="0" CaptionHeight="0" 
            UseAeroCaptionButtons="False" ResizeBorderThickness="7" />
    </WindowChrome.WindowChrome>
</Window>

The window will not have any of the default window pane, but will still allow resizing and will not cover the task bar when maximized.



回答5:

In my case the XAML tag of the Window had the Property SizeToContent="True" and all I had to do was to remove it.



回答6:

In Xaml set the following binding on the Window.MaxHeight:

MaxHeight="{DynamicResource {x:Static SystemParameters.MaximizedPrimaryScreenHeightKey}}"

No need for an additional utility class.



回答7:

I needed this to work across different size displays (like most people it sounds). So I simply did this...

<Window x:Class="MyApp.MainWindow"
    ResizeMode="CanResize" 
    WindowStyle="SingleBorderWindow"
    SizeChanged="Window_SizeChanged">
....
Code
....
</Window>


public void Window_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        if (this.WindowState == WindowState.Maximized)
        {
            this.BorderThickness = new System.Windows.Thickness(8);
        }
        else
        {
            this.BorderThickness = new System.Windows.Thickness(0);
        }
    }


回答8:

I use a trigger on the window:

<Style.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=WindowState}" Value="{x:Static WindowState.Maximized}">
        <Setter Property="BorderThickness" Value="8"/>
    </DataTrigger>
</Style.Triggers>

this trigger create a Border when the windowstate is Maximized. I think it's useful.