是否有任何方法可以检测到哪种颜色和哪种类型的Windows风格的Windows 10正在运行(我猜最新的一种具有浅色/深色主题-1903)
我有一个托盘图标应用程序,并希望根据主题显示黑色/白色图标。内置的应用程序可以正确显示它们,但我不知道如何检测到它。
答案 0 :(得分:2)
您可以从注册表中获取当前的主题信息:
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);
要获取当前的主题颜色(强调色),可以执行以下操作:
[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;