Saturday, December 11, 2010

C# Mouse

Have you ever wanted to manipulate the mouse cursor programmatically using C#?  Well, look no further, because this small Mouse class puts the mouse actions into a nice, small class you can use in your programs.

If you are still wondering what you can do with this simple class, here are a few examples:
  • Moving the mouse around so the user has no idea what's going on.
  • Making a macro (or "bot") for an online game.
  • Repeatedly clicking the same thing over and over again.

Many of the programs I will post will be based on this Mouse class!


Have a look at the code here (although you can download it at bottom of this post):


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;


static class Mouse
{
private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x02;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x04;
    private const UInt32 MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const UInt32 MOUSEEVENTF_RIGHTUP = 0x10;
    private const UInt32 MOUSEEVENTF_MIDDLEDOWN = 0x20;
    private const UInt32 MOUSEEVENTF_MIDDLEUP = 0x40;


    [DllImport("user32.dll")]
    private static extern void mouse_event(
        UInt32 dwFlags, // motion and click options
        UInt32 dx, // horizontal position or change
        UInt32 dy, // vertical position or change
        UInt32 dwData, // wheel movement
        IntPtr dwExtraInfo // application-defined information
    );


public static void Move(Point p)
{
Cursor.Position = p;
}


    public static void Move(int x, int y)
    {
        Mouse.Move(new Point(x, y));
    }


public static void Click()
{
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
}


public static void Click(Point p)
{
Mouse.Move(p);
Mouse.Click();
}


    public static void Click(int x, int y)
    {
        Mouse.Click(new Point(x, y));
    }


    public static void RightClick()
    {
        mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, new System.IntPtr());
        mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, new System.IntPtr());
    }


    public static void RightClick(Point p)
    {
        Mouse.Move(p);
        Mouse.RightClick();
    }


    public static void RightClick(int x, int y)
    {
        Mouse.RightClick(new Point(x, y));
    }


    public static void MiddleClick()
    {
        mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, new System.IntPtr());
        mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, new System.IntPtr());
    }


    public static void MiddleClick(Point p)
    {
        Mouse.Move(p);
        Mouse.MiddleClick();
    }


    public static void MiddleClick(int x, int y)
    {
        Mouse.MiddleClick(new Point(x, y));
    }
}



Most of the methods are self-explanatory (a.k.a. they do pretty much what the method names would suggest).  However, I give some examples anyway:

For each of these lines, assume that somePoint is some Point already defined, such as:
Point somePoint = new Point(13, 37);

Mouse.Move(somePoint);    // Moves the cursor to somePoint
Mouse.Move(123, 456);    // Moves the cursor to the pixel on your screen at (123, 456)
Mouse.Click();    // Left-click at current cursor position
Mouse.Click(somePoint);    // Left-click at somePoint
Mouse.Click(135, 246);    // Left-click at the pixel on your screen at (135, 246)
Mouse.RightClick();    // Right-click at current cursor position
Mouse.RightClick(somePoint);    // Right-click at somePoint
Mouse.RightClick(147, 258);    // Right-click at the pixel on your screen at (147, 258)
Mouse.MiddleClick();    // Middle-click at current cursor position
Mouse.MiddleClick(somePoint);    // Middle-click at somePoint
Mouse.MiddleClick(159, 260);    // Middle-click at the pixel on your screen at (159, 260)

After reading that, you're probably thinking, Why did I bother reading that?  It was so freakin' obvious!  Or maybe you were thinking, Why did that guy waste his time posting those examples?  Does he think I'm stupid or something?!


Anyway, you can download the C# class here (Mouse.cs).

Note: If you don't remember how to add this class to your C# project, see C# InputBox.

No comments:

Post a Comment