Windows 10 任务栏颜色检测托盘图标

Mla*_*vic 6 c# windows

有没有办法检测 Windows 10 正在运行哪种颜色和哪种类型的 Windows 风格(我猜最新的有浅色/深色主题 - 1903)

我有一个托盘图标应用程序,想根据主题显示黑/白图标。内置应用程序可以正确显示它们,但我不知道如何检测它。

Cas*_*rix 4

您可以从注册表获取当前主题信息:

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);
Run Code Online (Sandbox Code Playgroud)

要获取当前的主题颜色(强调颜色),您可以执行以下操作:

[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;
Run Code Online (Sandbox Code Playgroud)

  • 感谢您让我走上这条路!在我看来,Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\SystemUsesLightTheme 是我需要知道是否使用浅色或深色图标的确切键。 (2认同)