简体   繁体   English

Visual Studio C# Windows 窗体……更改按钮颜色?

[英]Visual Studio C# Windows Forms… changing button color?

I have a form (see screenshot):我有一个表格(见截图):

默认表单外观

As you can see, its a pretty basic form, with a save button.如您所见,它是一个非常基本的表单,带有一个保存按钮。 I have programmed it so that if any of the text fields get changed, the "SAVE" button changes color so that its obvious that I haven't clicked save and don't forget to.我已经对它进行了编程,以便如果任何文本字段发生更改,“保存”按钮会更改颜色,以便很明显我没有单击保存并且不要忘记。 Unfortunately, simply changing the BackColor of the button to red isn't enough, because its UGLY as sin.不幸的是,简单地将按钮的 BackColor 更改为红色是不够的,因为它丑陋如罪。

丑陋的背景未保存

What can I do to change the color of the button to red, but not as ugly.我该怎么做才能将按钮的颜色更改为红色,但又不那么难看。 As you can see, the "BackColor" doesn't change the entire button, just the inner piece.如您所见,“BackColor”不会改变整个按钮,只会改变内部部分。 The border is still the same old fashioned transparent grey.边框仍然是老式的透明灰色。

A little bit of a LinearGradientBrush can go a long way to soften the harshness of a pure red button.一点点LinearGradientBrush可以LinearGradientBrush减轻纯红色按钮的刺耳感。

button1.ForeColor = Color.White;

Bitmap bmp = new Bitmap(button1.Width, button1.Height);
using (Graphics g = Graphics.FromImage(bmp)) {
  Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
  using (LinearGradientBrush br = new LinearGradientBrush(
                                      r,
                                      Color.Red,
                                      Color.DarkRed,
                                      LinearGradientMode.Vertical)) {
      g.FillRectangle(br, r);
    }
  }

then you can just assign the image to the button's BackgroundImage property:然后您可以将图像分配给按钮的BackgroundImage属性:

  button1.BackgroundImage = bmp;

Result:结果:

在此处输入图片说明

Note: Assigning a background image will lose the mouse hover coloring of the button.注意:分配背景图像将失去按钮的鼠标悬停颜色。

另一种解决方案是向按钮添加一个图标(例如感叹号),而不是通知用户更改尚未保存。

There are many tutorials online on how to create nice buttons with c#.网上有很多关于如何使用 c# 创建漂亮按钮的教程。 For example this one allows you to create Vista like buttons.例如,这个允许您创建类似 Vista 的按钮。 Have a look here: http://www.codeproject.com/Articles/19318/Vista-Style-Button-in-C看看这里: http : //www.codeproject.com/Articles/19318/Vista-Style-Button-in-C

For basic colors visit this SO question:对于基本颜色,请访问这个 SO 问题:

C#: Changing Button BackColor has no effect C#:更改按钮背景颜色无效

You can just use one of the button's properties, "FlatStyle".您可以只使用按钮的属性之一“FlatStyle”。 By default, it is standard.默认情况下,它是标准的。 But if you switch it to "Popup", your background color will be extended to the area of the button.但是如果你把它切换到“Popup”,你的背景颜色会扩展到按钮的区域。 You can compare the followings:您可以比较以下内容:

left-standard, right-popup左侧标准,右侧弹出

This won't work in WinForms, but you might want to switch over to WPF.这在 WinForms 中不起作用,但您可能希望切换到 WPF。 It's much more convenient because you can configure EVERYTHING.它更方便,因为您可以配置一切。 (Even the color of a progressbar) (甚至进度条的颜色)

Edit: The OP doesn't have to entirely rewrite his application.编辑:OP 不必完全重写他的应用程序。 He just needs to redo the layout of it, the code can be C&P'd over to the new WPF project, fyi他只需要重做它的布局,代码可以被C&P'd到新的WPF项目中,仅供参考

Edit²: You don't even need to use code to change the color of a WPF button, you can just define a red overlay with 30% opacity in the XAML file.编辑²:您甚至不需要使用代码来更改 WPF 按钮的颜色,您只需在 XAML 文件中定义一个不透明度为 30% 的红色叠加层即可。 It's that easy, really.就这么简单,真的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM