新建一个控件库,然后在里面拖放一个button按钮,再给这个按钮添加mouseenter事件,代码如下:
- C# code
LinearGradientBrush brush = new LinearGradientBrush(button1.ClientRectangle, Color.Red , Color.Brown ,LinearGradientMode.ForwardDiagonal); Graphics g = button1.CreateGraphics(); g.FillRectangle(brush, button1.ClientRectangle); g.Dispose();
------解决方案--------------------------------------------------------
添加自定义控件继承于Button类,然后添加MouseEnter,MouseLeave。重写OnPaint事件
bool _enter=false;
void testButton_MouseLeave(object sender, EventArgs e)
{
_enter = false;
}
void testButton_MouseEnter(object sender, EventArgs e)
{
_enter = true;
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
if (_enter)
{
LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Red,
Color.Brown, LinearGradientMode.Horizontal);
Graphics g = pevent.Graphics;
g.FillRectangle(brush, this.ClientRectangle);
}
}
------解决方案--------------------------------------------------------
- C# code
private bool _enter; protected override void OnMouseEnter(EventArgs e) { _enter = true; base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { _enter = false; base.OnMouseLeave(e); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); }