# Displacement Mouse Travel #
HI Vbforums
Im gonna tell me my question in the form example :
suppose my mouse is in some random position on the screen and so i want my mouse to travel shortest distance (displacement) from that random mouse point to a specific point say bottom left corner or center of the screen. but i want it to travel each and every step, not to skip.
if any having code for this displacement thing then please post it here.
thank you
Boing
Re: # Displacement Mouse Travel #
Hey there Boing. I thought your post sounded fun and I didn't already have it in my library so I messed around for a bit and this is what I came up with.
It could use some fine tuning but it should get you started off in the right direction.
In this example I used two timers, Timer1 and Timer2. The intervals of both timers are set to 50 and both timers are disabled by default.
I set the destinations and initiate the movement with btnMoveIt.
I used two timers because I stumbled across a cool effect and wanted to save it for future use.
Timer1 takes equal steps from source to destination so the motion is rather direct and mechanical.
Timer2 will start your cursor off fast but the step size will constantly be recalculated (getting smaller) so you gradually slow down near the destination.
Code:
Private Declare Function SetCursorPos Lib "user32.dll" ( _
ByVal x As Long, _
ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32.dll" ( _
ByRef lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Dim dblX As Long
Dim dblY As Long
Dim lngDX As Long
Dim lngDY As Long
Dim MyPoint As POINTAPI
Dim NewPoint As POINTAPI
Private Sub btnMoveIt_Click()
lngDX = 500 'Destination X
lngDY = 600 'Destination Y
Call GetCursorPos(MyPoint)
If MyPoint.x > lngDX Then
dblX = (MyPoint.x - lngDX) / 20 'Higher denominator is slower but more precise.
Else
dblX = (lngDX - MyPoint.x) / 20
End If
If MyPoint.y > lngDY Then
dblY = (MyPoint.y - lngDY) / 20
Else
dblY = (lngDY - MyPoint.y) / 20
End If
'Timer1.Enabled = True 'Direct
Timer2.Enabled = True 'Gradual
End Sub
Private Sub Timer1_Timer()
Call GetCursorPos(NewPoint)
If NewPoint.x > lngDX And NewPoint.y > lngDY Then
Call SetCursorPos(NewPoint.x - dblX, NewPoint.y - dblY)
ElseIf NewPoint.x < lngDX And NewPoint.y < lngDY Then
Call SetCursorPos(NewPoint.x + dblX, NewPoint.y + dblY)
ElseIf NewPoint.x > lngDX And NewPoint.y < lngDY Then
Call SetCursorPos(NewPoint.x - dblX, NewPoint.y + dblY)
ElseIf NewPoint.x < lngDX And NewPoint.y > lngDY Then
Call SetCursorPos(NewPoint.x + dblX, NewPoint.y - dblY)
End If
If Abs(lngDX - NewPoint.x) < 10 Or Abs(lngDY - NewPoint.y) < 10 Then
Call SetCursorPos(lngDX, lngDY)
Timer1.Enabled = False
End If
End Sub
Private Sub Timer2_Timer()
Call GetCursorPos(MyPoint)
If MyPoint.x > lngDX Then
dblX = (MyPoint.x - lngDX) / 10
Else
dblX = (lngDX - MyPoint.x) / 10
End If
If MyPoint.y > lngDY Then
dblY = (MyPoint.y - lngDY) / 10
Else
dblY = (lngDY - MyPoint.y) / 10
End If
If MyPoint.x > lngDX And MyPoint.y > lngDY Then
Call SetCursorPos(MyPoint.x - dblX, MyPoint.y - dblY)
ElseIf MyPoint.x < lngDX And MyPoint.y < lngDY Then
Call SetCursorPos(MyPoint.x + dblX, MyPoint.y + dblY)
ElseIf MyPoint.x > lngDX And MyPoint.y < lngDY Then
Call SetCursorPos(MyPoint.x - dblX, MyPoint.y + dblY)
ElseIf MyPoint.x < lngDX And MyPoint.y > lngDY Then
Call SetCursorPos(MyPoint.x + dblX, MyPoint.y - dblY)
End If
If Abs(lngDX - MyPoint.x) < 10 Or Abs(lngDY - MyPoint.y) < 10 Then
Call SetCursorPos(lngDX, lngDY)
Timer2.Enabled = False
End If
End Sub