9

我正在尝试使用 Visual Studio 2005 为我的表单(具有 FormBorderStyle = none)做一个自定义按钮。我在一个链接到该按钮的 ImageList 中有我的 3 个状态按钮图像。

this.btnClose.AutoSize = false;
this.btnClose.BackColor = System.Drawing.Color.Transparent;
this.btnClose.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.btnClose.FlatAppearance.BorderSize = 0;
this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnClose.ForeColor = System.Drawing.Color.Transparent;
this.btnClose.ImageKey = "Disabled";
this.btnClose.ImageList = this.imageList1;
this.btnClose.Location = new System.Drawing.Point(368, -5);
this.btnClose.Margin = new System.Windows.Forms.Padding(0);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(31, 31);
this.btnClose.TabIndex = 0;
this.btnClose.UseVisualStyleBackColor = false;
this.btnClose.MouseLeave += new System.EventHandler(this.btnClose_MouseLeave);
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
this.btnClose.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnClose_MouseDown);
this.btnClose.MouseHover += new System.EventHandler(this.btnClose_MouseHover);

private void btnClose_MouseHover(object sender, EventArgs e)
{
    btnClose.ImageKey = "enabled";
}

private void btnClose_MouseDown(object sender, MouseEventArgs e)
{
    btnClose.ImageKey = "down";
}

private void btnClose_MouseLeave(object sender, EventArgs e)
{
    btnClose.ImageKey = "disabled";
}

一切正常,但有一个问题。每当我移动鼠标悬停按钮时,我都会得到一个非常烦人的灰色背景。

我怎样才能删除它?

4

8 回答 8

44
btnClose.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
于 2010-06-01T15:27:54.133 回答
11

灰色背景是由于“System.Windows.Forms.FlatStyle.Flat”的设置,这是默认行为,因为它需要在悬停时突出显示按钮。为了消除这种情况,您可能必须编写一个自定义按钮类,从原始按钮继承并进行一些自定义绘画来实现这一点。

顺便说一句,您应该在 MouseHover 中设置,而不是在 MouseHover 中设置“启用”。MouseEnter 和 MouseLeave 是一对指示鼠标是否在按钮内,并且每次进入/退出都会触发一次。每当鼠标在按钮内移动时,MouseHover 就会触发,这会创建不必要的“启用”重复设置。

于 2008-10-12T05:17:01.557 回答
2

我已经使用标签而不是按钮解决了这个问题。

// 
// imageListButtons
// 
this.imageListButtons.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageListButtons.ImageStream")));
this.imageListButtons.TransparentColor = System.Drawing.Color.Transparent;
this.imageListButtons.Images.SetKeyName(0, "close_normal");
this.imageListButtons.Images.SetKeyName(1, "close_hover");
// 
// lblClose
// 
this.lblClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.lblClose.BackColor = System.Drawing.Color.Transparent;
this.lblClose.ImageKey = "close_normal";
this.lblClose.ImageList = this.imageListButtons;
this.lblClose.Location = new System.Drawing.Point(381, 7);
this.lblClose.Margin = new System.Windows.Forms.Padding(0);
this.lblClose.Name = "lblClose";
this.lblClose.Size = new System.Drawing.Size(12, 12);
this.lblClose.TabIndex = 0;
this.lblClose.MouseLeave += new System.EventHandler(this.lblClose_MouseLeave);
this.lblClose.MouseClick += new System.Windows.Forms.MouseEventHandler(this.lblClose_MouseClick);
this.lblClose.MouseEnter += new System.EventHandler(this.lblClose_MouseEnter);


private void lblClose_MouseEnter(object sender, EventArgs e)
{
    lblClose.ImageKey = "close_hover";
}

private void lblClose_MouseLeave(object sender, EventArgs e)
{
    lblClose.ImageKey = "close_normal";
}

private void lblClose_MouseClick(object sender, MouseEventArgs e)
{
    this.Close();
}

PS:请注意,我现在使用的是两个状态按钮,而不是三个。这是有意的(我知道我仍然可以使用三个)。

于 2008-10-12T22:10:48.983 回答
2

创建下面给出的鼠标输入事件。

private void forAllButtons_MouseEnter(object sender, EventArgs e)
{
    Button b = (Button)sender;
    b.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
}

然后将此事件分配给所有按钮。

快乐编程:)

于 2013-12-20T23:32:28.703 回答
2

我有一个建议。创建您自己的从 Button 派生的按钮类。然后覆盖其中的 MouseEnter 事件。只需删除用于调用基本实现的代码。

base.OnMouseEnter(e)

PS:您将无法在派生类之外使用 MouseEnter 事件(例如使用此控件的项目)

于 2015-09-23T01:34:57.870 回答
1

嗨,您只需使用这两行代码轻松地将这些更改应用于您的按钮。

  1. 将按钮的 FlatStyle 设置为 Flat

    this.btnClose.FlatStyle = FlatStyle.Flat;
    
  2. 将按钮的 MouseOverBackColor 设置为透明

    this.btnClose.FlatAppearance.MouseOverBackColor = Color.Transparent;
    

希望这会有所帮助。谢谢

于 2016-03-12T12:58:28.390 回答
0

您还可以通过取消选择按钮属性>常用> IsHitTestVisible中的IsHitTestVisible选项来停止更改按钮的颜色也许这也可以帮助...

于 2014-05-08T16:36:26.840 回答
-1

要解决此问题,请将 MouseOverBackColor 设置为透明以去除灰色背景。

于 2017-09-03T23:41:31.127 回答