首页 > 其他分享 >WPF使用WindowChrome自定义标题栏

WPF使用WindowChrome自定义标题栏

时间:2022-12-14 11:11:56浏览次数:52  
标签:自定义 标题栏 WindowState WindowChrome 按钮 WPF Btn Click

第一步:基本实现

  • 添加Window的Style定义,并设置WindowChrome.WindowChrome属性;
  • 设置WindowChrome标题栏:
  1. CaptionHeight——主要用于拖动有效区;
  2. GlassFrameThickness——影响标题栏系统按钮显示,0表示不使用系统按钮【后面介绍】,-1表示用的系统默认值,如下示例则表示标题栏高度30;
  3. 自定义窗体Title 
  4. <Style x:Key="WindowStyle1" TargetType="{x:Type Window}">
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome CaptionHeight="30" GlassFrameThickness="0,30,0,0"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
                        <AdornerDecorator  >
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="30"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <ContentPresenter Grid.Row="1"/>
                                <TextBlock Text="{TemplateBinding Title}" HorizontalAlignment="Left" VerticalAlignment="Center" />
                            </Grid>
                        </AdornerDecorator>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

第二步:优化边界处理

  1. 方法1:模板添加最大化触发器,设置最大化时,内部布局Margin设为8
  2. 方法2:模板添加最大化触发器,设置最大化时,限制布局最大化的宽高最大值
    <ControlTemplate TargetType="{x:Type Window}">
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
            <AdornerDecorator  >
                <Grid Name="win_content">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="30" x:Name="row_title"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <ContentPresenter Grid.Row="1"/>
                    <TextBlock Text="{TemplateBinding Title}" HorizontalAlignment="Left" VerticalAlignment="Center" />
                </Grid>
            </AdornerDecorator>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="WindowState" Value="Maximized">
                <Setter Property="Margin" TargetName="win_content" Value="8"/>
                <!--二选一-->
                <Setter Property="MaxWidth" TargetName="win_content" Value="{Binding Source={x:Static SystemParameters.WorkArea},Path=Width}" />
                <Setter Property="MaxHeight" TargetName="win_content" Value="{Binding Source={x:Static SystemParameters.WorkArea},Path=Height}"/>
    
                <Setter Property="Height" TargetName="row_title" Value="22" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

     

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

  1. 初步操作类似第一步,其中将GlassFrameThickness设置为0
  2. 在内容定义部分添加自定义的标题栏,添加操作按钮,并设置按钮属性WindowChrome.IsHitTestVisibleInChrome="True"
  3. 如果不设置WindowChrome.IsHitTestVisibleInChrome,则由于我们之前设置CaptionHeight,则这个区域内,按钮将失效。
  4. 但是,也不能将整个标题栏布局设置这个属性,那样会完全覆盖系统标题栏的操作,如拖动效果,即CaptionHeight设置的那个区域。
    <!--样式定义-->
    <Style x:Key="WindowStyle2" TargetType="{x:Type Window}">
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome UseAeroCaptionButtons="False" GlassFrameThickness="0" CaptionHeight="30"  />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"  >
                        <AdornerDecorator >
                            <ContentPresenter x:Name="win_content" />
                        </AdornerDecorator>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="WindowState" Value="Maximized">
                            <Setter Property="Margin" TargetName="win_content" Value="8"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!--定义标题栏-->
    <Grid Background="Red" >
        <Grid Height="30" HorizontalAlignment="Stretch" VerticalAlignment="Top" Background="Blue">
            <StackPanel Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True" HorizontalAlignment="Right" >
                <Button Name="btn_Min" Content="—" ></Button>
                <Button Name="btn_Max" Content="☐" ></Button>
                <Button Name="btn_Close" Content="✕" ></Button>
            </StackPanel>
        </Grid>
    </Grid>
    //标题栏按钮功能实现
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            this.btn_Min.Click += Btn_Min_Click;
            this.btn_Max.Click += Btn_Max_Click;
            this.btn_Close.Click += Btn_Close_Click;
        }
    
        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;
        }
    }

     

标签:自定义,标题栏,WindowState,WindowChrome,按钮,WPF,Btn,Click
From: https://www.cnblogs.com/lisghxfdfgh/p/16981542.html

相关文章

  • 写过vue自定义指令吗,原理是什么?.m
    背景看了一些自定义指令的文章,但是探究其原理的文章却不多见,所以我决定水一篇。如何自定义指令?其实关于这个问题官方文档上已经有了很好的示例的,我们先来温故一下。除......
  • 自定义RBAC(5)
    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~ 把实体类及Service类都准备好了之后,就可以开始继续写业务代码了。Spring Security的强大之一就在于它的拦截器。那么这......
  • #yyds干货盘点#【愚公系列】2022年12月 微信小程序-自定义导航栏功能的实现
    前言导航栏是指位于页面顶部或者侧边区域的,在页眉横幅图片上边或下边的一排水平导航按钮,它起着链接站点或者软件内的各个页面的作用。小程序原生导航栏的限制除了胶囊按......
  • java自定义注解实现前后台参数校验
     其实是可以通过@Constraint来限定自定义注解的方法。@Constraint(validatedBy=xxxx.class) 下面是我做的java自定义注解实现前后台参数校验的代码示例对这个感兴趣的......
  • WPF 系列-01默认程序结构
    WPF应用程序启动项创建一个WPF应用程序,系统为我们自动生成了App.xaml和一个普通的MainWindow.xaml窗体文件。App.xaml和cs文件文件如下:<Applicationx:C......
  • WPF 系列-01默认程序结构
    WPF应用程序启动项创建一个WPF应用程序,系统为我们自动生成了App.xaml和一个普通的MainWindow.xaml窗体文件。App.xaml和cs文件文件如下:<Applicationx:C......
  • spring mvc环境之UI到控制器的自定义类型转换(十三)
     spring其实有默认的类型转换,比如前端表单提交数字的字符串,在控制器可接收为int或string都是没有错的.另外控制器也可以把前端数据接收为一个对象.即使spring为我们考虑......
  • wpf WebView2 打印
    如果想要在wpf中使用浏览器的打印功能如何实现呢?可以选择CefSharp。但是CefSharp打包的程序似乎是包含浏览器内核,导致程序包太大。现在用WebView2试试。<Grid><Grid.......
  • Winform Vs Installer之添加自定义安装流程
    1、简介在Winform安装工具之VsInstaller介绍了VsInstaller的基本使用,可以满足基本需求,但是开发中遇到一些需要自定义安装流程的需求,如何通过VsInstaller来完成......
  • 【Unity】 HTFramework框架(三十七)Inspector自定义序列化检视器
    更新日期:2020年8月21日。Github源码:​​​[点我获取源码]​​​Gitee源码:​​[点我获取源码]​​索引​​Inspector自定义序列化检视器​​​​使用​​​​Dropdown下拉......