Borderless window application takes up more space

2020-02-28 05:02发布

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

8条回答
Evening l夕情丶
2楼-- · 2020-02-28 05:27

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-28 05:30

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

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

No need for an additional utility class.

查看更多
男人必须洒脱
4楼-- · 2020-02-28 05:32

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.

查看更多
Melony?
5楼-- · 2020-02-28 05:32

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楼-- · 2020-02-28 05:34

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);
        }
    }
查看更多
何必那么认真
7楼-- · 2020-02-28 05:38

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.

查看更多
登录 后发表回答