使用 WPF shell 集成库最大化时窗口超出屏幕 [英] Window out of the screen when maximized using WPF shell integration library
问题描述
我正在使用 WPF Shell 集成库来创建我的 wpf 应用程序的自定义镶边.一切都很好,但在最大化应用程序时,屏幕外有 6 或 7 个像素.
I'm using the WPF Shell Integration Library to create a custom chrome of my wpf app. All is good, but when maximizing the app, 6 or 7 pixels are out of the screen.
这是我正在使用的代码:
This is the code I'm using:
<Style TargetType="{x:Type local:MainWindow}">
<Setter Property="shell:WindowChrome.WindowChrome">
<Setter.Value>
<shell:WindowChrome
ResizeBorderThickness="6"
CaptionHeight="10"
CornerRadius="0"
GlassFrameThickness="1"/>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MainWindow}">
<Grid>
<Border BorderThickness="1" BorderBrush="#389FD1" Background="#389FD1">
<ContentPresenter Margin="0,22,0,0" Content="{TemplateBinding Content}"/>
</Border>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" >
<TextBlock Text="{Binding NombreUsuario}" Foreground="White" Margin="5,5,20,5" Opacity=".8" />
<Button Style="{StaticResource ImageButton}" Height="20" Width="20" Margin="0" Click="WindowMinimize" shell:WindowChrome.IsHitTestVisibleInChrome="True">
<Image Height="10" Width="10" Source="/Resources/Images/minimize.png" />
</Button>
<Button Style="{StaticResource ImageButton}" Height="20" Width="20" Margin="0" Click="WindowMaximizeRestore" shell:WindowChrome.IsHitTestVisibleInChrome="True" >
<Image Height="10" Width="10" Source="/Resources/Images/maximize.png" />
</Button>
<Button Style="{StaticResource ImageButton}" Height="20" Width="20" Margin="0" Click="WindowClose" shell:WindowChrome.IsHitTestVisibleInChrome="True">
<Image Height="10" Width="10" Source="/Resources/Images/close.png" />
</Button>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
推荐答案
当窗口最大化时,Windows 会裁剪窗口的边缘以掩盖通常是调整大小的边缘.您可以通过在窗口和内容之间放置一个代理边框来解决这个问题,然后在最大化时增加厚度.
Windows crops the edges of the window when it's maximized to obscure what would normally be the resize edges. You can get around this by putting a proxy border between the window and your content and then inflate the thickness when it's maximized.
我修改了 lib 附带的示例来执行此操作,可以对您的示例进行相同的基本更改:
I modified the example that came with the lib to do this, the same basic change could be made to your sample:
<ControlTemplate TargetType="{x:Type local:SelectableChromeWindow}">
<Border BorderBrush="Green">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="0"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ThisWindow, Path=WindowState}" Value="Maximized">
<Setter Property="BorderThickness" Value="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowResizeBorderThickness}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Grid [...]/>
</Border>
</ControlTemplate>
希望能帮到你.
对于 .net 4.5 及更高版本,系统参数略有不同,例如:
For .net 4.5 and above, the SystemParameters are a little different, e.g.:
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}, Path=WindowState}" Value="Maximized">
<Setter Property="BorderThickness" Value="{Binding Source={x:Static SystemParameters.WindowResizeBorderThickness}}"/>
</DataTrigger>
这篇关于使用 WPF shell 集成库最大化时窗口超出屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!