locked
How to get the Computer's Accent Color RRS feed

  • Question

  • Hello,

    I am making a custom title bar and I want to make sure it is the same color as the accent color.  So I was wondering if there was anyway to get the computer's accent color.

    Thx :P


    I am not tasty

    Tuesday, January 16, 2018 8:32 PM

Answers

  • How would i make that a panel's fore color


    You can use Immersive APIs =>

    Dim colorSystemAccent As UInteger = GetImmersiveColorFromColorSetEx(GetImmersiveUserColorSetPreference(False, False), GetImmersiveColorTypeFromName(Marshal.StringToHGlobalUni("ImmersiveSystemAccent")), False, 0)
    Dim colorAccent As System.Drawing.Color = System.Drawing.Color.FromArgb((&HFF000000 And colorSystemAccent) >> 24, &HFF And colorSystemAccent, (&HFF00 And colorSystemAccent) >> 8, (&HFF0000 And colorSystemAccent) >> 16)
    ' test back color
    Me.BackColor = colorAccent

    Declarations =>

    <DllImport("Uxtheme.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="#95")>
        Public Shared Function GetImmersiveColorFromColorSetEx(ByVal dwImmersiveColorSet As UInteger, ByVal dwImmersiveColorType As UInteger, ByVal bIgnoreHighContrast As Boolean, ByVal dwHighContrastCacheMode As UInteger) As UInteger
        End Function
    <DllImport("Uxtheme.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="#96")>
        Public Shared Function GetImmersiveColorTypeFromName(ByVal pName As IntPtr) As UInteger
        End Function
    <DllImport("Uxtheme.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="#98")>
        Public Shared Function GetImmersiveUserColorSetPreference(ByVal bForceCheckRegistry As Boolean, ByVal bSkipCheckOnFail As Boolean) As UInteger
        End Function


    • Edited by Castorix31 Tuesday, January 16, 2018 10:42 PM
    • Proposed as answer by Reed KimbleMVP Tuesday, January 16, 2018 11:06 PM
    • Marked as answer by Reed KimbleMVP Wednesday, January 17, 2018 6:25 PM
    Tuesday, January 16, 2018 10:39 PM
  • No, Accent color is not a system color (from GetSysColor())

    It is stored in the registry.

    (HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM)



    • Edited by Castorix31 Tuesday, January 16, 2018 9:27 PM
    • Proposed as answer by Reed KimbleMVP Tuesday, January 16, 2018 11:00 PM
    • Marked as answer by Snowcone_65 Wednesday, January 17, 2018 3:53 PM
    Tuesday, January 16, 2018 9:21 PM

All replies

  • I am making a custom title bar and I want to make sure it is the same color as the accent color.  So I was wondering if there was anyway to get the computer's accent color.

    Is it one of these: https://msdn.microsoft.com/en-us/library/system.drawing.systemcolors%28v=vs.110%29.aspx

    • Proposed as answer by Reed KimbleMVP Tuesday, January 16, 2018 8:38 PM
    • Unproposed as answer by Reed KimbleMVP Tuesday, January 16, 2018 11:00 PM
    Tuesday, January 16, 2018 8:35 PM
  • Not quite what I am looking for.


    I am not tasty

    Tuesday, January 16, 2018 9:01 PM
  • Not quite what I am looking for.


    I am not tasty

    Yes, that is the list of system colors assigned at the Windows level.  That is where all forms and controls get their default colors from.  What you are calling "accent color" is probably known by another name (possibly GradientActiveCaption?).

    The usage of each color is listed in the documentation.

    https://docs.microsoft.com/en-us/dotnet/api/system.drawing.systemcolors?view=netframework-4.7.1


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, January 16, 2018 9:06 PM
  • No, Accent color is not a system color (from GetSysColor())

    It is stored in the registry.

    (HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM)



    • Edited by Castorix31 Tuesday, January 16, 2018 9:27 PM
    • Proposed as answer by Reed KimbleMVP Tuesday, January 16, 2018 11:00 PM
    • Marked as answer by Snowcone_65 Wednesday, January 17, 2018 3:53 PM
    Tuesday, January 16, 2018 9:21 PM
  • No, Accent color is not a system color (from GetSysColor())

    It is stored in the registry.

    (HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM)

    How would i make that a panel's fore color


    I am not tasty

    Tuesday, January 16, 2018 9:25 PM
  • How would i make that a panel's fore color


    You can use Immersive APIs =>

    Dim colorSystemAccent As UInteger = GetImmersiveColorFromColorSetEx(GetImmersiveUserColorSetPreference(False, False), GetImmersiveColorTypeFromName(Marshal.StringToHGlobalUni("ImmersiveSystemAccent")), False, 0)
    Dim colorAccent As System.Drawing.Color = System.Drawing.Color.FromArgb((&HFF000000 And colorSystemAccent) >> 24, &HFF And colorSystemAccent, (&HFF00 And colorSystemAccent) >> 8, (&HFF0000 And colorSystemAccent) >> 16)
    ' test back color
    Me.BackColor = colorAccent

    Declarations =>

    <DllImport("Uxtheme.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="#95")>
        Public Shared Function GetImmersiveColorFromColorSetEx(ByVal dwImmersiveColorSet As UInteger, ByVal dwImmersiveColorType As UInteger, ByVal bIgnoreHighContrast As Boolean, ByVal dwHighContrastCacheMode As UInteger) As UInteger
        End Function
    <DllImport("Uxtheme.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="#96")>
        Public Shared Function GetImmersiveColorTypeFromName(ByVal pName As IntPtr) As UInteger
        End Function
    <DllImport("Uxtheme.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="#98")>
        Public Shared Function GetImmersiveUserColorSetPreference(ByVal bForceCheckRegistry As Boolean, ByVal bSkipCheckOnFail As Boolean) As UInteger
        End Function


    • Edited by Castorix31 Tuesday, January 16, 2018 10:42 PM
    • Proposed as answer by Reed KimbleMVP Tuesday, January 16, 2018 11:06 PM
    • Marked as answer by Reed KimbleMVP Wednesday, January 17, 2018 6:25 PM
    Tuesday, January 16, 2018 10:39 PM
  • How would i make that a panel's fore color


    You can use Immersive APIs =>

    Dim colorSystemAccent As UInteger = GetImmersiveColorFromColorSetEx(GetImmersiveUserColorSetPreference(False, False), GetImmersiveColorTypeFromName(Marshal.StringToHGlobalUni("ImmersiveSystemAccent")), False, 0)
    Dim colorAccent As System.Drawing.Color = System.Drawing.Color.FromArgb((&HFF000000 And colorSystemAccent) >> 24, &HFF And colorSystemAccent, (&HFF00 And colorSystemAccent) >> 8, (&HFF0000 And colorSystemAccent) >> 16)
    ' test back color
    Me.BackColor = colorAccent

    Declarations =>

    <DllImport("Uxtheme.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="#95")>
        Public Shared Function GetImmersiveColorFromColorSetEx(ByVal dwImmersiveColorSet As UInteger, ByVal dwImmersiveColorType As UInteger, ByVal bIgnoreHighContrast As Boolean, ByVal dwHighContrastCacheMode As UInteger) As UInteger
        End Function
    <DllImport("Uxtheme.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="#96")>
        Public Shared Function GetImmersiveColorTypeFromName(ByVal pName As IntPtr) As UInteger
        End Function
    <DllImport("Uxtheme.dll", SetLastError:=True, CharSet:=CharSet.Auto, EntryPoint:="#98")>
        Public Shared Function GetImmersiveUserColorSetPreference(ByVal bForceCheckRegistry As Boolean, ByVal bSkipCheckOnFail As Boolean) As UInteger
        End Function


    Not working.


    I am not tasty

    • Marked as answer by Snowcone_65 Wednesday, January 17, 2018 3:52 PM
    • Unmarked as answer by Reed KimbleMVP Wednesday, January 17, 2018 6:25 PM
    Wednesday, January 17, 2018 2:33 AM
  • Hi Snowcone_65,

    From this page, you can also see several other color settings there, such as "AccentColor" which is probably what you're looking for.

    Hope it is helpful to you.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 17, 2018 6:48 AM
  • Not working.

    "Not working" doesn't help...

    Is there an error ? at which line ?

     I tested on Windows 10 only (those colors did not exist on old OS) and it works on my PC.

    Or if you don't want to use Immersive APIs, you can just read the color in the registry (HKEY_CURRENT_USER\Software\Microsoft \Windows\DWM\AccentColor) and convert it the same way from ARGB into a System.Drawing.Color



    • Edited by Castorix31 Wednesday, January 17, 2018 8:21 AM
    Wednesday, January 17, 2018 8:13 AM
  • Ok so convert it like this

    Dim AccentColor As System.Drawing.Color = System.Drawing.Color.FromArgb("HKEY_CURRENT_USER\Software\Microsoft \Windows\DWM\AccentColor")


    I am not tasty

    Wednesday, January 17, 2018 2:59 PM
  • Not working.

    "Not working" doesn't help...

    Is there an error ? at which line ?

     I tested on Windows 10 only (those colors did not exist on old OS) and it works on my PC.

    Or if you don't want to use Immersive APIs, you can just read the color in the registry (HKEY_CURRENT_USER\Software\Microsoft \Windows\DWM\AccentColor) and convert it the same way from ARGB into a System.Drawing.Color



    Yes there is an error for the DLLImport lines


    I am not tasty

    Wednesday, January 17, 2018 3:00 PM
  • Yes there is an error for the DLLImport lines

    Add at beginning :

    Imports System.Runtime.InteropServices

    Wednesday, January 17, 2018 3:09 PM
  • Yes there is an error for the DLLImport lines

    Add at beginning :

    Imports System.Runtime.InteropServices

    worked thx


    I am not tasty

    Wednesday, January 17, 2018 3:48 PM
  • I know I'm replying to an old thread, but this one just stymied me too, and none of the solutions I found online really worked for my particular situation - a Winforms project, in which I'm trying to minimize the number of undocumented calls.

    In the end I used Castorix31's suggestion, and pulled the value from the Registry:

    // Previously defined: Color _accent = System.Drawing.SystemColors.ActiveBorder;
    
    try
       {
           using (RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\DWM"))
               {
                  if (key != null)
                        {
                            Object o = key.GetValue("AccentColor");
                            if (o != null)
                            {
                                _accent = Color.FromArgb(getARGB((int)o));
                            }
                        }
                    }
                }
                catch (Exception ex)  
                {
                   //couldn't read color - not a problem in this case, we already defined a default color
                }

    Be aware though that the value stored there is in ABGR format (Alpha, Blue, Green, Red) - not the ARGB that is preferred by most Windows color functions. Specifically, Color.FromArgb(int i) - so i had to do some bit-swapping:

    private int getARGB(int iABGR)
            {
                return
                ((iABGR >> 24) << 24) |          // alpha value stays where it is (top 8 bits)
                ((iABGR >> 16) & 0xFF) |         // red value moves to the next 8 bits
                ((iABGR >> 8) & 0xFF) << 8 |     // green value stays where it is
                ((iABGR) & 0xFF) << 16;          // blue value in the lowest 8 bits
            }

    If you don't, then the color you end up with is invariably of a shade politely described as "manure".

    Anyway, this is certainly not the neatest answer, BUT it worked for my specific situation and I thought I'd share it.


    • Edited by Threap Tuesday, April 9, 2019 6:18 AM removed irrelevant line
    Tuesday, April 9, 2019 6:17 AM