当前位置: 首页 > news >正文

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

参考文章链接: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、自定义
在这里插入图片描述

http://www.lryc.cn/news/223807.html

相关文章:

  • MapReduce 读写数据库
  • 设计模式 -- 状态模式(State Pattern)
  • qt quick发布程序启动失败
  • nginx反向代理报错合集
  • 【Linux精讲系列】——vim详解
  • 微信小程序自动化采集方案
  • 操作系统第三章王道习题_内存管理_总结易错知识点
  • uniapp刻度尺的实现(swiper)滑动打分器
  • cordova Xcode打包ios以及发布流程(ionic3适用)
  • idea中的.idea文件夹以及*.iml文件(新版idea没有*.iml文件了),新旧版idea打开同一个项目会不会出现不兼容
  • 高性能网络编程 - The C10K problem 以及 网络编程技术角度的解决思路
  • uniapp u-tabs表单如何默认选中
  • 2023年腾讯云双11活动入口在哪里?
  • Windows 下编译 TensorFlow 2.12.0 CC库
  • Spring Boot 中自动装配机制的原理
  • 如何安装Wnmp并结合内网穿透实现外网访问内网Wnmp服务
  • 网工内推 | 上市公司,云平台运维,IP认证优先,13薪
  • Linux安装DMETL4
  • Python中编码声明的方法
  • css设置浏览器表单自动填充时的背景
  • windows系统下查看安卓apk的sha1
  • openGauss学习笔记-116 openGauss 数据库管理-设置数据库审计-审计概述
  • python编程复习系列——week2(Input Output (2))
  • 为什么HTTP用得很好的,开始普及HTTPS呢?
  • C++day6作业
  • 【 毕设项目源码推荐 javaweb 项目】 基于 springboot+vue 的图书个性化推荐系统的设计与实现(springboot003)
  • FFmpeg编译hevc版本,支持mac、linux系统
  • AI系统ChatGPT程序源码+AI绘画系统源码+支持GPT4.0+Midjourney绘画+已支持OpenAI GPT全模型+国内AI全模型
  • Unity 3D 调整cube的颜色
  • 数字通信和fpga概述——杜勇版本学习笔记