![]() |
Languages »
VBScript »
General
Advanced
Adding MouseLeave, MouseHover events to VB6 ControlsBy hspcThis article is about creating ActiveX controls in Visual Basic 6 that has two extra mouse Events : MouseLeave ,MouseHover. |
VBScript, VB 6Win2K, WinXP, Win2003, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

This article is about creating ActiveX controls in Visual Basic 6 that has two extra mouse Events:
MouseLeave : raised when the cursor get out of the control.
MouseHover : Raised when the user pouses 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 a MouseLeave,MouseHover Messages (Events).
We need 3 things to achieve this :
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. Dim trk As tagTRACKMOUSEEVENT
trk.cbSize = 16
trk.dwFlags = TME_LEAVE Or TME_HOVER
trk.dwHoverTime = trak.HoverTime
trk.hwndTrack = trak.hwnd
TrackMouseEvent trk
SetWindowLong API to set the new window procdure :SetWindowLong(ctl.hwnd, GWL_WNDPROC, AddressOf WindowProc)WindowProc Function is defined in mdlProc.bas like this :Private Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongWindowProc = CallWindowProc(trak.PrevProc, hwnd, uMsg, wParam, lParam)
WindowProc Function.But we may have multiple controls on the form. so we want to know which control was this message originally sent to.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.trackCol.Add trak, CStr(trak.hwnd)To search for the requires control : Set trak = trackCol.Item(CStr(hwnd))then we use this code to check the value of the message and take the required action.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
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 cerculer references.
(More about this in :Knowledge base)
note that I declared MyTrak with events :
Dim WithEvents MyTrak As clsTrackInfoCode: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.
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.UserControl_ReadProperties not UserControl_Initialize to be able to check the Ambient.UserMode property which is not available in the UserControl_Initialize event.
RequestTracking when WM_MOUSEMOVE message is sent.Instancing property of clsTrackInfo to private.On Error ... goto.. or On Error Resume Next is advisable.End or by clicking End in the IDE..This causes Terminate Events not to be called.You still can use the code.
mdlProc.bas , clsTrackInfo.cls to the project.Please feel free to contact the author for any questions or comments using this forum.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Apr 2004 Editor: |
Copyright 2004 by hspc Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |