WPF 切换主题使用 luna 复古版本

本文告诉大家如何在 WPF 里面使用 luna 等复古主题

今天在 lsj 说他准备优化 WPF 的程序集时,准备删除 luna 等程序集时,找到了一段有趣的注释,发现在 WPF 里面可以通过一些有趣的反射的方法修改主题,让应用使用 luna 的主题,实现复古的界面

使用方法非常简单,在 App.xaml.cs 的构造函数里面,添加如下代码即可

        public App()
        {
            SetTheme("luna", "normalcolor");
        }

        ///
        /// Sets the WPF system theme.

        ///
        /// The name of the theme. (ie "aero")
        /// The name of the color. (ie "normalcolor")
        /// Copy https://github.com/danielmoore/SystemThemeChange/blob/master/App.xaml.cs
        public static void SetTheme(string themeName, string themeColor)
        {
            const BindingFlags staticNonPublic = BindingFlags.Static | BindingFlags.NonPublic;

            var presentationFrameworkAsm = Assembly.GetAssembly(typeof(Window));

            var themeWrapper = presentationFrameworkAsm.GetType("MS.Win32.UxThemeWrapper");

            var isActiveField = themeWrapper.GetField("_isActive", staticNonPublic);
            var themeColorField = themeWrapper.GetField("_themeColor", staticNonPublic);
            var themeNameField = themeWrapper.GetField("_themeName", staticNonPublic);

            // Set this to true so WPF doesn't default to classic.

            isActiveField.SetValue(null, true);

            themeColorField.SetValue(null, themeColor);
            themeNameField.SetValue(null, themeName);
        }

为了展示界面效果,我添加了如下界面


软件运行效果如下

WPF 切换主题使用 luna 复古版本

再换成 Aero 效果,修改代码如下

        public App()
        {
            SetTheme("areo", "normalcolor");
        }

可以看到界面更加好看

WPF 切换主题使用 luna 复古版本

以上方法实际上在很久之前就有大佬写了,详细请看 danielmoore/SystemThemeChange: A demonstration of a theme change helper for WPF

在 WPF 里面,可以看到 UxThemeWrapper 有如下注释

    internal static class UxThemeWrapper
    {
        // There are apps that override the system theme with one of their own
        // choosing, and intercept (and discard) WM_THEMECHANGED messages to
        // keep their theme in place even when the end-user selects a different
        // theme.   They do this using private reflection to assign values to
        // the three state variables.

        //
        // This state (i.e. where the three variables have values that differ from
        // the ones we choose) is unsupported.  So is the technique for getting
        // into that state (i.e. private reflection).  Nevertheless, .Net wants
        // to preserve some level of compatibility - at the very least, avoid
        // crashing.  [The apps use the result of GetField("_isActive") without
        // checking for null.]
        //
        // We do this in three steps:
        // 1) preserve the three fields;  this fixes the crashes.

        // 2) if the app overrides the values, use the overridden values
        //      in preference to ours.

        // 3) during WM_THEMECHANGED, restore the preference for our values.

        //      If the app overrides them again, step (2) will kick in.

        // Note that step (3) will never happen if the app is intercepting
        // WM_THEMECHANGED.

        private static bool    _isActive;
        private static string  _themeName;
        private static string  _themeColor;
    }

也就是以上的写法是符合预期的

本文以上的测试代码放在githubgitee 欢迎访问

可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码

git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin 3a6a955fdd761b3f45d9195abc241c70574413d3

以上使用的是 gitee 的源,如果 gitee 不能访问,请替换为 github 的源

git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git

获取代码之后,进入 BearweakiqeNurwhallcarnearcowar 文件夹

Original: https://www.cnblogs.com/lindexi/p/16733238.html
Author: lindexi
Title: WPF 切换主题使用 luna 复古版本

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/577724/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 你还有什么问题吗?

    在面试过程中,一般都会有一个固定环节,那就是在临近结束时,面试官会问求职者: 你还有什么问题吗? 其实,这是一个很好的了解公司,了解未来团队的机会,但很多求职者却不知道问什么,或者…

    Linux 2023年6月7日
    0110
  • 一篇文章扒掉“桥梁Handler”的底裤

    Android跨进程要掌握的是Binder, 而同一进程中最重要的应该就是Handler 消息通信机制了。我这么说,大家不知道是否认同,如果认同,还希望能给一个关注哈。 什么是Ha…

    Linux 2023年6月13日
    099
  • 字典服务的设计与管理

    编码问题,谁不想避其锋芒; 一、业务背景 在搜索引擎的功能上,曾经遇到过这样一个问题,数据库中某个公司名称中存在特殊编码,尽管数据已经正常同步到索引中,但是系统中关键词始终也无法匹…

    Linux 2023年6月14日
    083
  • Xbox分辨率突然变成640p

    今天XBox突然抽风还是发什么神经,输出分辨率突然变得非常模糊。一开始以为是HDMI线出现问题,后来用一条新的也是一样,所以就怀疑系统出了什么幺蛾子。 进入【电视和显示选项】——【…

    Linux 2023年6月13日
    0127
  • Fabric2.2中的Raft共识模块源码分析

    引言 Hyperledger Fabric是当前比较流行的一种联盟链系统,它隶属于Linux基金会在2015年创建的超级账本项目且是这个项目最重要的一个子项目。目前,与Hyperl…

    Linux 2023年6月7日
    076
  • python语法01

    在某.py文件中调用其他.py文件中的内容。全局变量的使用。线程的使用。if name == ‘ main‘: 的作用新建两个python脚本文件f1Fil…

    Linux 2023年6月14日
    086
  • 文件相关命令

    pwd指令 基本语法:pwd功能:显示当前工作的绝对目录 ls指令 基本语法:ls [选项][目录或者文件]常用选项 -a 显示所有文件及目录 (. 开头的隐藏文件也会列出) -l…

    Linux 2023年6月6日
    084
  • Linux 进程卡住了怎么办?

    在我们使用 Linux 系统时,如果网络或者磁盘等 I/O 出问题,会发现进程卡住了,即使用 kill -9 也无法杀掉进程,很多常用的调试工具,比如 strace, pstack…

    Linux 2023年5月27日
    086
  • 分区表、文件系统等计算机基础知识

    开机固件 BIOS UEFI 特别老的BIOS不支持GPT分区 目前已取代BIOS 分区表类型 MBR GPT 1.不支持2TB以上的硬盘2.主分区不能超过4个3.可以通过建立扩展…

    Linux 2023年6月7日
    0101
  • 高通平台如何避免误入FFBM模式

    前面两篇博客分别介绍了通过fastboot和QFIL工具退出FFBM模式的方法。虽然售后的同学可以这么指导用户做恢复,但步骤多操作也麻烦,且属于事后处理,如果大面积高概率地出现,会…

    Linux 2023年6月7日
    098
  • vue-admin-template组件前端,登录验证成功后,getInfo方法不调用

    先查看request.js中自定义状态码是否为自己的定义的成功状态码 这里的状态码该为自己的成功状态码 Original: https://www.cnblogs.com/antl…

    Linux 2023年6月7日
    0108
  • NGINX压力测试

    Nginx可以作为HTTP服务器和反向代理服务器。反向代理服务器取决于后端服务器的性能,这次只针对HTTP服务器做性能测试。Nginx作为服务器对于网络的性能必然是非常依赖的,尤其…

    Linux 2023年6月14日
    0102
  • 高级IPC DBus

    404. 抱歉,您访问的资源不存在。 可能是URL不正确,或者对应的内容已经被删除,或者处于隐私状态。 [En] It may be that the URL is incorre…

    Linux 2023年5月27日
    0106
  • Spring常见异常说明

    文章要点 Spring bean 的声明方式 Spring bean 的注入规则 Spring bean 的依赖查找规则 Spring bean 的名称定义方式和默认名称规则 XX…

    Linux 2023年6月6日
    096
  • 音频属性

    采样频率就是采用一段音频,做为样本,因为wav使用的是数码信号,它是用一堆数字来描述原来的模拟信号,所以它要对原来的模拟信号进行分析,我们知道所有的声音都有其波形,数码信号就是在原…

    Linux 2023年6月8日
    0111
  • MAC安装redis

    一、安装命令使用mac的包管理工具brew一行命令搞定安装。若未安装brew,命令行先输入以下命令安装brew。 /usr/bin/ruby -e “$(curl -f…

    Linux 2023年5月28日
    099
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球