|
-
Jan 29th, 2004, 09:17 PM
#1
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
[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 : 
-
Jan 30th, 2004, 04:49 PM
#2
-
Jan 30th, 2004, 05:28 PM
#3
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 : 
-
Jan 31st, 2004, 04:00 PM
#4
-
Jan 31st, 2004, 04:04 PM
#5
Hyperactive Member
Or you could pass an HWND itself, then find the window cordinate's that are associated with it, and procede from there.
cjqp
-
Jan 31st, 2004, 04:08 PM
#6
Originally posted by manavo11
If you change this line :
VB Code:
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 : 
-
Jan 31st, 2004, 04:12 PM
#7
Hyperactive Member
We're just saying how this could be expanded to meet other people's needs.
cjqp
-
Jan 31st, 2004, 04:15 PM
#8
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 : 
-
Jan 31st, 2004, 05:05 PM
#9
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|