【问题标题】:Disabling the hover behavior on winform buttons禁用 winform 按钮上的悬停行为
【发布时间】:2017-03-03 04:08:52
【问题描述】:

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



}
}

提前致谢。

【问题讨论】:

  • 您能告诉我们您的表单代码吗?也许有一个 onMouseOver 事件处理程序附加到您的按钮会改变颜色?
  • 似乎更好的方法是同时处理 MouseEnter 和 MouseLeave 事件,不是吗?
  • 这取决于按钮的更改方式。按钮发生了变化,因此必须有代码来处理它。如果在 MouseEnter 和 MouseLeave 事件上调用该代码,则这些事件不应引发。我什至不确定 WinForms 中是否存在 onMouseOver 事件(我不这么认为),但你明白了。 :)

标签: c# winforms


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这很简单。谢谢
    【解决方案3】:

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

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

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

    【讨论】:

    • 它确实提供了问题的答案。它展示了如何做到这一点:一个例子。
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2012-12-26
      • 2018-07-20
      • 2019-01-20
      相关资源
      最近更新 更多