[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Simulate mouse movement and clicks in C#

[Simulate mouse movement and clicks in C#]

This program uses the mouse_event API function to simulate mouse movement and to 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 = 5; 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 following code 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); using (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.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.