1、功能说明
将窗体的标题栏改成自定义标题栏。
2、自定义控件库UiLib
(1)、BaseWindow.cs
namespace UiLib
{
public class BaseWindow:Window
{
public BaseWindow()
{
DefaultStyleKey = typeof(BaseWindow);
CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, CloseWindow));
CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, ResizeWindow));
CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, MinimizeWindow));
}
private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.MinimizeWindow(this);
}
private void ResizeWindow(object sender, ExecutedRoutedEventArgs e)
{
WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
}
private void CloseWindow(object sender, ExecutedRoutedEventArgs e)
{
SystemCommands.CloseWindow(this);
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
if (e.ButtonState == MouseButtonState.Pressed)
DragMove();
}
}
}
(2)、BaseWindow.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UiLib">
<Style TargetType="{x:Type local:BaseWindow}">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="BorderThickness" Value="7"/>
<Setter Property="Background" Value="#F7F7F7" />
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CaptionHeight="50" ResizeBorderThickness="10"/>
</Setter.Value>
</Setter>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect BlurRadius="10" ShadowDepth="0" Color="Black"/>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:BaseWindow}">
<Border x:Name="windowBorder" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid x:Name="layoutRoot" Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="#305090" IsHitTestVisible="True">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding RelativeSource={RelativeSource AncestorLevel=1,Mode=FindAncestor,AncestorType={x:Type local:BaseWindow}},Path=Title}" Foreground="White" FontSize="20" FontFamily="微软雅黑"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Width="120" FlowDirection="RightToLeft" Margin="10" Background="#01FFFFFF">
<Button x:Name="close" Width="20" Height="20" Margin="5,0" Command="{x:Static SystemCommands.CloseWindowCommand}" WindowChrome.IsHitTestVisibleInChrome="True">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" Background="#01FFFFFF">
<Path x:Name="path" Data="M1,1 L13,13 M1,13 L13,1" FlowDirection="LeftToRight" Width="14" Height="14" Stretch="None" UseLayoutRounding="True" VerticalAlignment="Center" HorizontalAlignment="Center" Stroke="#FFFFFF" StrokeThickness="2.5" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="grid" Property="Background" Value="#55FFFFFF"/>
<Setter TargetName="path" Property="Stroke" Value="White"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="grid" Property="Background" Value="#33FFFFFF"/>
<Setter TargetName="path" Property="Stroke" Value="#D0D6DF"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<Button x:Name="resize" Width="20" Height="20" Margin="5,0" Command="{x:Static SystemCommands.RestoreWindowCommand}" WindowChrome.IsHitTestVisibleInChrome="True">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" Background="#01FFFFFF">
<Path x:Name="path" Data="M1,1 L1,13 L13,13 L13,1 z" FlowDirection="LeftToRight" Width="14" Height="14" UseLayoutRounding="True" VerticalAlignment="Center" HorizontalAlignment="Center" Stroke="#E0E6EF" StrokeThickness="2"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="grid" Property="Background" Value="#55FFFFFF"/>
<Setter TargetName="path" Property="Stroke" Value="White"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="grid" Property="Background" Value="#33FFFFFF"/>
<Setter TargetName="path" Property="Stroke" Value="#D0D6DF"/>
</Trigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorLevel=1,Mode=FindAncestor,AncestorType={x:Type local:BaseWindow}},Path=WindowState}" Value="Maximized">
<Setter TargetName="path" Property="Data" Value="M1,4 L10,4 L10,13 L1,13 z M4,4 L4,1 L13,1 L13,9 L10,9"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<Button x:Name="minsize" Width="20" Height="20" Margin="5,0" Command="{x:Static SystemCommands.MinimizeWindowCommand}" WindowChrome.IsHitTestVisibleInChrome="True">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid x:Name="grid" Background="#01FFFFFF">
<Path x:Name="path" Data="M0,7 L14,7" FlowDirection="LeftToRight" Width="14" Height="14" UseLayoutRounding="True" VerticalAlignment="Center" HorizontalAlignment="Center" Stroke="#E0E6EF" StrokeThickness="2"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="grid" Property="Background" Value="#55FFFFFF"/>
<Setter TargetName="path" Property="Stroke" Value="White"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="grid" Property="Background" Value="#33FFFFFF"/>
<Setter TargetName="path" Property="Stroke" Value="#D0D6DF"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</Grid>
<AdornerDecorator Grid.Row="1" KeyboardNavigation.IsTabStop="False">
<ContentPresenter x:Name="windowContent" KeyboardNavigation.TabNavigation="Cycle" />
</AdornerDecorator>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
(3)、Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:UiLib">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/UiLib;component/Themes/BaseWindow.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
3、WPF应用程序
(1)、MainWindow.xaml
<Window x:Class="Demo.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:Demo"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
xmlns:wccl="clr-namespace:UiLib;assembly=UiLib"
FontFamily="Source Han Sans CN-Regular"
FontWeight="Regular"
FontSize="20"
Title="Demo" Height="500" Width="800" WindowState="Maximized">
<Grid>
</Grid>
</Window>
(2)、MainWindow.cs
namespace Demo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : UiLib.BaseWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}