【发布时间】:2019-07-03 18:05:39
【问题描述】:
有什么方法可以检测正在运行的 windows 10 是哪种颜色和哪种类型的 windows 样式(我猜是最新的一个有浅色/深色主题 - 1903)
我有一个托盘图标应用程序,想根据主题显示一个黑/白图标。内置应用程序可以正确显示它们,但我不知道如何检测它。
【问题讨论】:
有什么方法可以检测正在运行的 windows 10 是哪种颜色和哪种类型的 windows 样式(我猜是最新的一个有浅色/深色主题 - 1903)
我有一个托盘图标应用程序,想根据主题显示一个黑/白图标。内置应用程序可以正确显示它们,但我不知道如何检测它。
【问题讨论】:
您可以从注册表中获取当前的主题信息:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes
(GetCurrentThemeName api 在我的 Windows 10 操作系统上返回 InstallVisualStyle 值)
声明:
[DllImport("UxTheme.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetCurrentThemeName(StringBuilder pszThemeFileName, int cchMaxNameChars, StringBuilder pszColorBuff, int cchMaxColorChars, StringBuilder pszSizeBuff, int cchMaxSizeChars);
要获取当前的 Theme 颜色(Accent color),你可以这样做:
[DllImport("Uxtheme.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "#95")]
public static extern int GetImmersiveColorFromColorSetEx(int dwImmersiveColorSet, int dwImmersiveColorType, bool bIgnoreHighContrast, int dwHighContrastCacheMode);
[DllImport("Uxtheme.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "#96")]
public static extern int GetImmersiveColorTypeFromName(IntPtr pName);
[DllImport("Uxtheme.dll", SetLastError = true, CharSet = CharSet.Auto, EntryPoint = "#98")]
public static extern int GetImmersiveUserColorSetPreference(bool bForceCheckRegistry, bool bSkipCheckOnFail);
int nColorSystemAccent = GetImmersiveColorFromColorSetEx(GetImmersiveUserColorSetPreference(false, false), GetImmersiveColorTypeFromName(Marshal.StringToHGlobalUni("ImmersiveSystemAccent")), false, 0);
System.Drawing.Color colorSystemAccent = ColorTranslator.FromWin32(nColorSystemAccent);
// Test color
this.BackColor = colorSystemAccent;
【讨论】: