7

我正在使用 C# 4.0 开发一个 winform 应用程序

我有一个带有一个按钮的表单。我将按钮的背景颜色更改为黄色。在运行时,当我将鼠标移到按钮上时,按钮的背面颜色会略有变化。我想禁用它。无论发生什么,我都希望颜色保持不变。

这是表单代码:

using System;
using System.Windows.Forms;

namespace Something
{
    public partial class Home : Form
    {
        public Home()
        {
            InitializeComponent();
        }

    }
}



namespace Something
{
partial class Home
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Home));
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.BackColor = System.Drawing.Color.Yellow;
        resources.ApplyResources(this.button1, "button1");
        this.button1.Name = "button1";
        this.button1.UseVisualStyleBackColor = false;
        // 
        // Home
        // 
        resources.ApplyResources(this, "$this");
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(226)))), ((int)(((byte)(227)))), ((int)(((byte)(228)))));
        this.Controls.Add(this.button1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Home";
        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        this.Load += new System.EventHandler(this.Home_Load);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Button button1;



}
}

提前致谢。

4

4 回答 4

25

如果您已经设置FlatStyle为 flat,那么您可以执行以下操作很简单:

//place this code in your form constructor
button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
button1.BackColorChanged += (s, e) => {
   button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
};
于 2013-10-29T10:00:20.113 回答
6

我认为您已经将FlatStyle设置为Flat。在flatAppearance中,我们可以将MouseOverBackColor更改为透明

于 2017-03-01T13:49:45.820 回答
2

像我这样的人不知道编译器是什么。我查了一下它是什么,并找出了如何去做。它应该看起来像这样。

public Menu2**()
    {
        button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
        button1.BackColorChanged += (s, e) => {
            button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
        };

**Menu2 是您正在处理的表单的名称。

于 2017-03-02T20:08:52.207 回答
-1

我相信如果您不想创建自己的按钮类,快速解决方案是将按钮更改FlatStyleFlat

于 2013-10-29T09:39:09.187 回答