Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
Created September 6, 2012 08:33
Show Gist options
  • Save thomaslevesque/3653097 to your computer and use it in GitHub Desktop.
Save thomaslevesque/3653097 to your computer and use it in GitHub Desktop.
Get information about current Windows theme in C#
public class ThemeInfo
{
private readonly string _themeName;
private readonly string _themeColor;
private readonly string _themeSize;
private readonly string _themeFileName;
public ThemeInfo(string name, string fileName, string color, string size)
{
_themeName = name;
_themeFileName = fileName;
_themeColor = color;
_themeSize = size;
}
public string ThemeFileName
{
get { return _themeFileName; }
}
public string ThemeName
{
get { return _themeName; }
}
public string ThemeColor
{
get { return _themeColor; }
}
public string ThemeSize
{
get { return _themeSize; }
}
public static ThemeInfo Current
{
get
{
var fileName = new StringBuilder(260);
var color = new StringBuilder(260);
var size = new StringBuilder(260);
int hresult = GetCurrentThemeName(fileName, fileName.Capacity, color, color.Capacity, size, size.Capacity);
if (hresult < 0)
throw Marshal.GetExceptionForHR(hresult);
string themeName = Path.GetFileNameWithoutExtension(fileName.ToString());
return new ThemeInfo(themeName, fileName.ToString(), color.ToString(), size.ToString());
}
}
[DllImport("uxtheme", CharSet = CharSet.Auto)]
private static extern int GetCurrentThemeName(
StringBuilder pszThemeFileName,
int dwMaxNameChars,
StringBuilder pszColorBuff,
int cchMaxColorChars,
StringBuilder pszSizeBuff,
int cchMaxSizeChars
);
}
@EdwinHauspie
Copy link

ThemeColor is "NormalColor" .. what would you do with that? It's useless

@Galacticai
Copy link

Galacticai commented Jun 5, 2024

ThemeColor is "NormalColor" .. what would you do with that? It's useless

  • Option 1: Registry (accurate)
    • Good if you want the actual color that is selected in your personalization settings
public static (byte A, byte R, byte G, byte B)? GetDWMAccent() {
    try {
        const string registryKeyPath = @"Software\Microsoft\Windows\DWM";
        const string valueName = "AccentColor";
        using RegistryKey? key = Registry.CurrentUser.OpenSubKey(registryKeyPath);
        if (key is null) return null;
        object? color = key.GetValue(valueName);
        if (color is null) return null;

        // It is ABGR not ARGB because windows sucks
        // Map int to uint while preserving the value relative to the type
        uint abgr = unchecked((uint)(int)color);
        // split abgr (uint) into a,b,g,r (bytes)
        byte a = (byte)((abgr & 0xFF000000) >> 24);
        byte b = (byte)((abgr & 0x00FF0000) >> 16);
        byte g = (byte)((abgr & 0x0000FF00) >> 8);
        byte r = (byte)(abgr & 0x000000FF);
        return (a, r, g, b);
    } catch (Exception) { return null; }
}
  • Option 2:
    • Good for theming (lets you choose from several colors) BUT you don't get your current theme color for some reason (I tried all the UIColorTypes)
public static Windows.UI.Color GetAccentColor(UIColorType type = UIColorType.AccentDark1)
    => new UISettings().GetColorValue(type);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment