在C#中的自定义按钮:如何去除悬停背景? [英] Custom button in C#: How to remove hover background?

查看:1122
本文介绍了在C#中的自定义按钮:如何去除悬停背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个自定义的按钮,我的形式(其中有FormBorderStyle =无)。我在链接到该按钮的ImageList我的3种状态的按钮图像。

I'm trying to do a custom button to my form (which has FormBorderStyle = none). I have my 3 states button images in an ImageList linked to the button.

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.

我怎么能删除?

感谢您的时间。 最好的问候。

Thanks for your time. Best regards.

PS:我使用VS2005

PS: I'm using VS2005

推荐答案

灰色的背景是由于System.Windows.Forms.FlatStyle.Flat的设置,它的默认行为,因为它需要突出的按钮当你徘徊。为了避免这一点,你可能需要编写一个自定义按钮,从原来的按钮,继承和做一些自定义的绘画来实现这一点。

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.

顺便说一句,而不是设置为已启用,在MouseHover,你应该做它的MouseEnter。的MouseEnter和鼠标离开是一对表示是否是鼠标按键还是不中,它的每个入口/出口,一旦被解雇。凡为MouseHover属火,只要鼠标的按钮,创建unnessecery重复启动的设定范围内移动。

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".

这篇关于在C#中的自定义按钮:如何去除悬停背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆