当我改变一个.NET按钮的背景色恢复到原来的价值,它看起来不一样了 [英] When I change a .NET Button's BackColor back to its original value, it doesn't look the same anymore

查看:149
本文介绍了当我改变一个.NET按钮的背景色恢复到原来的价值,它看起来不一样了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,我想闪简单地得到用户的关注。我想最简单的方法是将按钮的背景色属性更改为另一种颜色,然后再重新打开。所以,我做这样的事情:

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秒:

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.

更糟的是,当我在看 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?

推荐答案

我想通了的原因的问题。原来还有另一种属性(按钮和的TabPages只是,似乎)命名为 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;

(是的,你必须重新设置背景色第一,然后才设置 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按钮的背景色恢复到原来的价值,它看起来不一样了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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