In Visual Studio (WinForms), when you drag a GroupBox control onto a Form the header label gets a default color (of Blue). Now i know the Forecolor property shows up as "{Name=ControlText, ARGB=(255, 0, 0, 0)}" but the Name=ControlText is useless to me, and so is the ARGB, which just happens to be black.
What shade of blue is it ?
I want to be able to write
myLabel.ForeColor = Color.FromArgb(?, ?, ?, ?);
and get exactly the same shade of blue.
(Surely this is not rocket science, but it's not as easy as it looks).
EDIT#1. Based on some answers i will clarify and rephrase my question. I know its tied to themes, and ControlText for a GroupBox is different than ControlText for a Label. I am on WindowsXP using the "Windows XP" theme. How then do i find this shade of blue (in terms of RGB) that gets applied to a GroupBox when using the Windows XP theme? i.e. What are the ? ? ? ? values.
This is completely dependent upon the user's current theme. There is a different color used for group box captions in the Windows Classic theme, all 3 of the standard Windows XP Luna themes, the Aero theme, and perhaps something else entirely coming up in Windows 8. Not to mention it's user customizable, meaning that they might not even be using the standard theme color.
Hardcoding an RGB value based on what's shown on your computer is therefore not a good plan. That's a good way to make sure that your app will stick out like a sore thumb. Thus, the strategy suggested in the other answers to this question of taking a screenshot and sampling the pixel color is right out. It will be wrong more often than it's right.
Instead, you need to ask the system for this value to ensure that it matches the user's current configuration. This can be broken up into two general possibilities:
The user has the Visual Styles (themes) service turned on, meaning that they're using something like Luna or Aero.
In this case, you'll need to query the Visual Styles service for the appropriate color. That's trivial using the managed wrappers provided in the System.Windows.Forms.VisualStyles
namespace. For example, you could write the following code (arbitrarily in C#):
using System.Windows.Drawing;
using System.Windows.Forms.VisualStyles;
// ...
var vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal);
var groupBoxCaptionColor = vsr.GetColor(ColorProperty.TextColor);
The user has the Visual Styles service turned off or it is unavailable (versions of Windows prior to XP), meaning that they're using the "Windows Classic" theme.
In this case, the group box uses the standard 3D (control) color for its caption, so you can simply get that from the System.Drawing.SystemColors
class. The property you're looking for is called ControlText
:
using System.Windows.Drawing;
// ...
var groupBoxCaptionColor = SystemColors.ControlText;
In a real application, you'll have to put these two cases together in order to handle all possible client configurations. If the Visual Styles service is turned off, the first route will bomb, so you need to check for that first (which you can do by querying the Application.RenderWithVisualStyles
property, and if it's turned off, fall back to the second method. Something like:
using System.Windows.Drawing;
using System.Windows.Forms.VisualStyles;
// ...
public Color GroupBoxCaptionColor
{
get
{
// Test to see if Visual Styles are enabled.
if (Application.RenderWithVisualStyles())
{
// If Visual Styles are enabled, use that color.
var vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal);
return vsr.GetColor(ColorProperty.TextColor);
}
else
{
// Otherwise, fall back to the Classic theme.
return SystemColors.ControlText;
}
}
}
The GroupBoxCaptionColor
property will return a Color
object corresponding to the current color used for the caption of group box controls. This Color
object will technically have an RGB value, but you have to go through all of this song and dance to make sure that the color your app uses is always synced up with the current theme color.
It's really not too complicated once you understand the various forces at work. Except the fun doesn't quite end there. You have to consider whether you want to handle the possibility that the user changes their system theme while your application is running. In that case, the caption color of the actual group box controls would change, but your programmatically-determined caption color would be obsolete, matching the old theme rather than the new one.
The fix is monitoring the SystemEvents.UserPreferenceChanged
event, which is raised when the user changes their theme in the Control Panel. In the handler method for that event, you need to get the group box caption color again, and update any UI elements that might be using it.
It's worth calling special attention to the fact that, as the above-linked documentation notes, this is a static event, meaning that you must detach your event handler when your application is closed, or you will potentially leak memory.