Getting System Colors In C#
Sunday, June 29th, 2008*** READ THE COMMENTS! I have been enlightened.
***
Maybe it’s just me, but it took me forever to figure out how to set the background color of a control to the “window” color the user has set up in the control panel. 99% of the time this is white but since I’m one of those guys who runs with dark window colors, I was determined not to make that assumption and do things the right way.
Ways you can’t do it:
- System.Windows.Forms.SystemInformation: You can get a wealth of information from here, including whether or not the user has chosen to always show pull-down menu access key underlines, or only when ALT is held… but no color information. Close, but no cigar.
- Color.System.
: all the other colors live here. I was hoping for a “System…” subsection under here or at least names like “ActiveBorder” intermingled with “hard coded” names (Lemon Chiffon!), but couldn’t find any. - Control.DefaultBackColor: So close! This appears to return the 3D face color, but regardless isn’t what I was looking for. I want a big list of all the system colors, not just a one-off.
The Way:
System colors, in C#, are called “known colors.” Once you know this it’s easy to find:
System.Drawing.Color.FromKnownColor(KnownColor.Window);
And, as you’d expect, the KnownColor enumeration contains all of the other user-definable colors (active window border, whatever).
I don’t understand the logic behind calling these colors “known colors.” If anything, Lemon Chiffon is a Known Color (well, OK maybe not to all of us, but to interior decorators at least), and the current window color is Unknown, or at least “user-defined.” But even more perplexing is why C# deviates from calling them “system colors.” In Win32 you’d call GetSysColor() (and truth be told, I almost just pinvoked that). KnownColors is a wart that detracts from the beautiful organization of C# in other places (for example, Process or System.IO.Ports).
If I were running the C# show, I would have put the system colors in with the rest of the system information.




