C# 如何将标签的字体颜色设置为与 GroupBox 的标题颜色相同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/590864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:27:26  来源:igfitidea点击:

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

c#.netvb.netwinforms

提问by Patrick McDonald

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?

我可以在不更改 GroupBox 标题的情况下执行此操作吗?

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.

我已经尝试将标签前景色设置为 ActiveCaption,这对于默认(蓝色)方案看起来没问题,但是当我将方案更改为橄榄绿色时,标签和分组框标题不一样。

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

此外,GroupBox 的正常行为是将 FlatStyle 设置为 Standard 会将标题颜色设置为 ForeColor,但是要创建新的 GroupBox 并将其 ForeColor 设置为 ControlText,您必须首先将其设置为 ControlText 以外的其他内容,然后再将其设置回来。(如果你不明白我的意思,那就试试看。)

采纳答案by Hans Passant

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;
      }
    }

回答by BFree

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

该标签公开了一个 ForeColorChanged 事件。然后你可以做这样的事情:

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。像这样的东西:

    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;
        }

回答by Andreas

I assume you use Windows Forms and not WPF. When you apply colors use the system colors (e.g. 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:

我假设您使用 Windows 窗体而不是 WPF。当您使用系统颜色(例如 Control 或 HighlightText)应用颜色时,这些将在用户切换 windows 主题时更改。这是将组框的颜色设置为系统颜色,然后将此颜色应用于标签的代码:

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

回答by Henk Holterman

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

从它的外观来看,GroupBox 使用 SystemColors.ActiveCaption 绘制它的标题,但您必须使用其他主题进行检查。

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

奇怪的是,这并没有反映在 ForeColor 属性中,但是如果您设置该属性,它就会接管。

So the answer is:

所以答案是:

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