VB 6 : Find out if Cursor is Inside a Form
I needed this in one of my project, so i thought it might be useful for others.
VB Code:
'Description : Tells if the cursor/mouse pointer is inside a form.
'Input : Form Name
'Output : Boolean (True/False)
'Requirements: Place a timer control inside the form.
'Example Code:
Option Explicit
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Const WM_USER As Long = &H400
Private Const SB_GETRECT As Long = (WM_USER + 10)
Function IsInside(frmName As Form) As Boolean
Dim X As Long
Dim X1 As Long
Dim Y As Long
Dim Y1 As Long
Dim Win As RECT
Dim Cur As POINTAPI
GetWindowRect frmName.hWnd, Win
GetCursorPos Cur
If Cur.X > Win.Left And Cur.X < Win.Right And Cur.Y > Win.Top And Cur.Y < Win.Bottom Then
IsInside = True
Else
IsInside = False
End If
End Function
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 100
End Sub
Private Sub Timer1_Timer()
If IsInside(Me) Then
Me.Cls
Print "The Cursor is Inside the form"
Else
Me.Cls
Print "The Cursor is Outside the form"
End If
End Sub