14

I'm writing a custom Button control as part of a (soon to be) free Control suite, and I would like to base my (default) Control colors on the corresponding Windows System colors. So, after looking up "default windows system colors" online I could not find information on the System Colors for Windows controls (especially not Button controls).

Is there a way to get this color information (e.g. Button Border Color, Button Highlight Color, Button Hover Color, Button Clicked Background Color, etc) in .NET?

jay_t55
  • 11,362
  • 28
  • 103
  • 174
  • 4
    Take a look at properties in `SystemColors` class – Sriram Sakthivel Sep 01 '14 at 10:21
  • 3
    Note that `SystemColors` corresponds mostly to color elements seen in the "Classic Windows" look - discontinued since Windows 8. Also note that there is no straightforward way to get the current Aero Glass / Window Color in the .NET Framework without P/Invoke. I think current fashion is trending away from allowing the user to customize their environment in this way. – Dai Sep 01 '14 at 10:24
  • @Dai: I totally agree (with tears in my eyes), "current fashion is trending away from allowing the user to customize their environment" - this should be formatted as bold text! – O. R. Mapper Sep 01 '14 at 10:26
  • 1
    I should have noted: I have already checked out the `SystemColors` class but I think it is incomplete. Maybe I'm wrong but I don't see anything for Button colors, etc. – jay_t55 Sep 01 '14 at 11:09
  • are you using WPF or WinForms ? If it's WPF you can see the used `SystemColors` if you're creating the DefaultTemplate the Background Property for example for a Button has `ControlBrushKey`. It don't know so much about WinForms but i think the equivalent is `Control` in the SystemColors Class – Mark Sep 01 '14 at 11:24
  • @Mark thanks for that info. I am using Windows Forms, but I will likely also need to do this with WPF at some point. I guess I kinda figured the info would be the same. – jay_t55 Sep 01 '14 at 11:36

5 Answers5

19

Yes. In fact, there is an entire class dedicated to this:

The SystemColors class.

...or for WPF (thanks @ORMapper), The System.Windows.SystemColors class.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • 2
    That class is for GDI+/Windows Forms, and the WPF equivalent is [`System.Windows.SystemColors`](http://msdn.microsoft.com/en-us/library/system.windows.systemcolors%28v=vs.110%29.aspx). – O. R. Mapper Sep 01 '14 at 10:24
10

There is a System Color Class out, which will provide you the Colors.

For WinForms use:

System.Drawing.SystemColors

For WPF use:

System.Windows.SystemColors

Mark
  • 3,273
  • 2
  • 36
  • 54
2

You can use also the GetSysColor function api function.

Valter

2

You could use the Win API, GetSysColor function...

[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int GetSysColor(int nIndex);

The function returns the red, green, blue (RGB) color value of the given element.

To display the component of the RGB value, use the GetRValue, GetGValue, and GetBValue macros.

System colors for monochrome displays are usually interpreted as shades of gray.

To paint with a system color brush, an application should use GetSysColorBrush(nIndex), instead of CreateSolidBrush(GetSysColor(nIndex)), because GetSysColorBrush returns a cached brush, instead of allocating a new one.

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
0

I am wanting the same thing. My approach is to, upon initialization, create a temporary window with the background color specified in GetSysColor(COLOR_BTNFACE), the "standard" background color for dialog boxes. Then, I create a button with no text and get the colors. This temporary window is never displayed, and is destroyed immediately (WM_CREATE exit code = -1).

DednDave
  • 11
  • 2
  • 2
    Why would you go through all the work of creating a window just to get its color? What's wrong with calling GetSysColor and using its result directly? The same color (COLOR_BTNFACE) is used as the background color of dialogs, buttons, and other controls. The text color is COLOR_BTNTEXT. – Cody Gray - on strike Feb 04 '16 at 04:28