.net 如何重置为默认按钮 BackColor?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10569200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:24:56  来源:igfitidea点击:

How to reset to default button BackColor?

.netwinformsvisual-studiobuttonbackcolor

提问by Dennis Zaitsev

I was experimenting with different options for the button background color and ended up changing the BackColorproperty in the Appearance category. I then changed it back to what it is by default which is Control, but it still looks different from other buttons:

我尝试了不同的按钮背景颜色选项,最终更改BackColor了外观类别中的属性。然后我将其改回默认情况下的Control,但它看起来仍然与其他按钮不同:

Screenshot of buttons

按钮截图

I've tried building the project and restarting Visual Studio, but it did not help.

我试过构建项目并重新启动 Visual Studio,但没有帮助。

I know I can just drag another button and copy/paste code, but what causes this in the first place and how do I properly fix it?

我知道我可以拖动另一个按钮并复制/粘贴代码,但是首先是什么导致了这种情况,我该如何正确修复它?

回答by Cody Gray

The BackColorproperty is an ambient property by default, meaning that it inherits its value from its parent control. When you set it explicitlyto a particular value, that overrides the ambient nature and forces it to use that particular value.

BackColor默认情况下,该属性是环境属性,这意味着它从其父控件继承其值。当您将其显式设置为特定值时,这会覆盖环境性质并强制它使用该特定值。

The standard Windows button control does not support custom colors, so WinForms will actually custom draw the control to allow the designer to override its color. That way, if you want to have an ugly green or red button, you can do that.

标准的 Windows 按钮控件不支持自定义颜色,因此 WinForms 实际上会自定义绘制控件以允许设计人员覆盖其颜色。这样,如果你想要一个丑陋的绿色或红色按钮,你可以这样做。

What's happened here is you've effectively set a custom background color for the button control (you set it to the background color of a 3D control, but it could just as easily have been purple), and that's forced WinForms to custom draw the control and paint its background with your specified color. That's what gives it the "flat" appearance—the background color is now painted with a single custom color, rather than using the default gradient effect. It wouldn't have been as noticeable under the Windows Classic (pre-Aero) theme, because buttons actually werepainted with the flat 3D control color. But Aero added gradients and other "hot" effects, which makes this stick out like a sore thumb.

这里发生的事情是您已经有效地为按钮控件设置了自定义背景颜色(您将其设置为 3D 控件的背景颜色,但它也可能很容易变成紫色),这迫使 WinForms 自定义绘制控件并用您指定的颜色绘制其背景。这就是赋予它“平面”外观的原因——背景颜色现在用单一的自定义颜色绘制,而不是使用默认的渐变效果。在 Windows Classic(Aero 之前)主题下它不会那么明显,因为按钮实际上用平面 3D 控件颜色绘制的。但是 Aero 添加了渐变和其他“热”效果,这使得它像拇指酸痛一样突出。

To clear the value you've set it to and restore the ambient nature of the property, you can right-click on the property in the Properties Window, and select "Reset". You can also do it through code by setting the property to default(Color):

要清除您为其设置的值并恢复属性的环境性质,您可以在“属性”窗口中右键单击该属性,然后选择“重置”。您也可以通过代码将属性设置为default(Color)

myButton.BackColor = default(Color);

You will also need to set the UseVisualStyleBackColorproperty back to true, which gets automatically set to falsewhenever the BackColorproperty is changed to support the custom background color.

您还需要将该UseVisualStyleBackColor属性重新true设置为 ,false每当该BackColor属性更改为支持自定义背景颜色时,它就会自动设置为。

Alternatively, you can tell WinForms to ignore custom properties like that altogether and ask Windows to draw the button control. Do this by setting the FlatStyleproperty to FlatStyle.System.

或者,您可以告诉 WinForms 完全忽略此类自定义属性,并要求 Windows 绘制按钮控件。通过将FlatStyle属性设置为FlatStyle.System.

Again, this can be done either in the designer or through code. Not only will this prevent you from altering silly things like the background color, creating a horridly ugly control, but it will also restore the native behavior of the Win32 button control to your WinForms application, including the subtle Aero fade effects on hover.

同样,这可以在设计器中或通过代码完成。这不仅可以防止您更改背景颜色等愚蠢的东西,创建一个非常丑陋的控件,而且还可以将 Win32 按钮控件的本机行为恢复到您的 WinForms 应用程序,包括悬停时微妙的 Aero 淡入淡出效果。

I have no idea why this was not the default. You should have to make a special request to get ugly and non-standard controls. That shouldn't just happen automatically. I can only assume it was a concession to VB 6 programmers, who have been able to make all sorts of ugly controls for years.

我不知道为什么这不是默认设置。您应该提出特殊要求才能获得丑陋和非标准的控件。这不应该只是自动发生。我只能假设这是对 VB 6 程序员的一种让步,他们多年来一直能够制作各种丑陋的控件。

回答by pglynn

When you change the default back color, it flips the UseVisualStyleBackColorproperty to false.

当您更改默认背景颜色时,它会将UseVisualStyleBackColor属性翻转为 false。

Just change that back to true and you should be set!

只需将其改回 true,您就应该设置好了!

回答by Muhammad Saqib

To restore the default background color of the button, use;

要恢复按钮的默认背景颜色,请使用;

Button1.BackColor = SystemColors.ButtonFace
Button1.UseVisualStyleBackColor = True

回答by user4854277

What worked for me:

什么对我有用:

CommandButton1.BackColor = vbButtonFace

回答by LordPhantom

Button1.BackColor = Color.FromKnownColor(KnownColor.Control)

Use the FromKnowColorto access the system colors. It is really that simple.

使用FromKnowColor访问系统颜色。真的就是这么简单。

回答by Ady

To restore the default background and foreground to a button use:

要将默认背景和前景恢复为按钮,请使用:

Button1.BackColor = SystemColors.ButtonFace;
Button1.ForeColor = default(Color);
Button1.UseVisualStyleBackColor = true;

回答by AbdEllah Abril

Button1.BackColor = Color.Gainsboro

回答by M Arslan Riaz

**Use this please **

**请使用这个**

myButton.BackColor = default(Color);

回答by Tim Raykowski

tmpButtonCtrl.BackColor = DefaultBackColor
tmpButtonCtrl.ForeColor = DefaultForeColor
tmpButtonCtrl.UseVisualStyleBackColor = True
.NET Framework 4.5
VB.Net Visual Studio 2013

tmpButtonCtrl.BackColor = DefaultBackColor
tmpButtonCtrl.ForeColor = DefaultForeColor
tmpButtonCtrl.UseVisualStyleBackColor = True
.NET Framework 4.5
VB.Net Visual Studio 2013

回答by Baby

Referring to your question title, "How to reset to default button BackColor?", I solved my problem by:

参考您的问题标题,“如何重置为默认按钮 BackColor?” ,我通过以下方式解决了我的问题:

Create another button, say ButtonFoo, and set it to invisible,

创建另一个按钮,例如ButtonFoo,并将其设置为不可见,

ButtonFoo.visible = False

and you can use this button to get its color (since it is a default color) to reset your another button color, for example,

你可以使用这个按钮来获取它的颜色(因为它是默认颜色)来重置你的另一个按钮颜色,例如,

ButtonChangedColor.BackColor = ButtonFoo.BackColor

There you go; color is restored to its default :)

你去吧;颜色恢复为默认值 :)