简体   繁体   中英

How do I set the Font color of a label to the same as the caption color of a GroupBox?

I want to have some labels on a form with the same font color as the caption on my group boxes, and furthermore I want these colors to change if the user has applied a different Theme on their system.

Can I do this without changing the GroupBox caption from its default?

UPDATE:

I have tried setting the Label ForeColor to ActiveCaption, this looks okay for the Default (Blue) scheme, but when I change the scheme to Olive Green, the label and group box captions are not the same.

Also, the GroupBox normal behaviour is that setting the FlatStyle to Standard sets the caption colour to ForeColor, however to create a new GroupBox and set its ForeColor to ControlText, you must first set it to something other than ControlText and then set it back again. (If you don't follow what I mean, then try it and see.)

Hmm, same question? I'll repeat my post:

using System.Windows.Forms.VisualStyles;
...

    public Form1()
    {
      InitializeComponent();
      if (Application.RenderWithVisualStyles)
      {
        VisualStyleRenderer rndr = new VisualStyleRenderer(VisualStyleElement.Button.GroupBox.Normal);
        Color c = rndr.GetColor(ColorProperty.TextColor);
        label1.ForeColor = c;
      }
    }

From the looks of it a GroupBox draws it's Caption with SystemColors.ActiveCaption, but you'll have to check that with other Themes.

Strangely this is not reflected in the ForeColor property, but if you set that property it takes over.

So the answer is:

private void Form1_Load(object sender, EventArgs e)
{            
  label1.ForeColor = SystemColors.ActiveCaption;
}

The label exposes a ForeColorChanged event. You can then do something like this:

this.label1.ForeColorChanged += (o,e) => { this.groupBox1.ForeColor = this.label1.ForeColor;};

If however you're trying to detect when the user changes their Theme, you can hook into the SystemEvents which can be found in the Microsoft.Win32 namespace. Something like this:

    Microsoft.Win32.SystemEvents.UserPreferenceChanged += new Microsoft.Win32.UserPreferenceChangedEventHandler(SystemEvents_UserPreferenceChanged);

void SystemEvents_UserPreferenceChanged(object sender, Microsoft.Win32.UserPreferenceChangedEventArgs e)
        {
            this.groupBox1.ForeColor = this.label1.ForeColor;
        }

I assume you use Windows Forms and not WPF. When you apply colors use the system colors (eg Control or HighlightText) these will be changed when the user switch the windows theme. Here is the code to set the color of the group box to system color and then apply this color for a label:

groupBox1.ForeColor = SystemColors.ActiveBorder;
label1.ForeColor = groupBox1.ForeColor;

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