新版自定义窗口标题栏样式基本完成

This commit is contained in:
zengwenjie
2025-09-29 17:15:26 +08:00
parent 3e0d8dc6c5
commit 72e57764ec
6 changed files with 46 additions and 48 deletions

View File

@@ -8,9 +8,19 @@
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<None Remove="Icons\Icon.png" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Deedy.Activity\Deedy.Activity.csproj" /> <ProjectReference Include="..\Deedy.Activity\Deedy.Activity.csproj" />
<ProjectReference Include="..\Deedy.Wpf\Deedy.Wpf.csproj" /> <ProjectReference Include="..\Deedy.Wpf\Deedy.Wpf.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Resource Include="Icons\Icon.png">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Resource>
</ItemGroup>
</Project> </Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 B

View File

@@ -6,10 +6,14 @@
xmlns:local="clr-namespace:Deedy.Testing" xmlns:local="clr-namespace:Deedy.Testing"
xmlns:deedy="clr-namespace:Deedy;assembly=Deedy.Wpf" xmlns:deedy="clr-namespace:Deedy;assembly=Deedy.Wpf"
mc:Ignorable="d" mc:Ignorable="d"
Title="WindowHeaderTest" Height="450" Width="800" Foreground="White"> Title="WindowHeaderTest" Height="450" Width="800" Foreground="White" FontSize="18" d:WindowStyle="None" Icon="/Icons/Icon.png">
<DockPanel> <DockPanel>
<deedy:WindowHeader> <deedy:WindowHeader ButtonBase.Click="WindowHeader_Click">
<UniformGrid Rows="1">
<Button Content="按钮"/> <Button Content="按钮"/>
<Button Content="按钮"/>
<Button Content="按钮"/>
</UniformGrid>
</deedy:WindowHeader> </deedy:WindowHeader>
<Grid/> <Grid/>
</DockPanel> </DockPanel>

View File

@@ -23,5 +23,10 @@ namespace Deedy.Testing
{ {
InitializeComponent(); InitializeComponent();
} }
private void WindowHeader_Click(object sender, RoutedEventArgs e)
{
}
} }
} }

View File

@@ -3,7 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Deedy"> xmlns:local="clr-namespace:Deedy">
<Style TargetType="{x:Type local:WindowHeader}"> <Style TargetType="{x:Type local:WindowHeader}">
<Setter Property="Height" Value="32"/> <Setter Property="Height" Value="40"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="DockPanel.Dock" Value="Top"/> <Setter Property="DockPanel.Dock" Value="Top"/>
@@ -14,6 +14,7 @@
<ControlTemplate.Resources> <ControlTemplate.Resources>
<Style x:Key="controlButtonStyle" TargetType="{x:Type Button}"> <Style x:Key="controlButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/> <Setter Property="Background" Value="Transparent"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template"> <Setter Property="Template">
<Setter.Value> <Setter.Value>
<ControlTemplate TargetType="{x:Type Button}"> <ControlTemplate TargetType="{x:Type Button}">
@@ -26,42 +27,27 @@
</Style> </Style>
</ControlTemplate.Resources> </ControlTemplate.Resources>
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Grid x:Name="Root"> <DockPanel>
<Grid.ColumnDefinitions> <Image Margin="{TemplateBinding Padding}" Stretch="Uniform" Source="{Binding Icon, RelativeSource={RelativeSource AncestorType=Window}}"/>
<ColumnDefinition Width="auto"/> <TextBlock VerticalAlignment="Center" Margin="0,0,6,0" Text="{Binding Title, RelativeSource={RelativeSource AncestorType=Window}}"/>
<ColumnDefinition Width="auto"/> <DockPanel x:Name="Controller" DockPanel.Dock="Right" MinWidth="147" Background="#01FFFFFF" VerticalAlignment="Stretch">
<ColumnDefinition /> <Button x:Name="CloseWin" DockPanel.Dock="Right" Style="{DynamicResource controlButtonStyle}" Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}">
<ColumnDefinition Width="auto"/> <Path Stretch="Uniform" Margin="{TemplateBinding Padding}" Stroke="Transparent" Fill="{TemplateBinding Foreground}"
</Grid.ColumnDefinitions> Data="M 1 0 L 0 1 L 23 24 L 24 23 Z M 1 24 L 0 23 L 11 12 L 12 13 Z M 12 11 L 23 0 L 24 1 L 13 12 Z"/>
<Border x:Name="IconView" Width="{Binding ActualHeight, ElementName=Root}">
<Image Margin="{TemplateBinding Padding}" VerticalAlignment="Center" HorizontalAlignment="Center"
Source="{Binding Icon, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
</Border>
<TextBlock x:Name="TitleBar" Grid.Column="1" FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}"
Margin="{TemplateBinding Padding}" VerticalAlignment="Center" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Text="{Binding Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
<Border x:Name="Container" Grid.Column="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
<StackPanel x:Name="Controller" Grid.Column="3" Orientation="Horizontal">
<Button x:Name="Minimize" VerticalAlignment="Stretch" Style="{StaticResource controlButtonStyle}" Cursor="Hand"
Width="{Binding RelativeSource={RelativeSource Self},Path=ActualHeight}" Padding="{TemplateBinding Padding}">
<Viewbox Stretch="Uniform">
<Path Width="1024" Height="1024" Stroke="{TemplateBinding Foreground}" Fill="{TemplateBinding Foreground}" Data="M917.333333 554.666667H106.666667a21.333333 21.333333 0 0 1 0-42.666667h810.666666a21.333333 21.333333 0 0 1 0 42.666667z"/>
</Viewbox>
</Button> </Button>
<Button x:Name="Maximize" VerticalAlignment="Stretch" Style="{StaticResource controlButtonStyle}" Cursor="Hand" <Button x:Name="Maximize" DockPanel.Dock="Right" Style="{DynamicResource controlButtonStyle}" Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}">
Width="{Binding RelativeSource={RelativeSource Self},Path=ActualHeight}" Padding="{TemplateBinding Padding}"> <Path Stretch="Uniform" Margin="{TemplateBinding Padding}" Stroke="Transparent" Fill="{TemplateBinding Foreground}"
<Viewbox Stretch="Uniform"> Data="M 0 6 V 24 H 18 V 6 H 16.5 V 22.5 H 1.5 V 7.5 H 16.5 V 6 Z M 6 0 H 24 V 18 H 22.5 V 1.5 H 6 Z"/>
<Path Width="1024" Height="1024" Stroke="{TemplateBinding Foreground}" Fill="{TemplateBinding Foreground}" Data="M714.666667 256H138.666667a53.393333 53.393333 0 0 0-53.333334 53.333333v576a53.393333 53.393333 0 0 0 53.333334 53.333334h576a53.393333 53.393333 0 0 0 53.333333-53.333334V309.333333a53.393333 53.393333 0 0 0-53.333333-53.333333z m10.666666 629.333333a10.666667 10.666667 0 0 1-10.666666 10.666667H138.666667a10.666667 10.666667 0 0 1-10.666667-10.666667V309.333333a10.666667 10.666667 0 0 1 10.666667-10.666666h576a10.666667 10.666667 0 0 1 10.666666 10.666666z m213.333334-746.666666v565.333333a21.333333 21.333333 0 0 1-42.666667 0V138.666667a10.666667 10.666667 0 0 0-10.666667-10.666667H320a21.333333 21.333333 0 0 1 0-42.666667h565.333333a53.393333 53.393333 0 0 1 53.333334 53.333334z"/>
</Viewbox>
</Button> </Button>
<Button x:Name="CloseWin" VerticalAlignment="Stretch" Style="{StaticResource controlButtonStyle}" Cursor=""
Width="{Binding RelativeSource={RelativeSource Self},Path=ActualHeight}" Padding="{TemplateBinding Padding}"> <Button x:Name="Minimize" DockPanel.Dock="Right" Style="{DynamicResource controlButtonStyle}" Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}">
<Viewbox Stretch="Uniform"> <Path Stretch="Uniform" Margin="{TemplateBinding Padding}" Stroke="Transparent" Fill="{TemplateBinding Foreground}"
<Path Width="1024" Height="1024" Stroke="{TemplateBinding Foreground}" Fill="{TemplateBinding Foreground}" Data="M542.173333 512l347.58-347.58a21.333333 21.333333 0 1 0-30.173333-30.173333L512 481.826667 164.42 134.246667a21.333333 21.333333 0 0 0-30.173333 30.173333L481.826667 512l-347.58 347.58a21.333333 21.333333 0 0 0 30.173333 30.173333L512 542.173333l347.58 347.58a21.333333 21.333333 0 0 0 30.173333-30.173333z"/> Data="M 0 0 V 24 M 0 11.25 V 12.75 H 24 V 11.25 Z"/>
</Viewbox>
</Button> </Button>
</StackPanel> <Border/>
</Grid> </DockPanel>
<Decorator x:Name="Container" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</DockPanel>
</Border> </Border>
</ControlTemplate> </ControlTemplate>
</Setter.Value> </Setter.Value>

View File

@@ -15,8 +15,6 @@ using System.Windows.Shell;
namespace Deedy namespace Deedy
{ {
[ContentProperty("Child")] [ContentProperty("Child")]
[TemplatePart(Name = "IconView", Type = typeof(Border))]
[TemplatePart(Name = "TitleBar", Type = typeof(TextBlock))]
[TemplatePart(Name = "Minimize", Type = typeof(Button))] [TemplatePart(Name = "Minimize", Type = typeof(Button))]
[TemplatePart(Name = "Maximize", Type = typeof(Button))] [TemplatePart(Name = "Maximize", Type = typeof(Button))]
[TemplatePart(Name = "CloseWin", Type = typeof(Button))] [TemplatePart(Name = "CloseWin", Type = typeof(Button))]
@@ -61,8 +59,6 @@ namespace Deedy
private Button? Minimize; private Button? Minimize;
private Button? Maximize; private Button? Maximize;
private Button? CloseWin; private Button? CloseWin;
private TextBlock? TitleBar;
private Border? IconView;
private Window? Target; private Window? Target;
private WindowChrome? Chrome; private WindowChrome? Chrome;
@@ -102,8 +98,7 @@ namespace Deedy
this.Minimize = GetTemplateChild("Minimize") as Button; this.Minimize = GetTemplateChild("Minimize") as Button;
this.Maximize = GetTemplateChild("Maximize") as Button; this.Maximize = GetTemplateChild("Maximize") as Button;
this.CloseWin = GetTemplateChild("CloseWin") as Button; this.CloseWin = GetTemplateChild("CloseWin") as Button;
this.IconView = GetTemplateChild("IconView") as Border;
this.TitleBar = GetTemplateChild("TitleBar") as TextBlock;
if (this.Container != null) this.Container.Child = this.Child; if (this.Container != null) this.Container.Child = this.Child;
} }
private void Target_Loaded(object sender, RoutedEventArgs e) private void Target_Loaded(object sender, RoutedEventArgs e)
@@ -115,6 +110,7 @@ namespace Deedy
WindowChrome.SetWindowChrome(this.Target, this.Chrome); WindowChrome.SetWindowChrome(this.Target, this.Chrome);
WindowChrome.SetIsHitTestVisibleInChrome(this.Container, true); WindowChrome.SetIsHitTestVisibleInChrome(this.Container, true);
WindowChrome.SetIsHitTestVisibleInChrome(this.Controller, true); WindowChrome.SetIsHitTestVisibleInChrome(this.Controller, true);
if (this.Controller != null) if (this.Controller != null)
{ {
foreach (var c in this.Controller.Children) foreach (var c in this.Controller.Children)
@@ -140,12 +136,11 @@ namespace Deedy
{ {
if (this.Target != null) if (this.Target != null)
{ {
this.Visibility = Visibility.Visible;
if (this.Controller != null) this.Controller.Visibility = Visibility.Visible; if (this.Controller != null) this.Controller.Visibility = Visibility.Visible;
if (this.Minimize != null) this.Minimize.Visibility = Visibility.Visible; if (this.Minimize != null) this.Minimize.Visibility = Visibility.Visible;
if (this.Maximize != null) this.Maximize.Visibility = Visibility.Visible; if (this.Maximize != null) this.Maximize.Visibility = Visibility.Visible;
if (this.CloseWin != null) this.CloseWin.Visibility = Visibility.Visible; if (this.CloseWin != null) this.CloseWin.Visibility = Visibility.Visible;
if (this.IconView != null) this.IconView.Visibility = Visibility.Visible;
if (this.TitleBar != null) this.TitleBar.Visibility = Visibility.Visible;
if (this.Target.ResizeMode == ResizeMode.NoResize) if (this.Target.ResizeMode == ResizeMode.NoResize)
{ {
@@ -158,9 +153,7 @@ namespace Deedy
} }
if (this.Target.WindowStyle == WindowStyle.None) if (this.Target.WindowStyle == WindowStyle.None)
{ {
if (this.IconView != null) this.IconView.Visibility = Visibility.Collapsed; this.Visibility = Visibility.Collapsed;
if (this.TitleBar != null) this.TitleBar.Visibility = Visibility.Collapsed;
if (this.Controller != null) this.Controller.Visibility = Visibility.Collapsed;
} }
if (this.Target.WindowStyle == WindowStyle.ToolWindow) if (this.Target.WindowStyle == WindowStyle.ToolWindow)
{ {