简体   繁体   中英

Get default Windows System Colors in .NET

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 (eg Button Border Color, Button Highlight Color, Button Hover Color, Button Clicked Background Color, etc) in .NET?

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

The SystemColors class .

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

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

You can use also the GetSysColor function api function.

Valter

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM