GetUserColorPreference (uxtheme)
Last changed: -151.56.233.32

.
Summary
GetUserColorPreference will get the current theme Accent color and the Start menu color through the IMMERSIVE_COLOR_PREFERENCE structure. Its counterpart is SetUserColorPreference

C# Signature:

    [DllImport("uxtheme.dll", EntryPoint = "#120")]
    static extern IntPtr GetUserColorPreference(ref IMMERSIVE_COLOR_PREFERENCE pcpPreference, bool fForceReload);

VB Signature:

Declare Function GetUserColorPreference Lib "uxtheme.dll" (TODO) As TODO

User-Defined Types:

    private struct IMMERSIVE_COLOR_PREFERENCE
    {
        public uint dwColorSetIndex;
        public uint crStartColor;
        public uint crAccentColor;
    }

Notes:

None.

Tips & Tricks:

To convert the uint color to the managed Color class and vice versa:

    private static uint ToUint(Color c)
    {
        return (uint)((c.B << 16) | (c.G << 8) | c.R);
    }

    private static Color ToColor(uint c)
    {
        int R = (int)(c & 0xFF) % 256;
        int G = (int)((c >> 8) & 0xFFFF) % 256;
        int B = (int)(c >> 16) % 256;
        return Color.FromArgb(R, G, B);
    }

When getting the accent color value, USE the crStartColor property, as crAccentColor might return 0, instead of the accent color.

Sample Code:

    public Color GetAccentColor()
    {
      IMMERSIVE_COLOR_PREFERENCE accent = new();
      GetUserColorPreference(ref accent, false);
      return ToColor(accent.crStartColor);

    }

Documentation