WPF 切换主题使用 luna 复古版本
WPF 版本 切换 主题 使用
2023-09-27 14:28:48 时间
本文告诉大家如何在 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;
}
也就是以上的写法是符合预期的
本文以上的测试代码放在github 和 gitee 欢迎访问
可以通过如下方式获取本文的源代码,先创建一个空文件夹,接着使用命令行 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 文件夹
相关文章
- Comparison: Standard WPF DataGrid and DevExpress WPF GridControl
- WPF binding not updating the view
- 【Wpf】设置Icon路径
- WPF 自定义控件 Loaded 导致的设计器异常 解决方案
- [WPF] 使用 MVVM Toolkit 构建 MVVM 程序
- [WPF 自定义控件]了解WPF的布局过程,并利用Measure为Expander添加动画
- wpf中:xaml中的命名空间的引入方法
- WPF整理-使用用户选择主题的颜色和字体
- C#界面wpf简介
- 力挺8天入门wpf【转载】
- CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)
- 【C#/WPF】限制GridSplitter分隔栏的滑动范围
- 【C#/WPF】修改图像的DPI、Resolution
- 【WPF】BusyIndicator做Loading遮罩层
- 【WPF】TabControl垂直分页栏/选项卡