Visual Basic 6 function "MouseAboveObject"

Go back

Below you'll find the source for the Visual Basic 6 function MouseAboveObject.

Attribute VB_Name = "modMouseAboveObject"
' This function is downloaded from:
' You may freely distribute this file but please leave all comments, including this one, in it.
' @Author Stefan Thoolen <>

Option Explicit

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) 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

' Checks if the mousepointer is above a specific object
' @param    long    hWnd        The handle of the object (Text1.hWnd, Me.hWnd, etc.)
' @return   boolean             True if the mouse cursor is at the same coords as the object
' @author   Stefan Thoolen <>
Public Function MouseAboveObject(hWnd As Long) As Boolean
    Dim p As POINTAPI, r As RECT
    GetCursorPos p: GetWindowRect hWnd, r
    If p.Y < r.Top Or p.Y > r.Bottom Or p.X < r.Left Or p.X > r.Right Then Exit Function
    MouseAboveObject = True
End Function