Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleProgrammatically move the mouse and simulate a click
Keywordsmouse, click, move, simulate
CategoriesTips and Tricks, API
 
This program moves the mouse from its current position to the middle of a PictureBox and then simulates a mouse click. The PictureBox's MouseDown, MouseUp, and Click event handlers displays messages in a TextBox so you can see that the event looks as if the user clicked the PictureBox.

The code starts by using the GetCursorPos API API function to get the mouse's current position. It converts the position into mouse coordinates which range from (0, 0) in the upper left corner to (65535, 65535) in the lower right.

Next the program uses ClientToScreen to see where the middle of the target PictureBox is in screen coordinates. It converts the result into the same mouse coordinate system.

The code then performs 3000 loops moving the mouse closer to its final destination. At each position, it uses the mouse_event API function to move the mouse.

(If you just want to jump the mouse to its destination, you can omit the intermediate moves. They are used here to make things easier to understand.)

Finally the program uses mouse_event to simulate a left button down and left button up.

 
' Simulate moving the mouse to the center of the
' PictureBox and clicking.
Private Sub cmdClick_Click()
Const NUM_MOVES = 3000
Dim pt As POINTAPI
Dim cur_x As Single
Dim cur_y As Single
Dim dest_x As Single
Dim dest_y As Single
Dim dx As Single
Dim dy As Single
Dim i As Integer

    ' Things are easier working in pixels.
    ScaleMode = vbPixels
    picClicker.ScaleMode = vbPixels

    ' 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.

    ' Get the current mouse coordinates and convert
    ' them into this new system.
    GetCursorPos pt
    cur_x = pt.X * 65535 / ScaleX(Screen.Width, vbTwips, _
        vbPixels)
    cur_y = pt.Y * 65535 / ScaleY(Screen.Height, vbTwips, _
        vbPixels)

    ' Convert the coordinates of the center of the
    ' picClicker PictureBox into this new system.
    pt.X = picClicker.ScaleWidth / 2
    pt.Y = picClicker.ScaleHeight / 2
    ClientToScreen picClicker.hwnd, pt
    dest_x = pt.X * 65535 / ScaleX(Screen.Width, vbTwips, _
        vbPixels)
    dest_y = pt.Y * 65535 / ScaleY(Screen.Height, vbTwips, _
        vbPixels)

    ' Move the mouse.
    dx = (dest_x - cur_x) / NUM_MOVES
    dy = (dest_y - cur_y) / NUM_MOVES
    For i = 1 To NUM_MOVES - 1
        cur_x = cur_x + dx
        cur_y = cur_y + dy
        mouse_event _
            MOUSEEVENTF_ABSOLUTE + _
            MOUSEEVENTF_MOVE, _
            cur_x, cur_y, 0, 0
        DoEvents
    Next i

    ' Move the mouse to its final destination and click it.
    mouse_event _
        MOUSEEVENTF_ABSOLUTE + _
        MOUSEEVENTF_MOVE + _
        MOUSEEVENTF_LEFTDOWN + _
        MOUSEEVENTF_LEFTUP, _
        dest_x, dest_y, 0, 0
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated