【问题标题】:Windows 10 taskbar color detection for tray icon托盘图标的 Windows 10 任务栏颜色检测
【发布时间】:2019-07-03 18:05:39
【问题描述】:

有什么方法可以检测正在运行的 windows 10 是哪种颜色和哪种类型的 windows 样式(我猜是最新的一个有浅色/深色主题 - 1903)

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

【问题讨论】:

    标签: c# windows


    【解决方案1】:

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

    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;
    

    【讨论】:

    • 感谢您让我走上这条道路!在我看来,Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\SystemUsesLightTheme 是我需要知道是使用浅色还是深色图标的确切键。
    • GetImmersiveColorTypeFromName() 不再在 UxTheme.dll 中导出。请改用 GetProcAddress(hUxTheme,MAKEINTRESOURCEA(96))。
    • 我想你从另一个页面复制了这段代码而没有理解它。无论这段代码应该做什么,它都不会返回任务栏的颜色,因此不会回答问题。
    猜你喜欢
    • 2017-06-07
    • 2016-04-14
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多