Results 1 to 9 of 9

Thread: VB 6 : Find out if Cursor is Inside a Form

  1. #1

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877

    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:
    1. 'Description : Tells if the cursor/mouse pointer is inside a form.
    2. 'Input : Form Name
    3. 'Output : Boolean (True/False)
    4. 'Requirements: Place a timer control inside the form.
    5.  
    6. 'Example Code:
    7.  
    8.  
    9. Option Explicit
    10.  
    11. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    12. Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
    13.  
    14. Private Type RECT
    15.     Left As Long
    16.     Top As Long
    17.     Right As Long
    18.     Bottom As Long
    19. End Type
    20.  
    21. Private Type POINTAPI
    22.     X As Long
    23.     Y As Long
    24. End Type
    25.  
    26. Private Const WM_USER As Long = &H400
    27. Private Const SB_GETRECT As Long = (WM_USER + 10)
    28.  
    29. Function IsInside(frmName As Form) As Boolean
    30.     Dim X As Long
    31.     Dim X1 As Long
    32.     Dim Y As Long
    33.     Dim Y1 As Long
    34.        
    35.     Dim Win As RECT
    36.     Dim Cur As POINTAPI
    37.    
    38.     GetWindowRect frmName.hWnd, Win
    39.    
    40.     GetCursorPos Cur
    41.        
    42.     If Cur.X > Win.Left And Cur.X < Win.Right And Cur.Y > Win.Top And Cur.Y < Win.Bottom Then
    43.         IsInside = True
    44.     Else
    45.         IsInside = False
    46.     End If
    47.    
    48. End Function
    49.  
    50. Private Sub Form_Load()
    51.     Timer1.Enabled = True
    52.     Timer1.Interval = 100
    53. End Sub
    54.  
    55. Private Sub Timer1_Timer()
    56.     If IsInside(Me) Then
    57.         Me.Cls
    58.         Print "The Cursor is Inside the form"
    59.     Else
    60.         Me.Cls
    61.         Print "The Cursor is Outside the form"
    62.     End If
    63.    
    64. End Sub
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    This can be generalised for all controls with an hwnd


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

  3. #3

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by manavo11
    This can be generalised for all controls with an hwnd
    Can you explain further. Look at this thread for more background on why I needed it.

    Thanks.

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    If you change this line :

    VB Code:
    1. Function IsInside(frmName As Form) As Boolean

    and you change the "As Form" to "As Object" (you might also want to change the name of the variable) you can check weather the mouse is over/in an object as long as it has an hwnd (like the form). This would work for a command button but not for a label... So you could either use some error trapping to see if there is an hwnd property for the object you passed to the function or use this...


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

  5. #5
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    Or you could pass an HWND itself, then find the window cordinate's that are associated with it, and procede from there.

    cjqp

  6. #6

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by manavo11
    If you change this line :

    VB Code:
    1. Function IsInside(frmName As Form) As Boolean

    and you change the "As Form" to "As Object" (you might also want to change the name of the variable) you can check weather the mouse is over/in an object as long as it has an hwnd (like the form). This would work for a command button but not for a label... So you could either use some error trapping to see if there is an hwnd property for the object you passed to the function or use this...
    OK, i think you didnt understand why i needed to write this function like that.

    Let me an example, In one of my project i needed to minimize my application after n minute of inactivity. So to do that i needed to know if the Mouse was inside the form. Now i could have used MouseMove property of the form, but since the form will be covered by other controls, i wont be able to use that. Thats why I used above method.

    I am sure there are other ways of doing it, but i found this to be the most suitable.

    Thanks

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  7. #7
    Hyperactive Member
    Join Date
    Nov 2002
    Location
    Someplace 'ore the rainbow
    Posts
    392
    We're just saying how this could be expanded to meet other people's needs.

    cjqp

  8. #8

    Thread Starter
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by cjqp
    We're just saying how this could be expanded to meet other people's needs.

    cjqp
    Sorry, if my answer came across like that. I was just trying to explain why i choose that particular method. Not discouraging posting alternatives.

    Danial
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  9. #9
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I see what you mean I was just suggesting how you can improve it for more general usage that might help other users as cjqp also said


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

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