One event that is noticably lacking from VB controls is a MouseOut event. The code below lets you create one. Please note that you need to change the hWnd that is checked in the tmrMouse_Timer() event to the control you want to monitor hovering over.
Add a timer to the form, called tmrMouse. Set its interval to 100. Also, add a command button called cmdHover.
'// VB Web Code Example
'// www.vbweb.co.uk
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI)
As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint
As Long, ByVal yPoint As Long) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private bHovering As Boolean
Private Sub MouseOut()
cmdHover.Caption = "Move Mouse Here"
bHovering = False
End Sub
Private Sub cmdHover_MouseMove(Button As Integer, Shift As Integer, X As Single,
Y As Single)
If bHovering = False Then
'// this event occurs a lot.
'// only change the caption when
we need to
bHovering = True
cmdHover.Caption = "Hovering..."
End If
End Sub
Private Sub tmrMouse_Timer()
Dim lpPos As POINTAPI
Dim lhWnd As Long
'// get the current pos
GetCursorPos lpPos
'// see if the mouse is currently over our form
lhWnd = WindowFromPoint(lpPos.X, lpPos.Y)
'// if it isn't over the button, then call the MouseOut procedure
If lhWnd <> cmdHover.hWnd And bHovering = True Then
MouseOut
End Sub
Comments