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
 
 
 
 
 
TitleMove the mouse to a particular position and click it
DescriptionThis example shows how to move the mouse to a particular position and click it in Visual Basic 6.
Keywordsmouse, click, move, mouse_event, API
CategoriesTips and Tricks, API
 
This program displays two forms. The target form uses the following code to make its region a circle with a hole in it.
 
Private Sub Form_Load()
Const INCH As Integer = 1440
Dim pix_wid As Long
Dim pix_hgt As Long
Dim rgn1 As Long
Dim rgn2 As Long
Dim rgn3 As Long

    Me.Width = 0.5 * INCH
    Me.Height = 0.5 * INCH

    pix_wid = Me.ScaleX(Me.Width, vbTwips, vbPixels)
    pix_hgt = Me.ScaleY(Me.Height, vbTwips, vbPixels)
    rgn1 = CreateEllipticRgn(0, 0, pix_wid, pix_hgt)
    rgn2 = CreateEllipticRgn( _
        pix_wid \ 2 - 5, pix_hgt \ 2 - 5, _
        pix_wid \ 2 + 5, pix_hgt \ 2 + 5)

    rgn3 = CreateEllipticRgn(0, 0, 10, 10)
    CombineRgn rgn3, rgn1, rgn2, RGN_DIFF
    DeleteObject rgn1
    DeleteObject rgn2

    SetWindowRgn Me.hwnd, rgn3, True
    DeleteObject rgn3
End Sub
 
The main form has text boxes for X and Y coordinates in twips. If you click the Move button, the following code centers the target form on the position (so the hole is at the position). It then moves the mouse to that position.
 
Private Sub cmdMove_Click()
Dim X As Long
Dim Y As Long
Dim mouse_x As Long
Dim mouse_y As Long

    ' Get the coordinates.
    X = CLng(txtX.Text)
    Y = CLng(txtY.Text)

    ' Position the target form.
    frmTarget.Left = X - frmTarget.Width / 2
    frmTarget.Top = Y - frmTarget.Height / 2

    ' 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 the coordinates.
    mouse_x = CLng(X * 65535 / Screen.Width)
    mouse_y = CLng(Y * 65535 / Screen.Height)

    ' Position the mouse.
    mouse_event _
        MOUSEEVENTF_ABSOLUTE + _
        MOUSEEVENTF_MOVE, _
        mouse_x, mouse_y, 0, 0
End Sub
 
When you click the Click button, the program uses the following code to position the target form and click the mouse at the target position.
 
Private Sub cmdClick_Click()
Dim X As Long
Dim Y As Long
Dim mouse_x As Long
Dim mouse_y As Long

    ' Get the coordinates.
    X = CLng(txtX.Text)
    Y = CLng(txtY.Text)

    ' Position the target form.
    frmTarget.Left = X - frmTarget.Width / 2
    frmTarget.Top = Y - frmTarget.Height / 2

    ' 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 the coordinates.
    mouse_x = CLng(X * 65535 / Screen.Width)
    mouse_y = CLng(Y * 65535 / Screen.Height)

    ' Click at that position.
    mouse_event _
        MOUSEEVENTF_ABSOLUTE Or _
        MOUSEEVENTF_MOVE Or _
        MOUSEEVENTF_LEFTDOWN Or _
        MOUSEEVENTF_LEFTUP, _
        mouse_x, mouse_y, 0, 0
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated