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
 
 
 
 
 
TitleSimulate a mouse movement and click the mouse in Visual Basic 2005
DescriptionThis example shows how to simulate a mouse movement and click the mouse in Visual Basic 2005 by using the mouse_event API function.
KeywordsVB2005, Visual Basic 2005, mouse move, click, mouse_event, API
CategoriesAPI, VB.NET, Tips and Tricks
 
This program uses the mouse_event API function. The program's Paint event handler draws some circles around a target point so you can see where it is.
 
' Draw a target.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    For i As Integer = 2 To 20 Step 5
        e.Graphics.DrawEllipse(Pens.Red, m_Target.X - i, _
            m_Target.Y - i, 2 * i, 2 * i)
    Next
End Sub
 
When you click the Move & Click button, the program converts the target point's coordinates from form to screen coordinates. It then converts the result into the mouse's special coordinate system that runs from 0 to 65535 in the X and Y directions. The program then uses the mouse_event API function to move the mouse to the target position and to click the mouse.
 
Private Sub btnMoveClick_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnMoveClick.Click
    ' Convert the target to absolute screen coordinates.
    Dim pt As Point = Me.PointToScreen(m_Target)

    ' 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.
    Dim screen_bounds As Rectangle = Screen.GetBounds(pt)
    Dim x As Integer = CInt(pt.X * 65535 / _
        screen_bounds.Width)
    Dim y As Integer = CInt(pt.Y * 65535 / _
        screen_bounds.Height)

    ' Move the mouse.
    mouse_event( _
        MOUSEEVENTF_ABSOLUTE + _
        MOUSEEVENTF_MOVE, _
        x, y, 0, 0)

    ' Click there.
    mouse_event( _
        MOUSEEVENTF_ABSOLUTE Or _
        MOUSEEVENTF_MOVE Or _
        MOUSEEVENTF_LEFTDOWN Or _
        MOUSEEVENTF_LEFTUP, _
        x, y, 0, 0)
End Sub
 
When you click the mouse on the form, the program draws an X where you clicked. When you use the Move & Click button to simulate a mouse click, you should also see the X.
 
' Draw an X where the user clicked.
Private Sub Form1_Click(ByVal sender As Object, ByVal e As _
    System.EventArgs) Handles Me.Click
    ' Get the mouse position.
    Dim pt As Point = MousePosition()

    ' Convert to screen coordinates.
    pt = Me.PointToClient(pt)

    Dim gr As Graphics = Me.CreateGraphics()
    gr.DrawLine(Pens.Blue, pt.X - 5, pt.Y - 5, pt.X + 5, _
        pt.Y + 5)
    gr.DrawLine(Pens.Blue, pt.X + 5, pt.Y - 5, pt.X - 5, _
        pt.Y + 5)
End Sub
 
This example was written in Visual Basic 2005 but you should be able to copy and paste the code to make it work in earlier versions of Visual Basic .NET.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated