WPF 使用WindowChrome自定义窗体样式

示例代码:

<Window x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
FontWeight="ExtraLight" ResizeMode="CanResize" WindowStartupLocation="CenterScreen"
WindowStyle="None" AllowsTransparency="True" Background="{x:Null}">
<Window.Resources>
<Style x:Key="btn_nap" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="FontSize" Value="18"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0.7"/>
</Trigger>
<!--<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="#EEF0F5"/>
</Trigger>-->
</Style.Triggers>
</Style>
</Window.Resources>
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="35" x:Name="windowChrome" CornerRadius="0" GlassFrameThickness="0"/>
</WindowChrome.WindowChrome>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Background="#3C6AB1">
<TextBlock x:Name="lblTitle" Text="测试" Foreground="White" FontSize="14" Margin="10 0 0 0" VerticalAlignment="Center"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button WindowChrome.IsHitTestVisibleInChrome="True" Name="button_MiniSize" Content="─" Style="{StaticResource btn_nap}" HorizontalAlignment="Right" Foreground="White" Margin="0 0 5 0" Height="30" Width="30"/>
<Button WindowChrome.IsHitTestVisibleInChrome="True" Name="button_MaxSize" Content="☐" Style="{StaticResource btn_nap}" HorizontalAlignment="Right" Foreground="White" Margin="0 0 5 0" Height="30" Width="30"/>
<Button WindowChrome.IsHitTestVisibleInChrome="True" x:Name="btn_Close" Content="✕" Style="{StaticResource btn_nap}" HorizontalAlignment="Right" Foreground="White" Margin="0 0 5 0" Height="30" Width="30"/>
</StackPanel>
</Grid>
<Grid Grid.Row="1" Background="LightBlue">

</Grid>
</Grid>
</Window>

  


效果:

介绍:
WindowChrome
若要在保留其标准功能的同时自定义窗口,可以使用 WindowChrome 类。 WindowChrome类将窗口框架的功能与视觉对象隔开,并使你能够控制应用程序窗口的客户端和非工作区之间的边界。 WindowChrome通过使用类,您可以通过扩展工作区以覆盖非工作区来将 WPF 内容置于窗口框架中。 同时,它将通过两个不可见区域保留系统行为; 调整边框 和 标题 区的大小。
使用类创建自定义窗口分为两个主要部分 WindowChrome 。 首先,通过设置对象上公开的属性来自定义窗口的非客户端部分 WindowChrome 。 然后,为窗口提供一个模板,该模板定义扩展到非工作区的应用程序部分。 对象上公开的属性 WindowChrome 为 ResizeBorderThickness 、、 CaptionHeight CornerRadius 和 GlassFrameThickness 。
ResizeBorderThickness属性指定应用程序窗口外的不可见边框,用户可以单击并拖动它来调整窗口的大小。
CaptionHeight属性在窗口顶部指定一个不可见的区域,该区域启用通常与标题栏关联的系统行为。 这些行为包括:单击并拖动以移动窗口,双击以最大化窗口,并右键单击以显示 “系统” 菜单。
调整边框和标题区的大小不包含任何可视元素;它们仅定义响应输入和启用标准系统提供的窗口行为的区域。
CornerRadius属性指定窗口的角的舍入量。 如果为窗口启用了玻璃框架,则此属性不起作用。
GlassFrameThickness属性指定窗口周围的玻璃帧的宽度。 默认情况下,它使用属性指定的系统值 WindowNonClientFrameThickness 来模拟标准窗口的外观。 使用玻璃帧时,“最小化”、“最大化” 和 “关闭” 的标题按钮是可见的,并且是交互式的。 应用程序负责显示应用程序图标和标题文本。 可以设置 GlassFrameThickness 属性,使玻璃框架更宽或更小。

介绍参考:https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.shell.windowchrome?view=netframework-4.7.2
————————————————
版权声明:本文为CSDN博主「Danny_hi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_43024228/article/details/113529541

原文地址:https://www.cnblogs.com/robertyao/p/14849560.html