Mouse EnterThe event
MouseEnter will be called when the mouse enteres a control.
MouseLeaveMouseLeave is raised when the mouse leaves the control.
MouseHoverMouseHoever event occurs after the cursor has entered the control (or the client area) and has stopped moving. The
MouseHoever event occurs at most only
once between
MouseEnter and
MouseLeave events.
Sample CodeHere’s a code that demonstrates the mentioned events, modified from Petzold’s
Programming Windows with C#.
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
class EnterLeave : Form
{
bool bInside = false;
public static void Main()
{
Application.Run(new EnterLeave() );
}
public EnterLeave()
{
Text = "Enter / Leave ";
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
}
protected override void OnMouseEnter(EventArgs e)
{
bInside = true;
Invalidate();
Trace.WriteLine("OnMouseEnter called");
}
protected override void OnMouseLeave(EventArgs e)
{
bInside = false;
Invalidate();
Trace.WriteLine("OnMouseLeave called");
}
protected override void OnMouseHover(EventArgs e)
{
Graphics grfx = CreateGraphics();
grfx.Clear(Color.Red);
System.Threading.Thread.Sleep(100);
grfx.Clear(Color.Green);
grfx.Dispose();
Trace.WriteLine("OnMouseHover called");
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics grfx = e.Graphics;
grfx.Clear(bInside ? Color.Green : BackColor);
}
}