Wpf 无边框窗口应用程序比我的屏幕分辨率占用更多空间
我已经在WPF中创建了一个无边界应用程序,它运行得非常好。但是,当我将WindowsState设置为全屏时,应用程序占用的空间比我的屏幕分辨率还要大,因此屏幕外各个方向都有一些像素!(看起来添加了一些硬编码的负边距以隐藏默认边框) 有没有办法防止这种情况发生 我的xaml:Wpf 无边框窗口应用程序比我的屏幕分辨率占用更多空间,wpf,window,fullscreen,borderless,Wpf,Window,Fullscreen,Borderless,我已经在WPF中创建了一个无边界应用程序,它运行得非常好。但是,当我将WindowsState设置为全屏时,应用程序占用的空间比我的屏幕分辨率还要大,因此屏幕外各个方向都有一些像素!(看起来添加了一些硬编码的负边距以隐藏默认边框) 有没有办法防止这种情况发生 我的xaml: <Window x:Class="MyApp.Shell" WindowStyle="None" BorderThickness="0" AllowsTransparency="True"
<Window x:Class="MyApp.Shell"
WindowStyle="None"
BorderThickness="0"
AllowsTransparency="True"
Background="Transparent"
ResizeMode="CanResizeWithGrip"
WindowState="{Binding MainApplicationWindowState}"
...
我这样解决了这个问题:
XAML:
WindowStyle="None"
Left="0"
Top="0"
Width="{Binding WPFSettings.Width}"
Height="{Binding WPFSettings.Height}">
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
Visual Basic:
WindowStyle="None"
Left="0"
Top="0"
Width="{Binding WPFSettings.Width}"
Height="{Binding WPFSettings.Height}">
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
它工作得很好。在Xaml中,在窗口上设置以下绑定。MaxHeight
:
MaxHeight="{DynamicResource {x:Static SystemParameters.MaximizedPrimaryScreenHeightKey}}"
不需要额外的实用程序类。在我的例子中,窗口的XAML标记具有SizeToContent=“True”属性,我所要做的就是删除它。我找到了这个很棒的解决方案
<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>
...
但对我来说,这个错误仍然存在,窗口在顶部和左侧偏移了几个像素。。。在更改窗口状态后,我尝试将“左”和“顶”设置为0,但什么也没有发生。试试这个
Width="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"
Height="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"
NET的一个较新功能解决了这个问题。
离开您的WindowStyle=“SingleBorderWindow”
或“ThreeDBorderWindow”
保留ResizeMode=“CanResize”
,然后将其添加到
<Window>
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="0" CornerRadius="0" CaptionHeight="0"
UseAeroCaptionButtons="False" ResizeBorderThickness="7" />
</WindowChrome.WindowChrome>
</Window>
该窗口将没有任何默认窗口窗格,但仍允许调整大小,并且在最大化时不会覆盖任务栏。我需要它来处理不同大小的显示(就像大多数人听起来一样)。所以我就这么做了
<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);
}
}
....
代码
....
公共无效窗口\u SizeChanged(对象发送方,SizeChangedEventArgs e)
{
if(this.WindowState==WindowState.Maximized)
{
this.BorderThickness=新系统.Windows.Thickness(8);
}
其他的
{
this.BorderThickness=新系统.Windows.Thickness(0);
}
}
我在窗口上使用触发器:
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=WindowState}" Value="{x:Static WindowState.Maximized}">
<Setter Property="BorderThickness" Value="8"/>
</DataTrigger>
</Style.Triggers>
当窗口状态最大化时,此触发器将创建边框。我认为它很有用。谢谢。我会试试看。我在PrimaryScreenHeight中看到的一个问题是当使用第二个监视器时,但解决这个问题应该相当简单!一个音符;既然您使用的是顶部和左侧,那么底部和右侧呢?它们会在屏幕外移动得更远吗?太好了。但对我来说,只有在您发布的XAML之上,我还设置了WindowState=“Maximized”,它才起作用,但如果我想在第二台显示器上使用我的应用程序呢?不起作用,而且当您使用WindowStyle时!=None AllowTransparency必须为false!我刚刚用WindowStyle=“None”和allowsttransparency=“True”试过,效果很好。它允许调整大小,就像有边框一样,当窗口最大化时,它不会覆盖开始菜单。(我使用.NET 4.7.2进行了测试,没有尝试早期版本)谢谢,这是唯一对我有效的解决方案,9年前也使用了.NET 4.7.2Wow:D问题是否仅在设置AllowTransparency或WindowChrome时出现?