C# 中的自定义按钮:如何删除悬停背景?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/195114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom button in C#: How to remove hover background?
提问by Matías
I'm trying to do a custom button to my form (which has FormBorderStyle = none) using Visual Studio 2005. I have my 3 states button images in an ImageList linked to the button.
我正在尝试使用 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";
}
All is working, but there's one catch. Whenever I move the mouse hover the button I get a really annoying grey background.
一切正常,但有一个问题。每当我将鼠标悬停在按钮上时,我都会得到一个非常烦人的灰色背景。
How can I remove that?
我怎样才能删除它?
采纳答案by faulty
The grey background is due to the setting of "System.Windows.Forms.FlatStyle.Flat", it's the default behaviour, since it need to highlight the button when you hover. To eliminate that, you might have to write a custom button class, inherit from the original button and do some custom painting to achieve that.
灰色背景是由于“System.Windows.Forms.FlatStyle.Flat”的设置,这是默认行为,因为它需要在您悬停时突出显示按钮。为了消除这种情况,您可能需要编写一个自定义按钮类,从原始按钮继承并进行一些自定义绘制来实现这一点。
Btw, instead of setting "enabled" in MouseHover, you should do it in MouseEnter. MouseEnter and MouseLeave is a pair which indicate whether is the mouse is within the button or not, and it's fired once per entry/exit. Where as MouseHover is fire whenever the mouse moved within the button, which create unnessecery repeated setting of "enabled".
顺便说一句,与其在 MouseHover 中设置“启用”,不如在 MouseEnter 中设置。MouseEnter 和 MouseLeave 是一对指示鼠标是否在按钮内,并且每次进入/退出都会触发一次。每当鼠标在按钮内移动时,MouseHover 就会触发,这会造成不必要的重复设置“启用”。
回答by Matías
I've solved this using a label instead of a button.
我已经使用标签而不是按钮解决了这个问题。
//
// 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: notice that I'm using now a two state button, instead of three. It is intended (I know that I still can use three).
PS:请注意,我现在使用的是两个状态按钮,而不是三个。它是有意的(我知道我仍然可以使用三个)。
回答by Vassili
btnClose.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
回答by Fazil Mir
create Mouse Enter event which is given below.
创建下面给出的鼠标输入事件。
private void forAllButtons_MouseEnter(object sender, EventArgs e)
{
Button b = (Button)sender;
b.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
}
then assign this event to all the buttons.
然后将此事件分配给所有按钮。
Happy programming :)
快乐编程:)
回答by Robin
You can also stop changing color of button by deselecting IsHitTestVisible option in Button Properties>common> IsHitTestVisible Maybe this can also help ...
您还可以通过在 Button Properties>common> IsHitTestVisible 中取消选择 IsHitTestVisible 选项来停止更改按钮的颜色也许这也可以帮助...
回答by Ahmed Suror
I have got one suggestion.Create your own button class deriving form Button.Then override the MouseEnter event in that.Just remove the code for calling the base implementaion.
我有一个建议。创建您自己的按钮类,派生表单 Button。然后在其中覆盖 MouseEnter 事件。只需删除用于调用基本实现的代码。
base.OnMouseEnter(e)
PS: You won't be able to use the MouseEnter event outside the derived class (e.g. a project using this control)
PS:您将无法在派生类之外使用 MouseEnter 事件(例如使用此控件的项目)
回答by Peck_conyon
Hi you simply can apply these changes to your button easily using these two lines of codes.
嗨,您可以使用这两行代码轻松地将这些更改应用于您的按钮。
Set the button's FlatStyle to Flat
this.btnClose.FlatStyle = FlatStyle.Flat;
Set the button's MouseOverBackColor to Transparent
this.btnClose.FlatAppearance.MouseOverBackColor = Color.Transparent;
将按钮的 FlatStyle 设置为 Flat
this.btnClose.FlatStyle = FlatStyle.Flat;
将按钮的 MouseOverBackColor 设置为透明
this.btnClose.FlatAppearance.MouseOverBackColor = Color.Transparent;
Hope this will help. Thanks
希望这会有所帮助。谢谢
回答by Sospeter Mong'are
To solve the problem, Set the MouseOverBackColor to transparent inorder to remove the grey backgroud.
为解决此问题,将 MouseOverBackColor 设置为透明以去除灰色背景。