禁用控件的文本颜色 - 如何更改它

发布于 2024-11-07 09:37:34 字数 381 浏览 7 评论 0原文

在创建我很棒的配对游戏期间;)我发现了一个完全无法解决的问题。

当玩家选择两个带有符号的标签时,我想锁定所有其他标签 4 秒。

但是当我这样做时,所有标签的前色都会变为灰色,并且符号可见。我的问题是 - 有没有一种方法可以更改 Visual C# 中禁用标签的 ForeColor

该项目是一个WinForm应用程序。

目前,我以这种方式在代码中设置标签的颜色:

label1.ForeColor = lable1.BackColor;

当用户单击标签时,我将其更改为:

lable1.ForeColor = Color.Black;

During the creation of my awesome Matching Game ;) I found a problem that is completely out of reach.

When the player chooses two labels with symbols on them I want to lock all the other labels for 4 seconds.

But when I do that, the forecolor of all the labels changes to grey and the symbols are visible. My question is - is there a method to change the ForeColor of a disabled label in visual c#?

The project is a WinForm application.

At the moment I set the color of a label in code this way:

label1.ForeColor = lable1.BackColor;

When the user clicks the label I change it to:

lable1.ForeColor = Color.Black;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

城歌 2024-11-14 09:37:34

只需使用重新定义的绘制事件创建您自己的标签:

protected override void OnPaint ( System.Windows.Forms.PaintEventArgs e )
{
    if ( Enabled )
    {
        //use normal realization
        base.OnPaint (e);
        return;
    }
    //custom drawing
    using ( Brush aBrush = new SolidBrush( "YourCustomDisableColor" ) )
    {
        e.Graphics.DrawString( Text, Font, aBrush, ClientRectangle );
    }
}

在文本绘制过程中要小心文本格式标志。

Just create your own label with a redefined paint event:

protected override void OnPaint ( System.Windows.Forms.PaintEventArgs e )
{
    if ( Enabled )
    {
        //use normal realization
        base.OnPaint (e);
        return;
    }
    //custom drawing
    using ( Brush aBrush = new SolidBrush( "YourCustomDisableColor" ) )
    {
        e.Graphics.DrawString( Text, Font, aBrush, ClientRectangle );
    }
}

Be careful with text format flags during text drawing.

○闲身 2024-11-14 09:37:34

比尝试更改 Windows 绘制禁用控件的方式更简单的方法是,当您希望有效“禁用”Label 时,只需设置一个标志,然后检查在执行您想要的任何操作之前,请在您的 Click 事件处理程序方法中添加该标志。如果该控件已被“禁用”,则不要采取任何操作。

像这样的东西:

private bool labelDisabled = false;

private void myLabel_Click(object sender, EventArgs e)
{
    if (!labelDisabled)
    {
        this.ForeColor = SystemColors.ControlText;
    }
}

另外,请注意,您应该始终使用 SystemColors 而不是 Color.Black 之类的东西。
如果您对特定颜色值进行硬编码,则当用户自定义其默认 Windows 主题时,它们通常会发生冲突。 Raymond Chen 在一篇文章中讨论了这种做法的危险他的博客上的文章。

Way simpler than trying to change the way Windows draws disabled controls is to simply set a flag when you want the Label to be effectively "disabled", and then check the value of that flag in your Click event handler method before taking whatever action you want. If the control has been "disabled", then don't take any action.

Something like this:

private bool labelDisabled = false;

private void myLabel_Click(object sender, EventArgs e)
{
    if (!labelDisabled)
    {
        this.ForeColor = SystemColors.ControlText;
    }
}

Also, note that you should always use the SystemColors instead of something like Color.Black.
If you hard-code specific color values, they will often conflict when the user customizes their default Windows theme. Raymond Chen discusses the perils of this in an article on his blog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文