当前位置:  开发笔记 > 编程语言 > 正文

C#中的自定义按钮:如何删除悬停背景?

如何解决《C#中的自定义按钮:如何删除悬停背景?》经验,为你挑选了2个好方法。

我正在尝试使用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";
}

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

我怎么能删除它?



1> 小智..:
btnClose.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;



2> faulty..:

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

顺便说一句,您应该在MouseEnter中执行此操作,而不是在MouseHover中设置"启用".MouseEnter和MouseLeave是一对指示鼠标是否在按钮内,并且每次进入/退出时触发一次.每当鼠标在按钮内移动时,鼠标悬停就会触发,这会创建"未启用"的重复设置.

推荐阅读
郑谊099_448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有