在winform按钮上禁用悬停行为

时间:2013-10-29 09:33:53

标签: c# winforms

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

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

这是表单代码:

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 个答案:

答案 0 :(得分:17)

如果您已将FlatStyle设置为扁平,则可以执行以下操作,这很简单:

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

答案 1 :(得分:5)

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

答案 2 :(得分:0)

像我这样的人不知道编译器是什么。我抬头看看它是什么,并找到了如何做到这一点。看起来应该是这样的。

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

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

答案 3 :(得分:-1)

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