WPF使用WindowChrome自定义标题栏

第一步:基本实现

  • 添加Window的Style定义,并设置WindowChrome.WindowChrome属性;
  • 设置WindowChrome标题栏:
  1. CaptionHeight——主要用于拖动有效区;
  2. GlassFrameThickness——影响标题栏系统按钮显示,0表示不使用系统按钮【后面介绍】,-1表示用的系统默认值,如下示例则表示标题栏高度30;
  3. 自定义窗体Title 

第二步:优化边界处理

  1. 方法1:模板添加最大化触发器,设置最大化时,内部布局Margin设为8
  2. 方法2:模板添加最大化触发器,设置最大化时,限制布局最大化的宽高最大值

     

第三步:完全自定义标题栏【即,不使用系统的操作按钮】

  1. 初步操作类似第一步,其中将GlassFrameThickness设置为0
  2. 在内容定义部分添加自定义的标题栏,添加操作按钮,并设置按钮属性WindowChrome.IsHitTestVisibleInChrome="True"
  3. 如果不设置WindowChrome.IsHitTestVisibleInChrome,则由于我们之前设置CaptionHeight,则这个区域内,按钮将失效。
  4. 但是,也不能将整个标题栏布局设置这个属性,那样会完全覆盖系统标题栏的操作,如拖动效果,即CaptionHeight设置的那个区域
    复制代码
    <Window x:Class="WindowChrome自定义窗体样式.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:WindowChrome自定义窗体样式"
            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" UseAeroCaptionButtons="False" 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>
    复制代码
    复制代码
     public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.button_MiniSize.Click += Btn_Min_Click;
                this.button_MaxSize.Click += Btn_Max_Click;
                this.btn_Close.Click += Btn_Close_Click;
                //CommandBindings.Add(new CommandBinding(SystemCommands.CloseWindowCommand, (_, __) => { SystemCommands.CloseWindow(this); }));
                //CommandBindings.Add(new CommandBinding(SystemCommands.MinimizeWindowCommand, (_, __) => { SystemCommands.MinimizeWindow(this); }));
                //CommandBindings.Add(new CommandBinding(SystemCommands.MaximizeWindowCommand, (_, __) => { SystemCommands.MaximizeWindow(this); }));
                //CommandBindings.Add(new CommandBinding(SystemCommands.RestoreWindowCommand, (_, __) => { SystemCommands.RestoreWindow(this); }));
                //CommandBindings.Add(new CommandBinding(SystemCommands.ShowSystemMenuCommand, ShowSystemMenu));
            }
            //private void ShowSystemMenu(object sender, ExecutedRoutedEventArgs e)
            //{
            //    var element = e.OriginalSource as FrameworkElement;
            //    if (element == null)
            //        return;
    
            //    var position = WindowState == WindowState.Maximized ? new Point(0, element.ActualHeight)
            //        : new Point(Left + BorderThickness.Left, element.ActualHeight + Top + BorderThickness.Top);
            //    position = element.TransformToAncestor(this).Transform(position);
            //    SystemCommands.ShowSystemMenu(this, position);
            //}
            private void Btn_Close_Click(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
    
            private void Btn_Max_Click(object sender, RoutedEventArgs e)
            {
                this.WindowState = WindowState.Maximized == this.WindowState ? WindowState.Normal : WindowState.Maximized;
            }
    
            private void Btn_Min_Click(object sender, RoutedEventArgs e)
            {
                this.WindowState = WindowState.Minimized;
            }
        }
    复制代码

     

     

posted @   ¥东方不败  阅读(869)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示