【问题标题】:C# Button Background ColorC# 按钮背景颜色
【发布时间】:2020-08-19 00:02:08
【问题描述】:

我有一个 Windows 窗体,我想做一个现代设计。
我只是在按钮样式方面遇到问题,您能帮帮我吗?

我想在单击按钮时删除或隐藏背景颜色,我设法使用以下代码从鼠标悬停在组件上时删除背景颜色:

FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;

现在我需要删除或隐藏它(灰色背景):
Form Picture

我是怎么做到的?

谢谢!

【问题讨论】:

  • 显示你做了什么。您似乎清除了太多背景(如果是 winforms)。
  • 嗨 @Sinatr,我有一个 Button 的扩展,请看这个:link 和这里 link 我有一个事件可以进行个性化,如悬停、打印边框和其他一些东西.. . 注意:点击按钮出现灰色背景,我想去掉,我已经做了一个功能,点击时,按钮会变暗一点,所以灰色背景是不必要的和不愉快的。
  • 请将代码包含在您使用的代码括号中。
  • @Coding-Is-An-Adventure 我有一个类扩展了100多行的Button对象,我不能把所有的代码都放在这里,有字数限制,见谅。跨度>
  • 添加一个最小的可重现示例。 stackoverflow.com/help/minimal-reproducible-example

标签: c# button components styles


【解决方案1】:

我经过多次尝试得到了解决方案。
只需将 FlatAppearance.MouseOverBackColor 和 FlatAppearance.MouseDownBackColor 更改为 Form 或它所附加到的项目的背景颜色。

示例:你有一个带有面板的表单,面板的颜色是红色,所以设置属性为红色,或者直接放“Color.Transparent”

buttonName.FlatAppearance.MouseOverBackColor = Color.FromArg(255, 0, 0) // red;
buttonName.FlatAppearance.MouseDownBackColor = Color.Transparent; // same result

所以,如果你有一个扩展 Button 对象的类,只需编写如下代码:

public class MyButton : Button
{
        public MyButton()
        {
            FlatAppearance.MouseOverBackColor = Color.Transparent; // or Color.[Preference]
            FlatAppearance.MouseDownBackColor = Color.Transparent; // or Color.[Preference]
        }

        // Rest of your code...
}

否则,做一个foreach,如下

foreach (var button in this.Controls.OfType<Button>())
{
         button.FlatAppearance.MouseOverBackColor = Color.Transparent; // or Color.[Preference]
         button.FlatAppearance.MouseDownBackColor = Color.Transparent; // or Color.[Preference]
}

上面的代码是每个人都能够建立自己的逻辑的例子,如果有疑问我可以帮助你。

【讨论】:

    猜你喜欢
    • 2013-06-24
    • 2011-06-26
    • 1970-01-01
    • 2013-08-06
    • 2021-08-10
    • 1970-01-01
    • 2020-11-11
    • 2015-06-04
    • 2019-05-16
    相关资源
    最近更新 更多