禁用组框时,如何更改组框中的字体颜色
本文关键字:字体 何更改 颜色 | 更新日期: 2023-09-27 18:29:54
我在c#中使用了一个Groupbox,起初它是Enabled。
当我使用Groupbox1.Enabled = false
时,它的前景色(以及里面的所有东西的前景色)都会变为默认的黑色。并且即使命令CCD_ 2也不起作用。(哪个label1
在Groupbox1
中)。当我启用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}"/>
Dimitri
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;
}
}
我手动更改了Groupbox的foreColor,这影响了Text属性,字体如下:
用-更改了所有出现的Enabled false
grpGeneral.ForeColor = SystemColors.GrayText;
grpGeneral.Enabled = false;
并用-更改了所有Enabled true的出现
grpGeneral.Enabled = true;
grpGeneral.ForeColor = SystemColors.ActiveCaptionText;
或者,您可以简单地不禁用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;
}
}
你可以看到我没有禁用标签或嵌套的GroupBoxes。
只需像这样使用,您通常会禁用GroupBox;
DisableChildren(GroupBox1);
顺便说一句:同样的事情发生在Windows窗体下的任何容器(GroupBox、Panel等)上。