PDA

Click to See Complete Forum and Search --> : Moving the mouse with a VB program


Jan 23rd, 2000, 04:18 AM
I get the mouse to move through vb? like move it from one corner to another corner? Or something to this exstant?

Bob Baddeley
Jan 23rd, 2000, 04:55 AM
There is an answer in vb-world tips under mouse/keyboard.

Civil78
Jan 23rd, 2000, 05:07 AM
Put this in a module

Option Explicit

Type POINTAPI
x As Long
y As Long
End Type

Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

And Final Put this to an form and create a command button called command1


Sub MoveMouse(x As Single, y As Single)
Dim pt As POINTAPI
pt.x = x
pt.y = y
ClientToScreen hwnd, pt
SetCursorPos pt.x, pt.y
End Sub

Private Sub Command1_Click()
MoveMouse 300, 150
End Sub

Final if you click the button mouse moves in 300,150 position(x,y) of your form
With MoveMouse procedure you can put mouse cursor in x,y position like

MoveMouse x,y

To make mouse to move in a line you can use this procedure

Sub domove(px1, py1, px2, py2 As Single)
Dim pb As Single
Dim i As Single
Dim pyold As Single
If (px1 - px2) <> 0 Then
pb = (py1 - py2) / (px1 - px2) '
pyold = py1
If (px1 - px2) < 0 Then
For i = px1 To px2
If (py1 - py2) < 0 Then
pyold = Abs(py1 + (i - px1) * pb)
MoveMouse Abs(Int(i)), Abs(Int(pyold))
End If
If (py1 - py2) > 0 Then
pyold = Abs(py1 + (i - px1) * pb)
MoveMouse Abs(Int(i)), Abs(Int(pyold))
End If
Next i
End If
If (px1 - px2) > 0 Then
For i = px1 To px2 Step -1
If (py1 - py2) < 0 Then
pyold = Abs(py1 + (i - px1) * pb)
MoveMouse Abs(Int(i)), Abs(Int(pyold))
End If
If (py1 - py2) > 0 Then
pyold = Abs(py1 + (i - px1) * pb)
MoveMouse Abs(Int(i)), Abs(Int(pyold))
End If
Next i
End If
End If
End Sub

Use it like this

domove (0,0,400,300)

This move mouse pointer from 0,0 position to 400,300 position

It's simple code.... bye

Created By Antony Tsiukas

Juan Carlos Rey
Jan 23rd, 2000, 07:49 AM
Be careful! I wouldn't like my mouse roaming all over my desktop!

------------------
I wish I was patient... RIGHT NOW!

Jan 23rd, 2000, 08:44 AM
i guess i got my answer from the article.