简体   繁体   English

C# 按钮背景色

[英]C# Button Background Color

i have a Windows Form, and i want make a modern design.我有一个 Windows 表格,我想做一个现代设计。
I'm just having a problem with button styling, you can help-me?我只是在按钮样式方面遇到问题,你能帮我吗?

I want to remove or hide the background color when the button was clicked, I managed to remove the background color from when the mouse is over the component with the following code:我想在单击按钮时删除或隐藏背景颜色,我设法使用以下代码从鼠标悬停在组件上时删除背景颜色:

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

Now i need remove or hide this (grey background):现在我需要删除或隐藏它(灰色背景):
Form Picture表格图片

How i make this?我怎么做这个?

Thank you!谢谢!

I obtained the solution with many attempts.我通过多次尝试获得了解决方案。
Just change the FlatAppearance.MouseOverBackColor and FlatAppearance.MouseDownBackColor to the background color of the Form or the item it is attached to.只需将 FlatAppearance.MouseOverBackColor 和 FlatAppearance.MouseDownBackColor 更改为 Form 或它所附加到的项目的背景颜色。

Example: You have a form with a panel, the color of the panel is red, so set the attributes to red, or just put "Color.Transparent"例子:你有一个带面板的表单,面板的颜色是红色,所以设置属性为红色,或者直接放“Color.Transparent”

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

So, if you have a class that extends the Button object, just make code like this:因此,如果您有一个扩展按钮 object 的 class,只需编写如下代码:

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

        // Rest of your code...
}

Otherwise, do a foreach, as below否则,做一个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]
}

The codes above are examples for everyone to be able to build their own logic, if in doubt I can help you.上面的代码是每个人都能够构建自己的逻辑的示例,如果有疑问我可以帮助你。

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

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