禁用组框时如何更改组框中的字体颜色

How can I change the font color in a groupbox when I disable the groupbox

我在 C# 中使用 Groupbox,起初它是启用的。

当我使用 Groupbox1.Enabled = false 时,前景色(以及其中所有的前景色)变为默认黑色。甚至命令 label1.Forecolor = Color.White 也不起作用。 (其中 label1Groupbox1 中)。 当我 Enable Groupbox 时,它修复了。但我希望它是白色的,无论 Groupbox1 是启用还是禁用。

如果是 WPF,请将其放入您的 XAML 资源中:

 <Style TargetType="GroupBox" x:Key="NameOfYourStyle">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="White"/>
            </Trigger>
        </Style.Triggers>
    </Style>

将样式应用到您的 GroupBox 即可完成工作。

<GroupBox Style="{StaticResource NameOfYOurStyle}"/>

迪米特里

由于某些原因,无法在 WinForms 世界中设置禁用控件的前景色。相反,禁用的前景色是根据 BackColor

计算的

来自 Label.OnPaint(通过 Reflector):

if (base.Enabled)
{
    TextRenderer.DrawText(e.Graphics, this.Text, this.Font, r, nearestColor, flags);
}
else
{
    Color foreColor = TextRenderer.DisabledTextColor(this.BackColor);
    TextRenderer.DrawText(e.Graphics, this.Text, this.Font, r, foreColor, flags);
}

但是,您可以像这样实现自定义 Label class:

public class MyLabel : Label
{
    private const ContentAlignment anyBottom = ContentAlignment.BottomRight | ContentAlignment.BottomCenter | ContentAlignment.BottomLeft;
    private const ContentAlignment anyMiddle = ContentAlignment.MiddleRight | ContentAlignment.MiddleCenter | ContentAlignment.MiddleLeft;
    private const ContentAlignment anyRight = ContentAlignment.BottomRight | ContentAlignment.MiddleRight | ContentAlignment.TopRight;
    private const ContentAlignment anyCenter = ContentAlignment.BottomCenter | ContentAlignment.MiddleCenter | ContentAlignment.TopCenter;

    protected override void OnPaint(PaintEventArgs e)
    {
        // drawing the label regularly
        if (Enabled)
        {
            base.OnPaint(e);
            return;
        }

        // drawing the background
        Rectangle backRect = new Rectangle(ClientRectangle.X - 1, ClientRectangle.Y - 1, ClientRectangle.Width + 1, ClientRectangle.Height + 1);
        if (BackColor != Color.Transparent)
        {
            using (Brush b = new SolidBrush(BackColor))
            {
                e.Graphics.FillRectangle(b, backRect);
            }
        }

        // drawing the image
        Image image = Image;
        if (image != null)
        {
            Region oldClip = e.Graphics.Clip;
            Rectangle imageBounds = CalcImageRenderBounds(image, ClientRectangle, RtlTranslateAlignment(ImageAlign));
            e.Graphics.IntersectClip(imageBounds);
            try
            {
                DrawImage(e.Graphics, image, ClientRectangle, RtlTranslateAlignment(ImageAlign));
            }
            finally
            {
                e.Graphics.Clip = oldClip;
            }
        }

        // drawing the Text
        Rectangle rect = new Rectangle(ClientRectangle.X + Padding.Left, ClientRectangle.Y + Padding.Top, ClientRectangle.Width - Padding.Horizontal, ClientRectangle.Height - Padding.Vertical);
        TextRenderer.DrawText(e.Graphics, Text, Font, rect, ForeColor, image == null ? BackColor : Color.Transparent, GetFormatFlags());
    }

    private TextFormatFlags GetFormatFlags()
    {
        TextFormatFlags flags = TextFormatFlags.GlyphOverhangPadding | TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak;

        bool isRtl = RightToLeft == RightToLeft.Yes;
        var contentAlignment = TextAlign;
        if (isRtl)
            contentAlignment = RtlTranslateContent(contentAlignment);

        if ((contentAlignment & anyBottom) != 0)
            flags |= TextFormatFlags.Bottom;
        else if ((contentAlignment & anyMiddle) != 0)
            flags |= TextFormatFlags.VerticalCenter;
        else
            flags |= TextFormatFlags.Top;

        if ((contentAlignment & anyRight) != 0)
            flags |= TextFormatFlags.Right;
        else if ((contentAlignment & anyCenter) != 0)
            flags |= TextFormatFlags.HorizontalCenter;
        else
            flags |= TextFormatFlags.Left;

        if (AutoEllipsis)
            flags |= TextFormatFlags.WordEllipsis | TextFormatFlags.EndEllipsis;
        if (isRtl)
            flags |= TextFormatFlags.RightToLeft;
        if (UseMnemonic)
            flags |= TextFormatFlags.NoPrefix;
        if (!ShowKeyboardCues)
            flags |= TextFormatFlags.HidePrefix;

        return flags;
    }
}

或者,您也可以不禁用 GroupBox。相反,创建一个禁用所有子项的方法;

private void DisableChildren(Control control)
{
    foreach(var child in control.Controls.Cast<Control>().Where(child.GetType != typeof(Label) && child.GetType() != typeof(GroupBox)))
    {
        if(child.HasChildren)
        {
            DisableChildren(child);
        }
        child.Enabled = false;
    }
}

您可以看到我没有禁用标签或嵌套的 GroupBox。

只需像这样使用您通常会禁用 GroupBox 的地方;

DisableChildren(GroupBox1);

附带说明:Windows 窗体(GroupBox、面板等)下的任何容器都会发生同样的事情。

我手动更改了影响文本属性的 Groupbox 的前景色,字体如下:

将所有出现的 Enabled false 更改为 -

            grpGeneral.ForeColor = SystemColors.GrayText;
            grpGeneral.Enabled = false;

并使用 -

更改了所有出现的 Enabled true
            grpGeneral.Enabled = true;
            grpGeneral.ForeColor = SystemColors.ActiveCaptionText;