VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



UserControl code that adds GotFocus, LostFocus, MouseIn and MouseOut events to a Form as well as th

by Michael A. Seel (4 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Mon 28th May 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

UserControl code that adds GotFocus, LostFocus, MouseIn and MouseOut events to a Form as well as the read-only properties of HasFocus and

API Declarations


I've left out the property bag code so, in the control's containing Form_Load() event, set the control's Enabled property to True

Rate UserControl code that adds GotFocus, LostFocus, MouseIn and MouseOut events to a Form as well as th



' Michael A. Seel
[email protected]
'
' :: EVENTS ::
' WndGotFocus: Fires when the container Form receives focus
' WndLostFocus: Fires when the container Form loses focus
' WndMouseIn: Fires when the mouse enters over the container
' WndMouseOut: Fires when the mouse exits from over the container
'
' :: PROPERTIES ::
' Enabled: True or False indicating whether events are fired
' WndHasFocus: True or False indicating whether the container Form as focus
' WndMouseOver: True or False indicating whether the mouse is over the container
'

Private Type PointAPI
    X As Long
    Y As Long
End Type

Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function IsChild Lib "user32" (ByVal hWndParent As Long, ByVal hwnd As Long) As Long

Private mMouseOver As Boolean  ' Tracks last known MouseOver status
Private mHasFocus As Boolean   ' Tracks last known HasFocus status

Public Event WndGotFocus()
Public Event WndLostFocus()
Public Event WndMouseIn()
Public Event WndMouseOut()


Public Property Let Enabled(ByVal vData As Boolean)
    If tmrCheck.Enabled <> vData Then
        If vData Then
            tmrCheck.Interval = 1
            tmrCheck.Enabled = True
        Else
            tmrCheck.Enabled = False
        End If
    End If
End Property

Public Property Get Enabled() As Boolean
    Enabled = tmrCheck.Enabled
End Property


Public Property Get WndHasFocus() As Boolean
    On Error Resume Next  ' In case the ContainerHwnd is invalid
    WndHasFocus = (GetForegroundWindow = UserControl.ContainerHwnd)
    On Error GoTo 0
End Property

Public Property Get WndMouseOver() As Boolean
    Dim P As PointAPI, lWnd As Long
    GetCursorPos P
    lWnd = WindowFromPoint(P.X, P.Y)
    On Error Resume Next  ' In case the ContainerHwnd is invalid
    WndMouseOver = CBool(IsChild(UserControl.ContainerHwnd, lWnd)) Or (lWnd = UserControl.ContainerHwnd)
    On Error GoTo 0
End Property

Private Sub tmrCheck_Timer()
    If mHasFocus <> Me.WndHasFocus Then
        mHasFocus = Me.WndHasFocus
        If mHasFocus Then
            RaiseEvent WndGotFocus
        Else
            RaiseEvent WndLostFocus
        End If
    End If
    If mMouseOver <> Me.WndMouseOver Then
        mMouseOver = Me.WndMouseOver
        If mMouseOver Then
            RaiseEvent WndMouseIn
        Else
            RaiseEvent WndMouseOut
        End If
    End If
End Sub

Download this snippet    Add to My Saved Code

UserControl code that adds GotFocus, LostFocus, MouseIn and MouseOut events to a Form as well as th Comments

No comments have been posted about UserControl code that adds GotFocus, LostFocus, MouseIn and MouseOut events to a Form as well as th. Why not be the first to post a comment about UserControl code that adds GotFocus, LostFocus, MouseIn and MouseOut events to a Form as well as th.

Post your comment

Subject:
Message:
0/1000 characters