禁用控件的文本颜色 - 如何更改它
在创建我很棒的配对游戏期间;)我发现了一个完全无法解决的问题。
当玩家选择两个带有符号的标签时,我想锁定所有其他标签 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用重新定义的绘制事件创建您自己的标签:
在文本绘制过程中要小心文本格式标志。
Just create your own label with a redefined paint event:
Be careful with text format flags during text drawing.
比尝试更改 Windows 绘制禁用控件的方式更简单的方法是,当您希望有效“禁用”
Label
时,只需设置一个标志,然后检查在执行您想要的任何操作之前,请在您的Click
事件处理程序方法中添加该标志。如果该控件已被“禁用”,则不要采取任何操作。像这样的东西:
另外,请注意,您应该始终使用
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 yourClick
event handler method before taking whatever action you want. If the control has been "disabled", then don't take any action.Something like this:
Also, note that you should always use the
SystemColors
instead of something likeColor.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.