/
AeroThemePolicy.cs
182 lines (167 loc) · 7.2 KB
/
AeroThemePolicy.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
namespace PresentationTheme.Aero
{
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
/// <summary>
/// Provides the default Aero theme policy which chooses the appropriate
/// theme resources depending on the current system theme and Windows version.
/// </summary>
/// <remarks>
/// <para>
/// Use the policy by passing <see cref="GetCurrentThemeUri"/> to
/// <see cref="ThemeManager.SetPresentationFrameworkTheme(IThemePolicy)"/>
/// or <see cref="ThemeManager.SetTheme(Assembly,IThemePolicy)"/>.
/// </para>
/// <para>
/// The policy chooses the following theme resource assemblies:
/// <list type="table">
/// <item>
/// <term>Windows 8 with Aero theme</term>
/// <description><c>PresentationTheme.Aero.Win8.dll</c></description>
/// </item>
/// <item>
/// <term>Windows 8/8.1 with AeroLite theme</term>
/// <description><c>PresentationTheme.AeroLite.Win8.dll</c></description>
/// </item>
/// <item>
/// <term>Windows 8/8.1 in high contrast mode</term>
/// <description>
/// <c>PresentationTheme.HighContrast.Win8.dll</c>
/// </description>
/// </item>
/// <item>
/// <term>Windows 10 with Aero theme</term>
/// <description><c>PresentationTheme.Aero.Win10.dll</c></description>
/// </item>
/// <item>
/// <term>Windows 10 with AeroLite theme</term>
/// <description><c>PresentationTheme.AeroLite.Win10.dll</c></description>
/// </item>
/// <item>
/// <term>Windows 10 in high contrast mode</term>
/// <description>
/// <c>PresentationTheme.HighContrast.Win10.dll</c>
/// </description>
/// </item>
/// <item>
/// <term>Other Windows versions</term>
/// <description>Fallback to default theme</description>
/// </item>
/// </list>
/// </para>
/// </remarks>
public class AeroThemePolicy : IThemePolicy
{
bool IThemePolicy.MergeWithBaseResources => true;
/// <summary>
/// Builds the pack <see cref="Uri"/> for the theme resources matching
/// the current system theme and Windows version.
/// </summary>
/// <returns>
/// An absolute pack <see cref="Uri"/> to a <see cref="ResourceDictionary"/>
/// with the theme resources. Returns <see langword="null"/> if the
/// current system theme or Windows version are not supported.
/// </returns>
public virtual Uri GetCurrentThemeUri()
{
var osVersion = GetRealWindowsVersion();
string uxThemeName = SystemParameters.UxThemeName;
string uxThemeColor = SystemParameters.UxThemeColor;
bool highContrast = SystemParameters.HighContrast;
string resourceName = GetThemeResourceName(osVersion, uxThemeName, uxThemeColor, highContrast);
if (resourceName == null)
return null;
string partialName = $"PresentationTheme.{resourceName}";
var asmName = GetFullAssemblyNameFromPartialName(typeof(AeroTheme).Assembly, partialName);
string themedResourceName =
"themes/" + resourceName.ToLowerInvariant() + "." + uxThemeColor.ToLowerInvariant() + ".xaml";
return PackUriUtils.MakeContentPackUri(asmName, themedResourceName);
}
/// <summary>
/// Gets theme resource name matching the current system theme and
/// Windows version.
/// </summary>
/// <param name="osVersion">The Windows version.</param>
/// <param name="themeName">The system theme name.</param>
/// <param name="themeColor">The system theme color.</param>
/// <param name="highContrast">
/// Indicates whether Windows is in high contrast mode.
/// </param>
/// <returns>
/// An absolute pack <see cref="Uri"/> to a <see cref="ResourceDictionary"/>
/// with the theme resources.
/// </returns>
/// <returns>
/// The theme resource name. Returns <see langword="null"/> if the
/// current system theme or Windows version are not supported.
/// </returns>
protected virtual string GetThemeResourceName(
Version osVersion, string themeName, string themeColor, bool highContrast)
{
var win7 = new Version(6, 1);
var win8 = new Version(6, 2);
var win10 = new Version(10, 0);
if (string.Equals(themeName, "Aero", StringComparison.OrdinalIgnoreCase)) {
if (osVersion >= win10)
return "Aero.Win10";
if(osVersion >= win8)
return "Aero.Win8";
} else if (string.Equals(themeName, "AeroLite", StringComparison.OrdinalIgnoreCase)) {
if (highContrast) {
if (osVersion >= win10)
return "HighContrast.Win10";
if (osVersion >= win8)
return "HighContrast.Win8";
} else {
if (osVersion >= win10)
return "AeroLite.Win10";
if (osVersion >= win8)
return "AeroLite.Win8";
}
}
return null;
}
private static AssemblyName GetFullAssemblyNameFromPartialName(
Assembly protoAssembly, string partialName)
{
return new AssemblyName(protoAssembly.FullName) {
Name = partialName
};
}
[StructLayout(LayoutKind.Sequential)]
private struct RTL_OSVERSIONINFOEXW
{
public uint dwOSVersionInfoSize;
public uint dwMajorVersion;
public uint dwMinorVersion;
public uint dwBuildNumber;
public uint dwPlatformId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x80)]
public string szCSDVersion;
public ushort wServicePackMajor;
public ushort wServicePackMinor;
public ushort wSuiteMask;
public byte wProductType;
public byte wReserved;
}
internal static Version GetRealWindowsVersion()
{
var version = new RTL_OSVERSIONINFOEXW {
dwOSVersionInfoSize = (uint)Marshal.SizeOf(typeof(RTL_OSVERSIONINFOEXW))
};
// RtlGetVersion does not require a manifest to detect Windows 10+.
int st = RtlGetVersion(ref version);
if (st != 0)
throw Marshal.GetExceptionForHR(st);
return new Version(
(int)version.dwMajorVersion,
(int)version.dwMinorVersion,
(int)version.dwBuildNumber,
(version.wServicePackMajor << 16) | version.wServicePackMinor);
}
[DllImport("ntdll", CharSet = CharSet.Auto)]
private static extern int RtlGetVersion(ref RTL_OSVERSIONINFOEXW lpVersionInformation);
}
}