Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>Hardcoding an RGB value based on what's shown on <em>your</em> 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.</p> <p>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:</p> <ol> <li><p>The user has the <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/bb773187.aspx" rel="nofollow noreferrer">Visual Styles</a> (themes) service turned on, meaning that they're using something like Luna or Aero.</p> <p>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 <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.visualstyles.aspx" rel="nofollow noreferrer"><code>System.Windows.Forms.VisualStyles</code></a> namespace. For example, you could write the following code (arbitrarily in C#):</p> <pre><code>using System.Windows.Drawing; using System.Windows.Forms.VisualStyles; // ... var vsr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal); var groupBoxCaptionColor = vsr.GetColor(ColorProperty.TextColor); </code></pre></li> <li><p>The user has the Visual Styles service turned <em>off</em> or it is unavailable (versions of Windows prior to XP), meaning that they're using the "Windows Classic" theme. </p> <p>In this case, the group box uses the standard 3D (control) color for its caption, so you can simply get that from the <a href="http://msdn.microsoft.com/en-us/library/system.drawing.systemcolors.aspx" rel="nofollow noreferrer"><code>System.Drawing.SystemColors</code></a> class. The property you're looking for is called <code>ControlText</code>:</p> <pre><code>using System.Windows.Drawing; // ... var groupBoxCaptionColor = SystemColors.ControlText; </code></pre></li> </ol> <p><a href="https://stackoverflow.com/a/5141282/366904">In a real application, you'll have to put these two cases together in order to handle all possible client configurations</a>. 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 <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.application.renderwithvisualstyles.aspx" rel="nofollow noreferrer"><code>Application.RenderWithVisualStyles</code></a> property, and if it's turned off, fall back to the second method. Something like:</p> <pre><code>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; } } } </code></pre> <p>The <code>GroupBoxCaptionColor</code> property will return a <code>Color</code> object corresponding to the <em>current</em> color used for the caption of group box controls. This <code>Color</code> 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.</p> <p>It's really not too complicated once you understand the various forces at work. Except the fun doesn't <em>quite</em> end there. You have to consider whether you want to handle the possibility that the user changes their system theme <em>while your application is running</em>. In that case, the caption color of the <em>actual</em> group box controls would change, but your programmatically-determined caption color would be obsolete, matching the old theme rather than the new one.</p> <p>The fix is monitoring the <a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.userpreferencechanged.aspx" rel="nofollow noreferrer"><code>SystemEvents.UserPreferenceChanged</code></a> 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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload