Get default Windows System Colors in .NET - c#

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?

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

Related

Drawing a custom button with hot or pressed state

I've created a custom button control. Basically one button-rectangle, but with two areas inside the rectangle that have a different behavior. For that reason I want to draw the hot and pressed state ONLY for the specific areas, not the hole button.
My current approach is drawing the basic-button using ButtonRenderer.DrawButton(...) with an emtpy text, draw the hot or pressed state if required and finally drawing the text. So far so good, but how do I get the (gradient) colors for the hot/pressed state?
I've tried SystemColors, KnownColors and VisualStyleRenderer.GetColor(ColorProperty.XYZ) but none of them seems to match? How can I read those colors from the host system?
EDIT:
Sample picture below:
I want the colors of both the hot and the pressed button-state - (light) blue in case of this win7 screenshot. If you zoom in you can see that a slight color gradient in both the upper and the lower half is used.
The last button shows what I want to accomplish.
Sure, I could extract the colors from the screenshots and hardcode them or use images like suggested, but that would work only for this specific system, wouldn't it?
Thanks for your answers, Jimi.
According to the accepted answer of your linked SO-question I checked ButtonBaseAdapter and ButtonStandardAdapter. As you also mentioned, ButtonRenderer.DrawButton calls VisualStyleRenderer.DrawBackground which calls the native API UxTheme.DrawThemedBackground to draw the button - the color determination happens inside.
This native API call also draws the button-border and thats the reason why I can't use it.
But I was able to solve my Problem, an unusual way, but it works.
I render both relevant states (hot and pressed) to a bitmap and extract the colors using .GetPixel. In OnPaint I create LinearGradientBrush-instances from the extracted colors and draw the hot/pressed effect over the specific areas of the normal button.
It's not the exact same behavior like a normal button (for both states the border color also changes), but I think it would look really strange if I change the border color only for a part of the button (where the hot/pressed effect is displayed) to match the normal button behavior...
If no other answers or solutions come up I'll mark this post in a few days as an answer...

How to change the backcolor of a Button in UWP

There are a few identical questions in SO asking about changing the background color of a button in UWP (Universal Windows Platform):
How to change the background color of button in Universal Windows Platform Apps?
how to change background color of button in UWP Apps in c# ?
However, it seems that Microsoft has been changing things. SolidColorBrush can no longer be found. I have tried using Windows.UI.Xaml.Media.Brush and Windows.UI.Colors. None of those work.
Mouse hovering over button.background, the hint shows that background is expecting type: Windows.UI.Xaml.Media.Brush.
My Question: How do I change the background color of a button using c# codes? If I use the suggested solutions in other identical posts, namespace SolidColorBrush will not be found.
Have you imported the following namespace?
Windows.UI.Xaml.Media
Because if have not, you won't be access the SolidColorBrush Class directly,
and will have to do so by:
Windows.UI.Xaml.Media.SolidColorBrush mycolor = new SolidColorBrush(Windows.UI.Colors.Blue);
In this example, I have created a SolidColorBrush with the Color of Blue, and I have setit directly, without any kind of conversion, like this:
myButton.Background = mycolor;
In case you want to create your own color, you can utilize the Windows.UI.Color.FromArgb method, where you can even specify the alpha of your Color.
Edit:
Looking back on your answer, I have realized that you were trying to create your Brush, which would set the Background of a button control, with the Brush class. The Brush class is the parent for several derived brushes classes, all with different purposes. One of them is the SolidColorBrush class.

How to make Custom color in c#?

In Visual Studio, please suggest how to make custom color for a control and add it to property-> backColor Section.?
You can type a comma separated RGB value into the BackColor value in the property grid, eg:
150, 250, 70
You can use this method:
Color clr = Color.FromArgb(int alpha, int red, int green, int blue)
If you want this done using some User interface:
You could find out where the custom colors are stored for the current user, and then add one.
Or, create your own UITypeEditor, that contains the colors you want. Probably you have to inherit from Form, and override the BackColor property to add the correct attributes.
Much easier is to just set it in code, using the Argb code from the other answers.
You can not.
What you see is a default editor for Color type (create own control, add there public property of Color type and it will also uses it). Web and System tabs working fine. First tab is a sort of custom color pickup part from standard color pickup dialog.
I think MS fails to make popup editor to show modal dialog (because popup will get closed). =D
Perhaps colors there are taken from Windows color dialog, so you have to arrange it there (perhaps you can use winapi to do that). /shrug

How to create shell styled title bar buttons in .NET

This is a follow-up to my earlier question Draw Custom Buttons on Windows Vista/7 Aero Titlebar.
I revisited the topic quite recently and found this article which is essentially a hack to 'drawing' buttons on Aero-enabled title bar (Windows Vista & 7). What the code does is to create a transparent window over the current one and places the buttons on it, giving the impression of additional buttons on the title bar. The only problem is the buttons look like regular WinForms buttons!
My question is, how do I read the windows shell style (aka theme) in order to create buttons styled just like those in the Control Box (see image)?
I'd like answers to be in .NET (VB.NET or C#). I'm okay with unmanaged code.
So if I understand you correctly, you want to read what Windows 7 calls the "Window Color" aspect of the current theme.
Acording to MSDN http://msdn.microsoft.com/en-us/magazine/cc163435.aspx, you want DwmGetColorizationColor: "retrieves the current color that is being used for DWM glass composition. This value is based on the current color scheme. Changing the setting causes a WM_WMCOLORIZATIONCOLORCHANGED notification."
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmGetColorizationColor(out int pcrColorization, [MarshalAs(UnmanagedType.Bool)]out bool pfOpaqueBlend);
"You can check to see the composition color and opacity by calling the DwmGetColorizationColor function. If this function succeeds, it will set a GDI+ ARGB color value and a Boolean indicating whether the color is opaque. Just like changing the Aero scheme in the control panel, there's a message broadcast when the composition color has changed. WM_DWMCOLORIZATIONCOLORCHANGED is sent when this happens, but in this case the parameters tell you what the new color and opacity are."

C# UseVisualStyleBackColor

Is there a way in which I can change the button color and preserve the Windows VisualStyle? I want to create a button that looks like checkBox2 (color and style)
this.button2.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.button2.UseVisualStyleBackColor = false; //if true, BackColor gets ignored
No, not really. This kind of button background is drawn by VisualStyleRenderer.DrawBackground(). Which in turns pinvokes DrawThemeBackground(). These methods don't take a color. None is needed because the color is already specified in the theme.
Simulating the appearance with a LinearGradientBrush is your only real hope. Note that custom drawing a button is quite difficult, all the code is internal and no owner-draw is provided.
Consider using an image.

Categories