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

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

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

        public App()
{
SetTheme("luna", "normalcolor");
} /// <summary>
/// Sets the WPF system theme.
/// </summary>
/// <param name="themeName">The name of the theme. (ie "aero")</param>
/// <param name="themeColor">The name of the color. (ie "normalcolor")</param>
/// 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);
}

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

    <Grid>
<TextBox Width="100" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBox>
<Button Margin="100,200,10,10" Height="100" Width="100"></Button>
</Grid>

软件运行效果如下

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

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

可以看到界面更加好看

以上方法实际上在很久之前就有大佬写了,详细请看 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 文件夹

WPF 切换主题使用 luna 复古版本的更多相关文章

  1. iOS 实现快速切换主题详细教程(附上源码)

    前言 iOS 实现主题切换,相信在未来的app里也是会频繁出现的,尽管现在只是出现在主流的APP,如(QQ.新浪微博.酷狗音乐.网易云音乐等),但是现在是看颜值.追求个性的年代,所以根据用户喜好自定义 ...

  2. 【ExtJs】使用Cookie、切换主题和语言

    转自:http://witmax.cn/extjs-cookie-theme-lang.html 使用Cookie:   1 2 3 Ext.state.Manager.setProvider(new ...

  3. LNMP下wordpress无法切换主题,只显示当前主题解决方法

    最近在lnmp下发现wordpress后台无法切换主题,只能显示当前主题,开始还以为是文件没传完,又重置了一遍,还是一样.百度得知,原来军哥的LNMP安装包默认关闭了scandir函数,为了安全考虑. ...

  4. Android 实现切换主题皮肤功能(类似于众多app中的 夜间模式,主题包等)

    首先来个最简单的一键切换主题功能,就做个白天和晚上的主题好了. 先看我们的styles文件: <resources> <!-- Base application theme. --& ...

  5. laravel4通过控制视图模板路劲来动态切换主题

    通过控制视图模板路劲来动态切换主题 App::before(function($request) { $paths = Terminal::isMobile() ? array(__dir__.'/v ...

  6. win10 uwp 切换主题

    本文主要说如何在UWP切换主题,并且如何制作主题. 一般我们的应用都要有多种颜色,一种是正常的白天颜色,一种是晚上的黑夜颜色,还需要一种辅助的高对比颜色.这是微软建议的,一般应用都要包含的颜色. 我们 ...

  7. WPF实现主题更换的简单DEMO

    WPF实现主题更换的简单DEMO 实现主题更换功能主要是三个知识点: 动态资源 ( DynamicResource ) INotifyPropertyChanged 接口 界面元素与数据模型的绑定 ( ...

  8. ionic动态切换主题皮肤

    本来想通过css自定义属性值: :root{ --red:red; } p{ color:var(--red); } div{ background:var(--red); } 在ionic2设置动态 ...

  9. jQuery实现无刷新切换主题皮肤功能

    主题皮肤切换功能在很多网站和系统中应用,用户可以根据此功能设置自己喜欢的主题颜色风格,增强了用户体验.本文将围绕如何使用jQuery实现点击无刷新切换主题皮肤功能. 查看演示DEMO:https:// ...

  10. Thinkphp动态切换主题

    'DEFAULT_THEME' => '2014', 'TMPL_DETECT_THEME' => true, // 自动侦测模板主题 'THEME_LIST' => '2012,2 ...

随机推荐

  1. Android线程池封装库

    目录介绍 1.遇到的问题和需求 1.1 遇到的问题有哪些 1.2 遇到的需求 1.3 多线程通过实现Runnable弊端 1.4 为什么要用线程池 2.封装库具有的功能 2.1 常用的功能 3.封装库 ...

  2. nohup操作

    nohup操作 背景:vscode 连接服务器远程运行时经常会出现断连需要重新输入密码,如果是在终端跑程序那么中断后无法再恢复在终端的输出,为了能够让程序保留实验结果或需要长时间跑程序而无需值守,需要 ...

  3. 【Nginx】如何使用自签CA配置HTTPS加密反向代理访问?看了这篇我会了!!

    写在前面 随着互联网的发展,很多公司和个人越来越重视网络的安全性,越来越多的公司采用HTTPS协议来代替了HTTP协议.为何说HTTPS协议比HTTP协议安全呢?小伙伴们自行百度吧!我就不说了.今天, ...

  4. ‘MsBuild.exe‘ 不是内部或外部命令,也不是可运行的程序

    方法一: 在系统环境变量中的path变量中添加一条路径: 32位环境   C:\Windows\Microsoft.NET\Framework\v4.0.30319  64位环境   C:\Windo ...

  5. 《HelloGitHub》第 96 期

    兴趣是最好的老师,HelloGitHub 让你对编程感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. https://github.com/521xueweiha ...

  6. OpenMP优化for循环的基础运用

    OpenMP优化for循环的基础运用 OpenMP作为多线程并行优化API,其使用方式与C++自带的多线程使用方式有很大的不同. 在使用OpenMP时,我们是通过 #pragma omp+字句 所组成 ...

  7. #树状数组,dp#SGU 521 North-East

    题目 在平面上有 \(n\) 个点,现在有一个人要从某个点出发, 每次只能到达横纵坐标都超过原坐标的点,也就是 \(x_j<x_i,y_j<y_i\) 如果他要经过最多的点,那么哪些点是可 ...

  8. 基于新版宝塔Docker部署在线客服系统过程小记

    我在业余时间开发维护了一款免费开源的升讯威在线客服系统,也收获了许多用户.对我来说,只要能获得用户的认可,就是我最大的动力. 客服系统开发过程中,最让我意外的是对 TCP/IP 协议的认识.过去一直认 ...

  9. 3.1版本【HarmonyOS 第一课】正式上线!参与学习赢官方好礼>>

      [课程介绍] <HarmonyOS第一课>是跟随版本迭代不断推出的系列化课程,本期课程基于HarmonyOS 3.1版本的新技术和特性,每个课程单元里面都包含视频.Codelab.文章 ...

  10. Excel 表间关联运算的示例

    用 Excel 处理数据时,经常会涉及到多页 sheet 数据之间的关联运算需求,用 vlookup 可以完成部分简单关联,但较复杂的情况时仍然不太方便,常常需要多次操作才能完成.另外,当要做关联的文 ...