C# 禁用winform按钮上的悬停行为

C# 禁用winform按钮上的悬停行为,c#,winforms,C#,Winforms,我正在使用C#4.0开发一个winform应用程序 我有一张有一个按钮的表格。我把按钮的底色改成了黄色。在运行时,当我将鼠标移到按钮上时,按钮的背面颜色会发生轻微变化。我想禁用这个。我希望无论发生什么,颜色都保持不变 以下是表格代码: using System; using System.Windows.Forms; namespace Something { public partial class Home : Form { public Home()

我正在使用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;



}
}
使用系统;
使用System.Windows.Forms;
名称空间
{
公共部分班级家庭:表格
{
公共住宅()
{
初始化组件();
}
}
}
名称空间
{
部分阶级家庭
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager资源=新的System.ComponentModel.ComponentResourceManager(typeof(Home));
this.button1=new System.Windows.Forms.Button();
这个.SuspendLayout();
// 
//按钮1
// 
this.button1.BackColor=System.Drawing.Color.Yellow;
resources.ApplyResources(此.button1,“button1”);
this.button1.Name=“button1”;
this.button1.UseVisualStyleBackColor=false;
// 
//家
// 
资源。ApplyResources(此,“$this”);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.BackColor=System.Drawing.Color.FromArgb(((int)(字节)(226)),((int)(字节)(227)),((int)(字节)(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\u Load);
此选项为.resume布局(false);
}
#端区
private System.Windows.Forms.Button按钮1;
}
}

提前感谢。

我相信如果您不想创建自己的button类,快速解决方案是将按钮的
FlatStyle
更改为
Flat
如果您已经将
FlatStyle
设置为Flat,您可以这样做:

//place this code in your form constructor
button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
button1.BackColorChanged += (s, e) => {
   button1.FlatAppearance.MouseOverBackColor = button1.BackColor;
};

我想您已经将FlatStyle设置为Flat。在平面外观中,我们可以将鼠标背景色更改为透明

像我这样的人不知道什么是编译器。我查了一下它是什么,并找到了怎么做。应该是这样的

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

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

能否显示您的表单代码?也许有一个onMouseOver事件处理程序连接到你的按钮上,它改变了颜色?似乎更好的方法是同时处理MouseEnter和MouseLeave事件,不是吗?这取决于按钮是如何改变的。按钮会改变,因此必须有代码来处理。如果在MouseEnter和MouseLeave事件上调用该代码,则不应引发这些事件。我甚至不确定WinForms中是否有onMouseOver事件(我不这么认为),但您明白了。:)它确实为这个问题提供了答案。它展示了如何做:一个例子。这非常简单。谢谢