资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

四、WinUI3下TitleBar的自定义

WinUI3下TitleBar的自定义

对于Windows软件开发者来说重写标题栏样式是一个很重要的事情,在WPF阶段很多人写出来性能很差的窗口,而且为了适配Win11系统的Snaplayout后性能就更差了,这篇是写WinUI3下提供的重写TitleBar的方式;

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了扶余免费建站欢迎大家使用!

1、修改文本

public MainWindow()
{
    this.InitializeComponent();
    Title = "Duwenlong learn App Title";
}

可以修改默认标题栏显示文本;但是无法自定义其他内容;所有操作都请在InitializeComponent方法后执行,不然会报错;

public MainWindow()
{
    this.InitializeComponent();

    // Hide default title bar.
    ExtendsContentIntoTitleBar = true;
}

修改后我们有完整的区域用看显示内容,只是目前还比较丑而且标题栏部分不支持拖动;

2、完全自定义

通过调用 Window.SetTitleBar 方法并传入定义拖动区域的 UIElement 来指定拖动区域。

1、标题栏内容和拖动区域

代码如下:


    
        
            
            
        
         
            
        
     
 
public MainWindow()
{
    this.InitializeComponent(); 
    ExtendsContentIntoTitleBar = true;
    SetTitleBar(AppTitleBar);
} 

2、交互式内容

我们可以放置一些不影响用户的交互式按钮
使用XAML的嵌套语法尝试放置一些内容,我放置了一个textblock,用于在标题栏显示一个消息;


    
        
            
            
        
        
            
            
        
     

4、系统标题按钮的颜色和透明度

将应用内容扩展到标题栏区域时,可以使标题按钮的背景透明,我默认设置了WindowCaptionBackground为透明色

 
    
        
            
                
                
            
            
            Transparent
            LightGreen
            Red
            Pink 
        
    
 

5、当窗口处于非活动状态时,调暗标题栏


    
        
            
                
                
            
            
            
            Green
            LightGreen
            Red
            Pink 
        
    


using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace TitleBarCustomizationDemo
{
    /// 
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// 
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent(); 
            ExtendsContentIntoTitleBar = true;
            SetTitleBar(AppTitleBar);
            Activated += MainWindow_Activated;
        }

        private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
        {
            if (args.WindowActivationState == WindowActivationState.Deactivated)
            {
                AppTitleTextBlock.Foreground =
                    (SolidColorBrush)App.Current.Resources["WindowCaptionForegroundDisabled"];
            }
            else
            {
                AppTitleTextBlock.Foreground =
                    (SolidColorBrush)App.Current.Resources["WindowCaptionForeground"];
            }
        }
    }
} 

6、重置标题栏

可以在应用运行时调用 SetTitleBar 切换到新的标题栏元素。或者恢复为默认;


    
        
            
            
        
         
            
            
        
        
private void Button_Click(object sender, RoutedEventArgs e)
{ 
    SetTitleBar(null);
    ExtendsContentIntoTitleBar = false;
}

因为使用的是Win10机器,图床又挂了所以不贴图了,Win11下会很好看。


网站名称:四、WinUI3下TitleBar的自定义
浏览地址:http://cdkjz.cn/article/dsoides.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220