Simulate a mouse movement and click the mouse in C#

This program uses the mouse_event API function to move the mouse and simulate a mouse click.

The program's Paint event handler draws some circles around a target point so you can see where it is.

// The mouse's target location.
private Point m_Target = new Point(200, 150);

// Draw a target.
private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    for (int i = 2; i <= 20; i += 5)
    {
        e.Graphics.DrawEllipse(Pens.Red, m_Target.X - i - 1, m_Target.Y - i - 1, 2 * i, 2 * i);
    }
}

When you click the mouse on the form, the program draws an X where you clicked.

// Draw an X where the user clicked.
private void Form1_Click(object sender, EventArgs e)
{
    // Get the mouse position.
    Point pt = MousePosition;

    // Convert to screen coordinates.
    pt = this.PointToClient(pt);

    Graphics gr = this.CreateGraphics();
    gr.SmoothingMode = SmoothingMode.AntiAlias;
    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);
}

When you click the Move & Click button, the program executes the following code.

// Move the mouse and click it.
private void btnMoveClick_Click(object sender, EventArgs e)
{
    // Convert the target to absolute screen coordinates.
    Point pt = this.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.
    Rectangle screen_bounds = Screen.GetBounds(pt);
    uint x = (uint)(pt.X * 65535 / screen_bounds.Width);
    uint y = (uint)(pt.Y * 65535 / screen_bounds.Height);

    // Move the mouse.
    mouse_event(
        (uint)(MouseEventFlags.ABSOLUTE | MouseEventFlags.MOVE),
        x, y, 0, 0);

    // Click there.
    mouse_event(
        (uint)(MouseEventFlags.ABSOLUTE | MouseEventFlags.MOVE |
            MouseEventFlags.LEFTDOWN | MouseEventFlags.LEFTUP),
        x, y, 0, 0);
}

The code first converts the target point's coordinates from form coordinates 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.

Next the program uses the mouse_event API function to move the mouse to the target position and to click the mouse. Because this simulates a normal mouse click event, the form's Click event handler executes and draws an X at the target point.

See the code to learn how the mouse_event API function is declared and how the MouseEventFlags enumeration is defined.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.