Simulating mouse movement and actions in C#

1
Categories: .NET, C#
Posted on: 2nd November 2009 by: Andrei

I’ve recently worked on a project that required my application to move the mouse and left click on some items.

Here’s a simple working solution that will handle mouse movement and left clicking.


using System.Runtime.InteropServices;

private const UInt32 MouseEventLeftDown = 0x0002;

private const UInt32 MouseEventLeftUp = 0x0004;

 [DllImport("user32.dll")]
 private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);

private void leftClick()
 {
 mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
 mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr());
 }

private void mouseMove()
 {
 Cursor.Position = new Point(Cursor.Position.X - 200, Cursor.Position.Y - 200);
 }