Click here to Skip to main content
15,885,782 members
Articles / Programming Languages / VBScript

Adding MouseLeave, MouseHover events to VB6 Controls

Rate me:
Please Sign up or sign in to vote.
4.28/5 (13 votes)
24 Apr 2004CPOL4 min read 111.5K   4.9K   15   8
This article is about creating ActiveX controls in Visual Basic 6 that has two extra mouse Events: MouseLeave, MouseHover
Sample Image - Link_Label_Sample.jpg

Introduction

This article is about creating ActiveX controls in Visual Basic 6 that has two extra mouse Events:

  1. MouseLeave: raised when the cursor get out of the control
  2. MouseHover: Raised when the user pauses the cursor over the control for a defined time (default is 400 milliseconds)

A famous approach to achieve this is to use a Timer control with a small interval. And in the timer event, the programmer checks the cursor location. (I do hate this. It's Painful and needs a lot of work and overhead to track the cursor).
Another way is to start using VB.NET which has these events built-in. (But you should have stronger reasons to switch to .NET !!).
The alternative way used in this article is to let Windows send you MouseLeave, MouseHover Messages (Events).

How To Do This?

We need 3 things to achieve this:

  1. To tell Windows that you want it to send you the required events.

    This is achieved by calling TrackMouseEvent API function specifying Events you need and the hover time you want.This is done in the main module (mdlProc.bas) in the RequestTracking Function.

    VB.NET
    Dim trk As tagTRACKMOUSEEVENT
    trk.cbSize = 16
    trk.dwFlags = TME_LEAVE Or TME_HOVER
    trk.dwHoverTime = trak.HoverTime
    trk.hwndTrack = trak.hwnd
    
    TrackMouseEvent trk
  2. To receive the message when Windows sends it.

    Visual Basic does not have a built-in mechanism to receive custom messages. You can only choose from a list of events in the form or control code window.
    So we need to Subclass the control's window to intercept all messages sent to the window.Then we can handle the messages we need and forward the rest to the original window procedure. This is done by calling the SetWindowLong API to set the new window procedure:

    VB.NET
    SetWindowLong(ctl.hwnd, GWL_WNDPROC, AddressOf WindowProc)

    The WindowProc Function is defined in mdlProc.bas like this:

    VB.NET
    Private Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, _
    	ByVal wParam As Long, ByVal lParam As Long) As Long

    We need to handle 3 specific messages: WM_MOUSELEAVE, WM_MOUSEHOVER, WM_MOUSEMOVE and forward other messages (as well as the WM_MOUSEMOVE message) directly to the original window procedure:

    VB.NET
    WindowProc = CallWindowProc(trak.PrevProc, hwnd, uMsg, wParam, lParam)
  3. We need to dispatch the message to the window.

    Note that all messages are sent to the WindowProc Function. But we may have multiple controls on the form. so we want to know which control was this message originally sent to.
    To make this, we use a collection trackCol to hold references to clsTrackInfo objects. And the keys of the collection are the window handles (hwnd). I use window handles as keys because WindowProc Function receives the window handle as a parameter. So we can use it to lookup the clsTrackInfo object in the collection.

    To add the control to the collection:

    VB.NET
    trackCol.Add trak, CStr(trak.hwnd)

    To search for the required control:

    VB.NET
    Set trak = trackCol.Item(CStr(hwnd))

    Then we use this code to check the value of the message and take the required action:

    VB.NET
    If uMsg = WM_MOUSELEAVE Then
        trak.RaiseMouseLeave
    ElseIf uMsg = WM_MOUSEHOVER Then
        trak.RaiseMouseHover
    ElseIf uMsg = WM_MOUSEMOVE Then
        RequestTracking trak
        WindowProc = CallWindowProc(trak.prevProc, hwnd, uMsg, wParam, lParam)
    Else
        WindowProc = CallWindowProc(trak.prevProc, hwnd, uMsg, wParam, lParam)
        'Debug.Print uMsg
    End If

Skeleton of the Control

In the mdlProc.bas I use the clsTrackInfo to be stored in the trackCol collection. These objects in the collection are used to connect the module code to the UserControl.
It makes more sense to store references to the UserControl directly. But this causes the Terminate event not to be raised in some cases due to circular references.
(More about this in :Knowledge base)

Control's Skeleton Code

Note that I declared MyTrak with events:

VB.NET
Dim WithEvents MyTrak As clsTrackInfo

The code is as follows:

VB.NET
Option Explicit

Public Event MouseLeave()
Public Event MouseHover()

Dim WithEvents MyTrak As clsTrackInfo

Private Sub MyTrak_MouseHover()
RaiseEvent MouseHover
End Sub

Private Sub MyTrak_MouseLeave()
RaiseEvent MouseLeave
End Sub

Public Property Get HoverTime() As Long
HoverTime = MyTrak.HoverTime
End Property

Public Property Let HoverTime(newHoverTime As Long)
MyTrak.HoverTime = newHoverTime
PropertyChanged "HoverTime"
End Property

Private Sub UserControl_InitProperties()
Set MyTrak = New clsTrackInfo
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Set MyTrak = New clsTrackInfo
MyTrak.hwnd = UserControl.hwnd

MyTrak.HoverTime = PropBag.ReadProperty("HoverTime", 400)

If Ambient.UserMode Then
StartTrack MyTrak
End If
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "HoverTime", MyTrak.HoverTime, 400
End Sub

Private Sub UserControl_Terminate()
EndTrack MyTrak
Set MyTrak = Nothing
End Sub

I handle MyTrak_MouseHover and MyTrak_MouseLeave events of MyTrak object to raise the required events.

Notes

  1. StartTrack is called in the UserControl_ReadProperties to start tracking the events and add the control to the trackCol Collection, and EndTrack is called in the UserControl_Terminate event to end tracking and remove the control from the trackCol Collection.
    I used UserControl_ReadProperties not UserControl_Initialize to be able to check the Ambient.UserMode property which is not available in the UserControl_Initialize event.
  2. WM_MOUSEHOVER is sent when the user pauses the mouse over the control for a specific time. The default hover time is 400 milliseconds (the same as Windows default) but you can change it.
  3. After the first time windows sends the WM_MOUSEHOVER or WM_MOUSELEAVE events, it does not resend them till you re-request this. So I call RequestTracking when WM_MOUSEMOVE message is sent.
  4. Set the Instancing property of clsTrackInfo to private.
  5. Take care when changing this article's code or generally when using window subclassing in Visual Basic. My IDE crashed many times before I could make it work fine!!.
  6. Handle all errors in MouseLeave, MouseHover and MouseMove Event handlers. Any unhandled errors can make the IDE or the application crash or give more errors. So using On Error ... goto.. or On Error Resume Next is advisable.
    Also in the error trapping (Tools->Options->General tab), select break on unhandled errors or break in class module not break on all errors.
  7. It's always better not to end your application using End or by clicking End in the IDE... This causes Terminate Events not to be called.

If You Don't Understand All the Above

You still can use the code.

  1. Create a new ActiveX Control project.
  2. Add the mdlProc.bas, clsTrackInfo.cls to the project.
  3. Copy and paste the skeleton code above to your control.

Please feel free to contact the author for any questions or comments using this forum.

History

  • 24th April, 2004: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Egypt Egypt
I’m a software architect with a long experience in software architecture and design, with interest in cloud platforms and Agile methodologies.
I worked with many teams to successfully deliver software products using many frameworks and languages, and targeting many platforms like Azure, SharePoint, mobile, and desktop applications.
During my work with Agile teams, I coached them in both the technical and engineering aspects and helped developers with different experience levels in the organization to achieve the best results and build their skills. I also worked with product owners to get the best possible value through a continuous delivery model.
All these activities do not keep me away from coding and keeping up to date with new technologies and exploring new things from cool new libraries to cloud offerings to data science.

Comments and Discussions

 
GeneralMy vote of 5 Pin
soraking18-Mar-11 18:13
soraking18-Mar-11 18:13 
GeneralMouseHover - MouseLeave Pin
Don Putch18-Feb-07 12:44
Don Putch18-Feb-07 12:44 
GeneralRe: MouseHover - MouseLeave Pin
Hesham Amin23-Feb-07 2:02
Hesham Amin23-Feb-07 2:02 
GeneralRe: MouseHover - MouseLeave Pin
Don Putch23-Feb-07 7:13
Don Putch23-Feb-07 7:13 
GeneralMSHFlexGrid Problem Pin
sabankocal28-Jul-05 23:05
sabankocal28-Jul-05 23:05 
GeneralDoesn't always work!!! Pin
Anonymous17-Oct-04 14:00
Anonymous17-Oct-04 14:00 
GeneralRe: Doesn't always work!!! Pin
Hesham Amin22-Oct-04 23:13
Hesham Amin22-Oct-04 23:13 
GeneralMessage From Avinash Pin
Avinash_Sonu7-Sep-04 17:42
Avinash_Sonu7-Sep-04 17:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.