Results 1 to 6 of 6

Thread: Determine If Mouse Is Within Your Form

  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!

  2. #2

  3. #3

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

    Re: Determine If Mouse Is Within Your Form

    Quote Originally Posted by MartinLiss
    Where would you put your usage code? BTW, it looks like you don't use Option Explicit becuse you have Dim iButtom As Long
    Thanks for that martin i fixed it and added where to use it...
    Born to help others
    (If I've been helpful then please rate my post. Thanks)

    call me EJ or be slapped!

  4. #4
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    Re: Determine If Mouse Is Within Your Form

    Very nice example EJ12N! Helped me on my last project!
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Determine If Mouse Is Within Your Form

    Another way :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
    4. Private Declare Function ReleaseCapture Lib "user32" () As Long
    5. Private Declare Function GetCapture Lib "user32" () As Long
    6.  
    7. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    8.     With Me
    9.         If (X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height) Then 'MouseLeave
    10.             Call ReleaseCapture
    11.             Debug.Print "Off"
    12.         ElseIf GetCapture() <> .hwnd Then 'MouseEnter
    13.             Call SetCapture(.hwnd)
    14.             Debug.Print "On"
    15.         Else
    16.             'Normal MouseMove
    17.         End If
    18.     End With
    19. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Hyperactive Member DarkX_Greece's Avatar
    Join Date
    Jan 2004
    Location
    Athens (Greece)
    Posts
    315

    Re: Determine If Mouse Is Within Your Form

    Much better, manavo11!
    Short CV:
    1. Visual Basic 6 Programmer
    2. Web Expert


    Botonakis Web Services

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