首页 > 其他分享 >WPF 自定义顶部标题栏的实现方式

WPF 自定义顶部标题栏的实现方式

时间:2024-08-17 09:54:56浏览次数:9  
标签:bitmapSource 自定义 BitmapSource 标题栏 System iconPath WPF icon Icon

感谢吕毅,本文主要全是他的思路,哈哈哈。我这里就是简单的做个归纳总结,不讲原理,不讲思路。主打一个拿来主义。

感兴趣的朋友可以直接前往他的文章里看一下,讲的很细致,可以照着做一做。我应该是借鉴了很久了,但是一致都没有正经的整理过。

本文主要是为了贯彻落实拿来就用,能跑就行这两大政策方针,将政策落地。

原文中只是写了windows的title,我的话,日常使用过程中还需要个icon在里面,所以多加了个icon上去。不废话,看代码吧。

首先是页面部分,基本照抄,margin top 略微加了2个像素,看起来协调点:

<WindowChrome.WindowChrome>
    <WindowChrome GlassFrameThickness="0 64 0 0" NonClientFrameEdges="Left,Bottom,Right" />
</WindowChrome.WindowChrome>
<Window.Template>
    <ControlTemplate TargetType="Window">
        <Border Padding="0 30 0 0">
            <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
                <Border Background="{TemplateBinding Background}" VerticalAlignment="Top" Height="30" Margin="0 -29 140 0">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center" Margin="4 0">
                            <!-- Image Control to show the window icon -->
                            <Image Source="{TemplateBinding Icon}" Width="16" Height="16" Margin="4,0"/>
                            <!-- TextBlock for the title -->
                            <TextBlock Foreground="Black" VerticalAlignment="Center" FontSize="12" Margin="0,2,0,0" Text="{TemplateBinding Title}" />
                        </StackPanel>
                        <!-- 这里可以搞点别的小东西 -->
                    </Grid>
                </Border>
                <ContentPresenter />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="WindowState" Value="Maximized">
                <Setter TargetName="RootGrid" Property="Margin" Value="6" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Template>

然后是xaml.cs部分, 如果icon图标未指定的话,从ApplicatonIcon里读一下:

public MainWindow()
{
    InitializeComponent();
    if (Icon == null)
    {
        string iconPath = System.Windows.Forms.Application.ExecutablePath;
        Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(iconPath);
        if (icon != null)
        {
            // 创建 BitmapSource
            BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHIcon(
                icon.Handle,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            // 将 BitmapSource 设置为 Icon
            this.Icon = bitmapSource;
        }
    }
}

标签:bitmapSource,自定义,BitmapSource,标题栏,System,iconPath,WPF,icon,Icon
From: https://blog.csdn.net/wynneira/article/details/141257719

相关文章

  • WPF事件
    鼠标输入事件必须继承FrameworkElement:UIElement鼠标事件:MouseEnterMouseLeaveMouseDownMouseUpMouseMoveMouseLeftButtonDownMouseLeftButtonUpMouseRightButtonDownMouseRightButtonUpMouseDoubleClickClick:事件:特殊<ButtonContent="MouseEvent"MouseLeftB......
  • WPF customize line with sharp arrow and direction
    //CustomizeLineArrowusingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Media;usingSystem.Windows.Shapes;usingSystem.Windows;namespac......
  • WPF 集合通知更改
    集合通知更改,ObservableCollection。属性通知更改,适合单个属性,如果是多个属性的集合数据,使用ObservableCollection。 publicpartialclassButtonWindow:Window{ObservableCollection<Students>infos;publicButtonWindow(){......
  • C# WPF现代化开发:绑定、模板与动画进阶
    ......
  • WPF控件结构与Content理解
    WPF控件结构WPF中控件继承图我们平时所用的容器如Grid、StackPanel等都是继承Panel控件类型分为3组:内容控件、Items控件、TextBoxBase如何理解Content?凡是继承ContentControl的控件,定义内容为Content,除了TextBlock用text以外,大部分都是用Content设置显示类容。一个窗......
  • WPF 命令Command
    MVVM的目的是为了最大限度地降低了Xaml文件和CS文件的合度,分离界面和业务逻辑,所以我们要尽可能的在View后台不写代码。但是这个例子中,我们将更新ViewModel的代码写在了View里。我们能否把按钮的响应处理代码也不写在后台代码里呢?WPF引入Command(命令),通过为Button设置Command来......
  • WPF 绑定
    绑定就是Binding,是控件和数据之间交互的类。source={binding}和source={bindingRelativeSource={RelativeSourceself},Path=DataContext}效果相同。例如:直接绑定数据源前台xaml界面<Grid><StackPanelOrientation="Vertical"><TextBlock......
  • 鸿蒙中hvigor自定义任务
    鸿蒙中,各个module都可以自定义hvigor任务进行一些编译期的操作,方便多模块打包时,进行编译期的一些修改和配置下面简单列举下自定义任务的创建import{hapTasks}from'@ohos/hvigor-ohos-plugin';let__Version='1.0.0'exportfunctionloadVersionFun(params){re......
  • WPF Animation 动画变化值的监控
    WPF动画XXXAnimation即关键类继承制AnimationBase的动画类线性插值动画主要属性FromToDurationAcceleratorDecceleratorFillBehavior等这些常用属性里不做介绍,主要介绍一下几个故事板属性,以备忘记.名称说明Completed动画到达终点时触发,该事件中可以......
  • WPF 触发器
    一、样式触发器样式触发器可以在指定的控件属性满足某种条件后进行一些样式的变换,当触发条件不满足时恢复原样。样式触发器的简单使用<Window.Resources><Stylex:Key="checkBoxStyle"TargetType="CheckBox"><Style.Triggers><TriggerProperty="......