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

时间:2016-01-12 12:41:10

标签: c# winforms groupbox

我在c#中使用Groupbox,首先,它已启用。

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

4 个答案:

答案 0 :(得分:1)

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

计算的

来自Label.OnPaint(反射器):

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类:

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

答案 1 :(得分:1)

我手动更改了Groupbox的foreColor,它影响了Text属性,字体如下:

使用 -

更改了所有出现的Enabled false
            grpGeneral.ForeColor = SystemColors.GrayText;
            grpGeneral.Enabled = false;

使用 -

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

答案 2 :(得分:0)

如果是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}"/>

迪米特里

答案 3 :(得分:0)

或者,您可以直接禁用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,Panel等)下的任何容器都会发生同样的事情。