MouseHover event in Windows Forms .Net – Generating it repeatedly

MouseHover Event

When mouse is stationary for some time over a Windows Forms control, Mouse Hover event is generated. This event is generated only once during mouse’s stay and movement over a control. If mouse leaves and re-enters the control, system again starts tracking for Mouse Hover and will generate the event accordingly.

A common use of Mouse Hover event is to display tool tip.

Following code attaches MouseHover handler to a control.

myControl.MouseHover += myControl_MouseHover;

The handler just logs that event is generated.

void myControl_MouseHover(object sender, EventArgs e)
        "MouseHover event generated");

Generating Mouse Hover event repeatedly

In some scenarios, it could be desirable to generate Mouse Hover event repeatedly inside a control, whenever mouse becomes stationary. For instance, this could be useful if you have a user-drawn control and want to generate Mouse Hover events for individual user-drawn components. To achieve this, we need to reset the Hover event every time it is generated. This can be done by calling TrackMouseEvent method in MouseMove event.

Adding MouseMove event handler…

myControl.MouseMove += myControl_MouseMove;

Calling TrackMouseEvent to reset tracking of Hover event:

void myControl_MouseMove(object sender, MouseEventArgs e)
    const uint HOVER_DEFAULT = 0xFFFFFFFF;
    TRACKMOUSEEVENT trackMouseEvent = 
    TrackMouseEvent(ref trackMouseEvent);

As TrackMouseEvent is a native method, we need to define it using DllImport along with the TRACKMOUSEEVENT structure.

static extern int TrackMouseEvent(ref TRACKMOUSEEVENT lpEventTrack);

    public Int32 cbSize;    // using Int32 instead of UInt32 is safe here, and this avoids casting the result  of Marshal.SizeOf()
    public TMEFlags dwFlags;
    public IntPtr hWnd;
    public UInt32 dwHoverTime;

    public TRACKMOUSEEVENT(TMEFlags dwFlags, IntPtr hWnd, UInt32 dwHoverTime)
        this.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(TRACKMOUSEEVENT));
        this.dwFlags = dwFlags;
        this.hWnd = hWnd;
        this.dwHoverTime = dwHoverTime;

/// <summary>
/// The services requested. This member can be a combination of the following values.
/// </summary>
/// <seealso cref=""/>
public enum TMEFlags : uint
    /// <summary>
    /// The caller wants to cancel a prior tracking request. The caller should also specify the type of tracking that it wants to cancel. For example, to cancel hover tracking, the caller must pass the TME_CANCEL and TME_HOVER flags.
    /// </summary>
    TME_CANCEL = 0x80000000,
    /// <summary>
    /// The caller wants hover notification. Notification is delivered as a WM_MOUSEHOVER message.
    /// If the caller requests hover tracking while hover tracking is already active, the hover timer will be reset.
    /// This flag is ignored if the mouse pointer is not over the specified window or area.
    /// </summary>
    TME_HOVER = 0x00000001,
    /// <summary>
    /// The caller wants leave notification. Notification is delivered as a WM_MOUSELEAVE message. If the mouse is not over the specified window or area, a leave notification is generated immediately and no further tracking is performed.
    /// </summary>
    TME_LEAVE = 0x00000002,
    /// <summary>
    /// The caller wants hover and leave notification for the nonclient areas. Notification is delivered as WM_NCMOUSEHOVER and WM_NCMOUSELEAVE messages.
    /// </summary>
    TME_NONCLIENT = 0x00000010,
    /// <summary>
    /// The function fills in the structure instead of treating it as a tracking request. The structure is filled such that had that structure been passed to TrackMouseEvent, it would generate the current tracking. The only anomaly is that the hover time-out returned is always the actual time-out and not HOVER_DEFAULT, if HOVER_DEFAULT was specified during the original TrackMouseEvent request.
    /// </summary>
    TME_QUERY = 0x40000000,

Above ensures that Hover event is generated whenever mouse stays still for sometime, and not only once after mouse enters the control.

Book Review: Threading in C# by Joseph Albahari

Threading is a complex subject, it is easy to get yourself entangled in threads which don’t behave as you expect them to. There are many constructs and approaches to synchronize threads or achieve parallelism, each with its own quirks and advantages. There are good old threads, there are thread pools, task parallelism, Parallel class, Async methods and Timers. Then there are a myriad ways of synchronizing thread like locks, spin locks, mutex, semaphores, event wait handles, concurrent collections, Synchronization attribute, Memory Barriers, Interlocked class, Monitor’s Wait/Pulse and Reader Writer Lock . To make sense of it all,  Threading in C# by Joseph Albahari is thorough reference on the subject. It is available online, free of charge in hypertext format which makes it particularly useful as a reference.

csianThreading in C# is also there as a chapter in somewhat abridged form in author’s book ‘C# in a Nutshell‘.