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)

大家都在看

  • MybatisPlus拓展——实现多数据源操作

    多数据源 适用:一般工作时候会有多个数据库,每个库对应不同的业务数据。程序如果每次数据都访问同一个数据库,该数据库压力很大访问会很慢。 1、导入依赖 com.baomidou dy…

    Linux 2023年6月7日
    080
  • win11下配置vue3版本

    安装node.js PS:全局需要使用管理员权限打开CMD** 下载nodejs的地址 选择左边就好 下载安装后,选择自己需要安装的盘符,即可,不再叙述。 打开CMD查看node是…

    Linux 2023年6月14日
    0105
  • VRRP配置即实验

    VRRP 概念: VRRP 全称是虚拟路由器冗余协议,它是一种容错协议。该协议通过把几台路由设备联合组成一台虚拟的路由设备,该虚拟路由器在本地局域网拥有唯一的一个虚拟ID和虚拟IP…

    Linux 2023年6月6日
    083
  • 【设计模式】Java设计模式-模板模式

    Java设计模式 – 模板模式 😄 不断学习才是王道🔥 继续踏上学习之路,学之分享笔记👊 总有一天我也能像各位大佬一样🏆原创作品,更多关注我CSDN: 一个有梦有戏的人…

    Linux 2023年6月6日
    0151
  • Redis 通过key前缀获取所有key的值

    Redis 通过key前缀获取所有key的值 public void getRedis(String cardId) { // 获取所有的key Set keys = redisT…

    Linux 2023年5月28日
    074
  • Shell 第二章《流控》

    前言 无论什么编程语言都离不开条件判断(流控)。SHELL也不例外。例如,用户输入的密码不够长时提示用户,你太短了例如,用户输入了备份的目录,如果有目录继续备份,如果没有目录创建目…

    Linux 2023年6月6日
    0146
  • [转] OSDI, SOSP与美国著名计算机系的调查报告

    看到一个很久之前的文章,重新排版后转发一下,希望能带来一些帮助;文章有时效性,出现的数据多为历史数据。资源来源自网络,侵删。 序言 按照USnews的分类,Computer Sci…

    Linux 2023年6月13日
    0105
  • 运维故障收集-考勤机无法连接考勤机网关系统故障验证流程

    博客园 :当前访问的博文已被密码保护 请输入阅读密码: Original: https://www.cnblogs.com/linuxshare/p/16474967.htmlAu…

    Linux 2023年6月6日
    097
  • 庐山真面目之十三微服务架构中如何在Docker上使用Redis缓存

    一、介绍 1、开始说明在微服务器架构中,有一个组件是不能少的,那就是缓存组件。其实来说,缓存组件,这个叫法不是完全正确,因为除了缓存功能,它还能完成其他很多功能。我就不隐瞒了,今天…

    Linux 2023年5月28日
    076
  • Go-channel

    (1)channel本质就是一个数据结构——队列 (2)数据先进先出 (3)线程安全,多goroutine访问时,不需要加锁,channel本身就是线程安全的 (4)channel…

    Linux 2023年6月8日
    0105
  • linux系统(centos)下kvm虚拟化用命令行给虚拟机添加硬盘

    linux系统(centos)下kvm虚拟化用命令行给虚拟机添加硬盘 背景 公司有用单台服务器使用kvm装虚拟机,利用webvirtmgr进行界面管理。当虚拟机创建时固定硬盘后,不…

    Linux 2023年6月8日
    0106
  • 01-MySQL连接查询、聚合函数

    1、连接查询 1.1、左连接 以左表为基准进行查询,左表数据回全部显示出来 右表中如果匹配连接条件的数据则显示相应字段的数据,如果不匹配,则显示为NULL 1.2、右连接 以右表为…

    Linux 2023年6月7日
    0128
  • sql server 增删改(查太多了)

    delete(删除) 使用 delete语句删除表中数据。 delete from 表名 [where where_definition] 如果不使用where子句,将删除表中所有…

    Linux 2023年6月7日
    087
  • python中的反射

    python反射简介 所谓反射是指通过字符串的方式获取对象,然后执行对象的属性或方法。在python中一切皆对象,因此我们可以对一切事物进行发射。 关于反射python为我们提供了…

    Linux 2023年6月7日
    0100
  • 高等代数: 2 行列式

    2 行列式 1、n个不同的自然数的一个全排列称为一个n元排列。 2、顺序、逆序、逆序数:τ(abcd…)(读音:tao)、奇排列、偶排列、对换(a,b) 3、定理1:对…

    Linux 2023年6月8日
    0147
  • 写给初学者的Linux errno 错误码机制

    不同于Java的异常处理机制, 当你使用C更多的接触到是基于错误码的异常机制, 简单来说就是当调用的函数发生异常时, 程序不会跳转到一个统一处理异常的地方, 取而代之的是返回一个整…

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