当我将 .NET 按钮的 BackColor 更改回其原始值时,它看起来不再相同

发布于 2024-07-12 21:57:13 字数 609 浏览 9 评论 0原文

我有一个按钮,我想短暂地“闪烁”以引起用户的注意。 我认为最简单的方法是将按钮的 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 技术交流群。

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

发布评论

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

评论(5

南七夏 2024-07-19 21:57:13

我怀疑区别在于,一种是常规 argb 颜色,一种是系统/已知颜色。

.NET 中的控件会跟踪颜色是显式的(在此控件上设置)还是继承的。 这使得很难正确地更改回来......但您也许可以使用PropertyDescriptor来做到这一点,如下所示:

    TextBox tb = new TextBox();
    tb.BackColor = Color.Red;
    // now set it back to inherited
    PropertyDescriptor prop = TypeDescriptor.GetProperties(tb)["BackColor"];
    if (prop.CanResetValue(tb))
    {
        prop.ResetValue(tb);
    }

有点笨拙,但它应该可以工作。

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:

    TextBox tb = new TextBox();
    tb.BackColor = Color.Red;
    // now set it back to inherited
    PropertyDescriptor prop = TypeDescriptor.GetProperties(tb)["BackColor"];
    if (prop.CanResetValue(tb))
    {
        prop.ResetValue(tb);
    }

A bit clunky, but it should work.

枫以 2024-07-19 21:57:13

我找出了问题的原因。 原来还有另一个名为 UseVisualStyleBackColor 的属性(似乎仅适用于 Buttons 和 TabPages)。 它控制在计算BackColor时是否使用“视觉样式”。 更糟糕的是,一旦您设置了 BackColor,它就会被设置为 false。 所以我最终这样做了:

this.oldUseVisualStyleBackColor = myButton.UseVisualStyleBackColor;
this.oldColor = myButton.BackColor;
myButton.BackColor = Color.Blue;

当我准备好重置它时:(

myButton.BackColor = this.oldColor;
myButton.UseVisualStyleBackColor = this.oldUseVisualStyleBackColor;

是的,您必须首先重置 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 the BackColor. And to make matters worse, as soon as you set the BackColor, it's set to false. So I just ended up doing this:

this.oldUseVisualStyleBackColor = myButton.UseVisualStyleBackColor;
this.oldColor = myButton.BackColor;
myButton.BackColor = Color.Blue;

And when I'm ready to reset it:

myButton.BackColor = this.oldColor;
myButton.UseVisualStyleBackColor = this.oldUseVisualStyleBackColor;

(Yes, you have to reset the BackColor first, and only then set UseVisualStyleBackColor.)

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!)

岁吢 2024-07-19 21:57:13

按钮的 UseVisualStyleBackColor 属性确定是否使用视觉样式绘制按钮的背景。 当按钮的 UseVisualStyleBackColor 属性设置为 True 时,BackColor 属性将被忽略。 当 BackColor 属性更改为其他颜色时,UseVisualStyleBackColor 属性会自动设置为 False。

当您处于设计模式时,您可以看到这种效果。 创建一个按钮并将 BackColor 属性更改为红色。 然后向下滚动到 UseVisualStyleBackColor 属性,并注意它已设置为 False。 如果随后将 UseVisualStyleBackColor 属性设置为 True,按钮颜色将变回默认颜色。 将 UseVisualStyleBackColor 属性切换回 False 会将按钮颜色更改回红色。

下面的程序演示了这种效果。

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Button1.BackColor = Color.Red
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Button1.UseVisualStyleBackColor = Not Button1.UseVisualStyleBackColor
End Sub

感谢 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.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Button1.BackColor = Color.Red
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Button1.UseVisualStyleBackColor = Not Button1.UseVisualStyleBackColor
End Sub

Thanks to scraimer for posting his problem and identifying the UseVisualStyleBackColor property as the guilty culpert.

情深缘浅 2024-07-19 21:57:13

或者您可以直接输入:

TextBox tb = new TextBox();
//Change the Backcolor
tb.BackColor = Color.Red;
//Resets the Backcolor to its default value, its pretty strange that you don't see the method but it works with allmost all properties
tb.ResetBackColor();

Or you could just type:

TextBox tb = new TextBox();
//Change the Backcolor
tb.BackColor = Color.Red;
//Resets the Backcolor to its default value, its pretty strange that you don't see the method but it works with allmost all properties
tb.ResetBackColor();
美男兮 2024-07-19 21:57:13

简单的方法:

myButton.BackColor = Color.Empty

Simple way:

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