当我将 .NET 按钮的 BackColor 更改回其原始值时,它看起来不再相同
我有一个按钮,我想短暂地“闪烁”以引起用户的注意。 我认为最简单的方法是将按钮的 BackColor
属性更改为另一种颜色,然后再次将其切换回来。 所以我做了这样的事情:
this.oldColor = myButton.BackColor;
myButton.BackColor = Color.Blue;
然后大约 1/2 秒后:
myButton.BackColor = this.oldColor;
但是按钮的背景颜色最终明显比表单上的其他按钮更暗!
起初,我认为这是因为按钮的原始颜色是命名颜色(在本例中为“Control”)有一些特殊之处,但事实并非如此。
更糟糕的是,当我在调试器中查看 myButton.BackColor
时,我得到了
{Name=Control, ARGB=(255, 236, 233, 216)}
Which is完全正确! 但是当我截图并检查颜色时,它与其他按钮不一样!
是否存在某种掩蔽? 或者也许是某种犹豫?
I have a button which I wanted to "flash" briefly to get the user's attention. I figured the easiest way would be to change the Button's BackColor
property to another color, and then switch it back again. So I do something like this:
this.oldColor = myButton.BackColor;
myButton.BackColor = Color.Blue;
and then after a about 1/2 a second:
myButton.BackColor = this.oldColor;
But the button's background color end up being distinctly darker than the rest of the buttons on the Form!
At first, I thought it was because there's something special about the button's original color being a named color (in this case, "Control") but it isn't.
What's worse, when I look at myButton.BackColor
in the debugger, I get
{Name=Control, ARGB=(255, 236, 233, 216)}
Which is exactly correct! But when I take a screenshot, and check the color, it isn't the same as the rest of the buttons!
Is there some kind of masking going on? Or perhaps some kind of dithering?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我怀疑区别在于,一种是常规 argb 颜色,一种是系统/已知颜色。
.NET 中的控件会跟踪颜色是显式的(在此控件上设置)还是继承的。 这使得很难正确地更改回来......但您也许可以使用PropertyDescriptor来做到这一点,如下所示:
有点笨拙,但它应该可以工作。
I suspect the difference is that one is a regular argb color, and one is a system/known color.
Controls in .NET keep track of whether the color is explicit (set on this control) or inherited. This makes it hard to change back properly... but you might be able to do it with
PropertyDescriptor
, like so:A bit clunky, but it should work.
我找出了问题的原因。 原来还有另一个名为
UseVisualStyleBackColor
的属性(似乎仅适用于 Buttons 和 TabPages)。 它控制在计算BackColor
时是否使用“视觉样式”。 更糟糕的是,一旦您设置了BackColor
,它就会被设置为false
。 所以我最终这样做了:当我准备好重置它时:(
是的,您必须首先重置
BackColor
,然后才设置UseVisualStyleBackColor
。)我不知道为什么使用
UseVisualStyleBackColor
,或者为什么它会做它所做的事情,但它现在似乎可以工作。(谢谢,马克!没有你的帮助我就不会找到这个!)
I figured out the cause for the problem. Turns out there's another property (for Buttons and TabPages only, it seems) named
UseVisualStyleBackColor
. It controls whether or not to use "Visual Styles" when calculating theBackColor
. And to make matters worse, as soon as you set theBackColor
, it's set tofalse
. So I just ended up doing this:And when I'm ready to reset it:
(Yes, you have to reset the
BackColor
first, and only then setUseVisualStyleBackColor
.)I have no idea why the
UseVisualStyleBackColor
is being used, or why it does what it does, but it seems to work now.(Thanks, Marc! I wouldn't have found this without your help!)
按钮的 UseVisualStyleBackColor 属性确定是否使用视觉样式绘制按钮的背景。 当按钮的 UseVisualStyleBackColor 属性设置为 True 时,BackColor 属性将被忽略。 当 BackColor 属性更改为其他颜色时,UseVisualStyleBackColor 属性会自动设置为 False。
当您处于设计模式时,您可以看到这种效果。 创建一个按钮并将 BackColor 属性更改为红色。 然后向下滚动到 UseVisualStyleBackColor 属性,并注意它已设置为 False。 如果随后将 UseVisualStyleBackColor 属性设置为 True,按钮颜色将变回默认颜色。 将 UseVisualStyleBackColor 属性切换回 False 会将按钮颜色更改回红色。
下面的程序演示了这种效果。
感谢 Scraimer 发布了他的问题并将 UseVisualStyleBackColor 属性识别为罪魁祸首。
The UseVisualStyleBackColor property of button determines whether the background of the button is drawn using visual styles. When the UseVisualStyleBackColor property of a button is set to True, the BackColor property is ignored. When the BackColor property is changed to a different color, the UseVisualStyleBackColor property is automatically set to False.
You can see this effect when you are in design mode. Create a button and change the BackColor property to red. Then scroll down to the UseVisualStyleBackColor property, and note that it has been set to False. If you then set the UseVisualStyleBackColor property to True, the button color will change back to the default color. Switching the UseVisualStyleBackColor property back to False will change the button color back to red.
The following program demonstrates this effect.
Thanks to scraimer for posting his problem and identifying the UseVisualStyleBackColor property as the guilty culpert.
或者您可以直接输入:
Or you could just type:
简单的方法:
Simple way: