Results 1 to 6 of 6

Thread: Determine If Mouse Is Within Your Form

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    In Front of my computer...
    Posts
    367

    Determine If Mouse Is Within Your Form

    This is to know whether the user has the mouse over your form or outside of it...Might be useful for people so... Enjoy
    VB Code:
    1. 'API AND DECLARATIONS
    2. Private Type POINTAPI
    3.     x As Long
    4.     y As Long
    5. End Type
    6.  
    7. Private Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Long
    8. '==========================================================
    9.  
    10. Private Function MouseInForm(pForm As Form) As Boolean
    11.  
    12.     Dim iRight As Long
    13.     Dim iLeft As Long
    14.     Dim iBottom As Long
    15.     Dim iTop As Long
    16.     Dim PT As POINTAPI
    17.    
    18.     'Lets convert everything to pixels since that's what GetCursorPos returns...
    19.     iRight = (pForm.Left + pForm.Width) / Screen.TwipsPerPixelX
    20.     iBottom = (pForm.Top + pForm.Height) / Screen.TwipsPerPixelY
    21.     iLeft = pForm.Left / Screen.TwipsPerPixelX
    22.     iTop = pForm.Top / Screen.TwipsPerPixelY
    23.    
    24.     GetCursorPos PT
    25.  
    26.     If (PT.x <= iRight) And (PT.x >= iLeft) _
    27.             And (PT.y >= iTop) And (PT.y <= iBottom) Then
    28.         MouseInForm = True
    29.     Else
    30.         MouseInForm = False
    31.     End If
    32.  
    33. End Function
    34.  
    35. 'USAGE:
    36. 'Can be used in Form_Mousemove or a Timer or anywhere you want to check if mouse is in your form
    37.     If MouseInForm(frmMain) Then
    38.         MsgBox "Mouse currently over frmMain"
    39.     Else
    40.         MsgBox "Mouse is not over frmMain"
    41.     End If
    Last edited by EJ12N; Feb 27th, 2005 at 10:52 PM.
    Born to help others
    (If I've been helpful then please rate my post. Thanks)

    call me EJ or be slapped!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width