【MAUI Blazor踩坑日记】3.Windows标题栏自定义颜色,运行时改变颜色
目录
MAUI中Windows默认的标题栏颜色是灰色的,有一点丑。

如果去掉默认的标题栏,自己画一个,可能会出现问题,也比较麻烦。
想要自定义默认标题栏的颜色,官方又没有提供接口。找过了很多资料,也用过很多办法,始终不能完美的解决这个问题,大多数只是类似于配置的修改,不能在运行时修改颜色。
直到看到一位大佬的文章https://www.dongchuanmin.com/net/2955.html ,终于有了眉目。
在Win10和Win11中都可以正常用。
修改默认颜色
找到Platforms/Windows/App.xaml
<maui:MauiWinUIApplication
x:Class="MauiApp1.WinUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:maui="using:Microsoft.Maui"
xmlns:local="using:MauiApp1.WinUI">
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="WindowCaptionBackground">#fff</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionBackgroundDisabled">#fff</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionForeground">#000</SolidColorBrush>
<SolidColorBrush x:Key="WindowCaptionForegroundDisabled">#000</SolidColorBrush>
</ResourceDictionary>
</Application.Resources>
</maui:MauiWinUIApplication>
修改运行时颜色
安装NuGet包
NuGet包中搜索 PInvoke.User32 ,安装它

特别提示
项目文件中请务必按照下面这样写,否则打包的时候其他平台也会将PInvoke.User32这个包打进去,实际上是完全无用的,它只适用于Windows。
```
<PackageReference Include="PInvoke.User32" Version="0.7.124" Condition="$(TargetFramework.Contains('-windows')) == true" />
```
添加修改标题栏颜色的类
Platforms/Windows文件夹下添加
WindowsTitleBar.csusing Microsoft.Maui.Platform;
using PInvoke;
using WinRT.Interop;
using static PInvoke.User32; namespace MauiApp1
{
# nullable disable
public static class WindowsTitleBar
{
private static Microsoft.UI.Xaml.Window GetActiveNativeWindow() =>
(Microsoft.UI.Xaml.Window)Application.Current.Windows.FirstOrDefault()?.Handler?.PlatformView; public static void SetColorForWindows(Color backgroundColor,Color foregroundColor)
{
var res = Microsoft.UI.Xaml.Application.Current.Resources;
res["WindowCaptionBackground"] = backgroundColor.ToWindowsColor();
res["WindowCaptionBackgroundDisabled"] = backgroundColor.ToWindowsColor();
res["WindowCaptionForeground"] = foregroundColor.ToWindowsColor();
res["WindowCaptionForegroundDisabled"] = foregroundColor.ToWindowsColor();
TriggertTitleBarRepaint();
} private static bool TriggertTitleBarRepaint()
{
#if WINDOWS
var nativeWindow = GetActiveNativeWindow();
if (nativeWindow is null)
{
return default;
} var hWnd = WindowNative.GetWindowHandle(nativeWindow);
var activeWindow = User32.GetActiveWindow();
if (hWnd == activeWindow)
{
User32.PostMessage(hWnd, WindowMessage.WM_ACTIVATE, new IntPtr((int)0x00), IntPtr.Zero);
User32.PostMessage(hWnd, WindowMessage.WM_ACTIVATE, new IntPtr((int)0x01), IntPtr.Zero);
}
else
{
User32.PostMessage(hWnd, WindowMessage.WM_ACTIVATE, new IntPtr((int)0x01), IntPtr.Zero);
User32.PostMessage(hWnd, WindowMessage.WM_ACTIVATE, new IntPtr((int)0x00), IntPtr.Zero);
} #endif
return true;
}
}
}调用
#if WINDOWS
var backgroundColor = Colors.White;
var foreColor = Colors.Black;
WindowsTitleBar.SetColorForWindows(backgroundColor, foreColor);
#endif
效果图

【MAUI Blazor踩坑日记】3.Windows标题栏自定义颜色,运行时改变颜色的更多相关文章
- weex 开发踩坑日记--环境配置、安卓运行、adb、开发
环境配置方面 1.需要安装java和android环境,java的话一定要下载jdk而不是jre. 在"系统变量"新建一个变量名为JAVA_HOME的变量,变量值为你本地java的 ...
- AI相关 TensorFlow -卷积神经网络 踩坑日记之一
上次写完粗浅的BP算法 介绍 本来应该继续把 卷积神经网络算法写一下的 但是最近一直在踩 TensorFlow的坑.所以就先跳过算法介绍直接来应用场景,原谅我吧. TensorFlow 介绍 TF是g ...
- 人工智能(AI)库TensorFlow 踩坑日记之一
上次写完粗浅的BP算法 介绍 本来应该继续把 卷积神经网络算法写一下的 但是最近一直在踩 TensorFlow的坑.所以就先跳过算法介绍直接来应用场景,原谅我吧. TensorFlow 介绍 TF是g ...
- hexo博客谷歌百度收录踩坑日记
title: hexo博客谷歌百度收录踩坑日记 toc: false date: 2018-04-17 00:09:38 百度收录文件验证 无论怎么把渲染关掉或者render_skip都说我的格式错误 ...
- Hexo搭建静态博客踩坑日记(二)
前言 Hexo搭建静态博客踩坑日记(一), 我们说到利用Hexo快速搭建静态博客. 这节我们就来说一下主题的问题与主题的基本修改操作. 起步 chrome github hexo git node.j ...
- Hexo搭建静态博客踩坑日记(一)
前言 博客折腾一次就好, 找一个适合自己的博客平台, 专注于内容进行提升. 方式一: 自己买服务器, 域名, 写前端, 后端(前后分离最折腾, 不分离还好一点)... 方式二: 利用Hexo, Hug ...
- React Native Android配置部署踩坑日记
万事开头难 作为一只进入ECMAScript世界不久的菜鸟,已经被React Native的名气惊到了,开源一周数万星勾起了我浓烈的兴趣.新年新气象,来个HellWorld压压惊吧^_^(故意少打个' ...
- JavaScript 新手的踩坑日记
引语 在1995年5月,Eich 大神在10天内就写出了第一个脚本语言的版本,JavaScript 的第一个代号是 Mocha,Marc Andreesen 起的这个名字.由于商标问题以及很多产品已经 ...
- 人工智能(AI)库TensorFlow 踩坑日记之二
上次 踩坑日志之一 遗留的问题终于解决了,所以作者(也就是我)终于有脸出来写第二篇了. 首先还是贴上 卷积算法的示例代码地址 :https://github.com/tensorflow/models ...
- 微信小程序开发踩坑日记
2017.12.29 踩坑记录 引用图片名称不要使用中文,尽量使用中文命名,IDE中图片显示无异样,手机上图片可能出现不显示的情况. 2018.1.5 踩坑记录 微信小程序设置元素满屏,横向直接w ...
随机推荐
- 用PHPstudy nginx 配置tp6 隐藏访问链接中的index.php
在server 里面加上如下这个判断就好了 if (!-e $request_filename){ rewrite ^(.*)$ /index.php?s=$1 last; break; 配置在对应网 ...
- 28-PWA
const { resolve } = require('path'); const MiniCssExtractPlugin = require('mini-css-extract-plugin') ...
- 深入理解python虚拟机:程序执行的载体——栈帧
深入理解python虚拟机:程序执行的载体--栈帧 栈帧(Stack Frame)是 Python 虚拟机中程序执行的载体之一,也是 Python 中的一种执行上下文.每当 Python 执行一个函数 ...
- java-io FileInputStream文件拷贝
1.编写代码 main方法: public static void main(String[] args) throws IOException { String pathFileUrl =" ...
- Wine运行问题 希沃-汉字卡无法显示
在wine环境下运行希沃白板5,汉字卡无法正常显示: 这个模块是我之前参与开发的模块,过了5年还有点印象. 大概原理是获取字体库中字符的笔画路径GraphicsPath,根据里面的路径点集PathPo ...
- 2023-04-27:用go语言重写ffmpeg的remuxing.c示例。
2023-04-27:用go语言重写ffmpeg的remuxing.c示例. 答案2023-04-27: ffmpeg的remuxing.c是一个用于将多媒体文件从一种容器格式转换为另一种容器格式的命 ...
- 2020-10-10:OOM都有哪些,说出几种?
福哥答案2020-10-10:#福大大架构师每日一题# [答案参考了此链接:](https://cloud.tencent.com/developer/article/1480668) 本地方法栈:1 ...
- PyCharm-汉化、中文语言包、英文语言包、中英文切换
PyCharm的汉化是非常简单的,不需要繁琐的步骤,只需要到设置的插件中搜索你需要的语言包安装即可. 登录 进入项目(随便进入一个项目,新建也可以) File->settings->Plu ...
- DataGridView数据内容自适应列宽
数据自适应宽度某一列dataGridView1.Columns[@"列名"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCe ...
- CogSci 2017-Learning to reinforcement learn
Key 元学习系统(监督+从属)扩展于RL设置 LSTM用强化学习算法进行训练,可以使agent获得一定的学习适应能力 解决的主要问题 DRL受限于特定的领域 DRL训练需要大量的数据 作者参考了Ho ...