当我将.NET Button的BackColor更改回其原始值时,它看起来不再相同 [英] When I change a .NET Button's BackColor back to its original value, it doesn't look the same any more

查看:47
本文介绍了当我将.NET Button的BackColor更改回其原始值时,它看起来不再相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,想简短地闪烁"以引起用户的注意.我认为最简单的方法是将Button的 BackColor 属性更改为另一种颜色,然后再次将其切换回原来的颜色.所以我做这样的事情:

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;

然后大约1/2秒后:

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.

更糟糕的是,当我在调试器中查看 myButton.BackColor 时,我得到

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?

推荐答案

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

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;

当我准备重置它时:

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

(是的,您必须先重置 BackColor ,然后再设置 UseVisualStyleBackColor .)

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

我不知道为什么要使用 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!)

这篇关于当我将.NET Button的BackColor更改回其原始值时,它看起来不再相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆