关于用PictureBox自定义Button控件,代码没有按我想的执行!求解!谢谢

benbenRH 2009-11-08 09:31:20
用picturebox写的一个按钮控件,为什么鼠标离开的时候,有时候是变回 SystemColors .ButtonFace ;颜色(正常),有时候却鼠标离开后还是Color.White 呢? 费解!请高手指点迷津!谢谢
public partial class ImageButton : System .Windows .Forms .PictureBox
{

private Color _mouseOnBackColor =Color.White ;
private Color _BackColor = SystemColors .ButtonFace ;
private Color _mouseDownBackColor = Color.SkyBlue ;

public ImageButton()
{
InitializeComponent();
this.MouseEnter += new EventHandler(ImageButton_MouseEnter);
this.MouseDown += new MouseEventHandler(ImageButton_MouseDown);
this.MouseUp += new MouseEventHandler(ImageButton_MouseUp);
this.MouseLeave += new EventHandler(ImageButton_MouseLeave);
this.Size = new Size(25, 25);

}

void ImageButton_MouseLeave(object sender, EventArgs e)
{
this.BackColor = backColorMouseOut;
this.BorderStyle = BorderStyle.None;
}

void ImageButton_MouseUp(object sender, MouseEventArgs e)
{
this.BackColor = backColorMouseOn;
this.BorderStyle = BorderStyle.FixedSingle;
}

void ImageButton_MouseDown(object sender, MouseEventArgs e)
{
this.BackColor = backColorMouseDown;
this.BorderStyle = BorderStyle.FixedSingle;
}

void ImageButton_MouseEnter(object sender, EventArgs e)
{
this.BackColor = backColorMouseOn;
this.BorderStyle = BorderStyle.FixedSingle;
}

[Description("按钮背景颜色"), Category("Appearance")]
public Color backColorMouseOut
{
get { return _BackColor; }
set { _BackColor = value; }
}

[Description("鼠标在按钮上面时的背景颜色"), Category("Appearance")]
public Color backColorMouseOn
{
get { return _mouseOnBackColor; }
set { _mouseOnBackColor = value; }
}

[Description("鼠标在按钮上按下时的背景颜色"), Category("Appearance")]
public Color backColorMouseDown
{
get { return _mouseDownBackColor; }
set { _mouseDownBackColor = value; }
}


}
...全文
61 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
benbenRH 2009-11-08
  • 打赏
  • 举报
回复
我把

this.MouseEnter += new EventHandler(ImageButton_MouseEnter);


去掉,就可以,可能是因为MouseEnter和MouseLeave工作的不是很顺利,请问怎么解决!谢谢
benbenRH 2009-11-08
  • 打赏
  • 举报
回复
还是一样,我移开的很慢也是一样!
wartim 2009-11-08
  • 打赏
  • 举报
回复
因为有时你移开太快没有来得及刷新的关系


void ImageButton_MouseLeave(object sender, EventArgs e)
{
this.BackColor = backColorMouseOut;
this.BorderStyle = BorderStyle.None;

this.Invalidate(); // 刷新一下
}

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧