McMillan's Visual Basic Code - Code Snippets - How to fire MouseIn and MouseOut events
Code Snippets
Home



How to fire MouseIn and MouseOut events

This snippet shows you how to add MouseIn and MouseOut events to a Custom ActiveX Control

Option Explicit

Private MouseIn As Boolean

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As _
Long) As Long

Private Declare Function GetCursorPos Lib "user32" (lpPoint As _
POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal _
hwnd As Long, lpPoint As POINTAPI) As Long

Public Event MouseIn()
Public Event MouseOut()
Public Event MouseMove(Button As Integer, Shift As Integer, X _
As Single, Y As Single)

Private Sub UserControl_MouseMove(Button As Integer, Shift As _
Integer, X As Single, Y As Single)

   Dim xyCursor As POINTAPI
   Dim Xi As Long
   Dim Yi As Long

   SetCapture UserControl.hwnd

   GetCursorPos xyCursor
   ScreenToClient UserControl.hwnd, xyCursor

   Xi = xyCursor.X * Screen.TwipsPerPixelX
   Yi = xyCursor.Y * Screen.TwipsPerPixelY

   If Xi < 0 Or Yi < 0 Or Xi > UserControl.Width Or Yi > _
   UserControl.Height Then
      If Button > 0 Then
         RaiseEvent MouseMove(Button, Shift, X, Y)
      End If
      If MouseIn Then
         RaiseEvent MouseOut
         If Button = 0 Then
            ReleaseCapture
         End If
         MouseIn = False
      End If
   Else
      RaiseEvent MouseMove(Button, Shift, X, Y)
      If Not MouseIn Then
         RaiseEvent MouseIn
         MouseIn = True
      End If
   End If

End Sub


© Copyright Notice

Unless otherwise stated, the code on this site is Copyright to Andrew McMillan. You may use this code in your projects (both commercial and non-commercial) but you are not permitted to republish this code in any form without the Author's prior consent.

The code on this site is supplied "as is" and no claims are made as to its soundness. The Author claims no responsibility for or liability from use of said source code.


Home