winui3开发笔记(二)自定义标题栏

本文介绍了如何在Windows11中使用WindowsAppSDK1.2及以上版本定制App窗口标题栏的颜色,强调了早期版本(如Win10的1.0)不支持此功能。开发者需确保开发环境正确以实现预期效果。
摘要由CSDN通过智能技术生成

参考文章链接:https://www.programminghunter.com/article/46392310600/

注意事项

获取 AppWindowTitleBar 的实例并设置其颜色属性时,InitializeTitleBar(AppWindow.TitleBar);,只适用于Windows App SDK 1.2及以上,所以如果用win10开发,最多1.0,之前一直没有发现时开发系统环境问题,根本运行不了。所以改成win11开发就可以了
官方文档说明链接:https://learn.microsoft.com/zh-cn/windows/apps/develop/title-bar?tabs=wasdk

代码:

App.xaml.cs:

using Microsoft.UI;
using Microsoft.UI.Windowing;
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 Microsoft.UI.Xaml.Shapes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using WinRT.Interop;

namespace App1
{
    public partial class App : Application
    {
        public App()
        {
            this.InitializeComponent();
        }
        protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
        {
            m_window = new MainWindow();
            _windowHandle = WindowNative.GetWindowHandle(m_window);
            var windowId = Win32Interop.GetWindowIdFromWindow(_windowHandle);
            AppWindow = AppWindow.GetFromWindowId(windowId);
            AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
            InitializeTitleBar(AppWindow.TitleBar);
            m_window.Activate();
        }

        private IntPtr _windowHandle;
        public static AppWindow AppWindow { get; private set; }// 应用窗口对象.
        public static Window m_window { get; private set; }// 主窗口.
        public static void InitializeTitleBar(AppWindowTitleBar bar)
        {
            bar.ExtendsContentIntoTitleBar = true;
            // 设置成自己预期的颜色即可
            bar.ButtonBackgroundColor = ColorHelper.FromArgb(0, 240, 243, 249);
            bar.ButtonForegroundColor = Colors.Black;
            bar.ButtonHoverBackgroundColor = ColorHelper.FromArgb(0, 240, 243, 249);
            bar.ButtonHoverForegroundColor = Colors.Black;
            bar.ButtonPressedBackgroundColor = ColorHelper.FromArgb(0, 240, 243, 249);
            bar.ButtonPressedForegroundColor = Colors.Black;
            bar.ButtonInactiveBackgroundColor = ColorHelper.FromArgb(0, 240, 243, 249);
            bar.ButtonInactiveForegroundColor = Colors.Black;
        }
    }
}

AppTitleBar.xaml

<Grid Height="48" Padding="16,0,0,0" Background="#F0F3F9" RequestedTheme="Light">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition x:Name="SearchColumn" Width="*" />
        <ColumnDefinition Width="120" />
    </Grid.ColumnDefinitions>

    <StackPanel VerticalAlignment="Center" Orientation="Horizontal" Spacing="16">
        <TextBlock VerticalAlignment="Center" Style="{StaticResource CaptionTextBlockStyle}"
    Text="DeskTop" />
    </StackPanel>
</Grid>

MainWindow.xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <!--删除border,改换titlebar-->
    <local1:AppTitleBar />
    <Frame x:Name="frame" Grid.Row="1"></Frame>
</Grid>

按照最开始的文章链接做的实验图片:
1、在这里插入图片描述
2、在这里插入图片描述3、在这里插入图片描述4、自定义
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值