|
|
Title | Record and play back mouse movements |
Description | This example shows how to record and play back mouse movements in Visual Basic 6. |
Keywords | mouse, move, mouse_event, API |
Categories | Tips and Tricks, API |
|
|
The program stores information about the mouse's position in the following PointData class.
|
|
Public X As Single
Public Y As Single
Public TheTime As Single
|
|
The program's MouseDown, MouseMove, and MouseUp event handlers record mouse events.
|
|
Private Sub picCanvas_MouseDown(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
picCanvas.Cls
m_Drawing = True
m_NumPoints = 1
ReDim m_PointData(1 To m_NumPoints)
Set m_PointData(1) = New PointData
m_PointData(1).X = X
m_PointData(1).Y = Y
m_PointData(1).TheTime = Now
End Sub
Private Sub picCanvas_MouseMove(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
If Not m_Drawing Then Exit Sub
m_NumPoints = m_NumPoints + 1
ReDim Preserve m_PointData(1 To m_NumPoints)
Set m_PointData(m_NumPoints) = New PointData
m_PointData(m_NumPoints).X = X
m_PointData(m_NumPoints).Y = Y
m_PointData(m_NumPoints).TheTime = Now
picCanvas.Line (m_PointData(m_NumPoints - 1).X, _
m_PointData(m_NumPoints - 1).Y)-(X, Y)
End Sub
Private Sub picCanvas_MouseUp(Button As Integer, Shift As _
Integer, X As Single, Y As Single)
m_Drawing = False
End Sub
|
|
When you click the Play Back button, the code enables the tmrDraw timer.
The Timer event handler draws a line from the last point drawn to the next point in the recorded series. It converts the point into screen coordinates, maps the result into mouse coordinates, and moves the mouse to that position.
Next the timer sets its Interval property to the amount of time that ellapsed between the recording of this point and the next. This makes the program draw the points at roughly the same speed as they were recorded.
|
|
Private Sub cmdPlayBack_Click()
If m_NumPoints < 2 Then Exit Sub
cmdPlayBack.Enabled = False
picCanvas.Cls
m_LastPoint = 2
tmrDraw.Enabled = True
End Sub
Private Sub tmrDraw_Timer()
Dim pt As POINTAPI
Dim X As Long
Dim Y As Long
Dim new_interval As Long
picCanvas.Line _
(m_PointData(m_LastPoint - 1).X, _
m_PointData(m_LastPoint - 1).Y)- _
(m_PointData(m_LastPoint).X, _
m_PointData(m_LastPoint).Y)
' Convert the next point into screen coordinates.
pt.X = m_PointData(m_LastPoint).X
pt.Y = m_PointData(m_LastPoint).Y
ClientToScreen picCanvas.hwnd, pt
' mouse_event moves in a coordinate system where
' (0, 0) is in the upper left corner and
' (65535,65535) is in the lower right corner.
' Convert into this coordinate system.
X = pt.X * 65535 / ScaleX(Screen.Width, vbTwips, _
vbPixels)
Y = pt.Y * 65535 / ScaleY(Screen.Height, vbTwips, _
vbPixels)
' Move the mouse.
mouse_event _
MOUSEEVENTF_ABSOLUTE + _
MOUSEEVENTF_MOVE, _
X, Y, 0, 0
' See if this was the last point.
m_LastPoint = m_LastPoint + 1
tmrDraw.Enabled = False
If m_LastPoint >= m_NumPoints Then
cmdPlayBack.Enabled = True
Else
new_interval = _
(m_PointData(m_LastPoint).TheTime - _
m_PointData(m_LastPoint - 1).TheTime)
If new_interval < 1 Then new_interval = 1
tmrDraw.Interval = new_interval
tmrDraw.Enabled = True
End If
End Sub
|
|
|
|
|
|