Page 1 of 1

Using the Mouse Virtually

#1 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Posted 07 April 2011 - 01:38 PM

This tutorial is about using the mouse from code in Visual C#. What this tutorial will cover is, getting and setting the mouse coordinates and right and left clicking. Now lets get started.

Clicking the Mouse
I am going to say this now: when you click the mouse with these functions it will click at the position the mouse is currently at. The next part of the tutorial tells you how to position the mouse.

First lets start off by creating our project. Open up Visual C# and create a windows form application. When we do that, rename it to whatever you want and change anything on the form you want and then we will put this little snippet of code in. It is a function so put it in wherever you would normally put a function.
        [System.Runtime.InteropServices.DllImport("user32.dll")] //this allows us to make a call to the native user32 dll
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); //this function allows us to click the mouse


To use that function to right and left click, we need to define some constants. These constants will define the numbers which decide what mouse button to click.
        public const int MOUSEEVENTF_LEFTDOWN = 0x02; //clicks the left mouse button
        public const int MOUSEEVENTF_LEFTUP = 0x04; //lets go of the left mouse button
        public const int MOUSEEVENTF_RIGHTDOWN = 0x08; //clicks the right mouse button
        public const int MOUSEEVENTF_RIGHTUP = 0x10; //lets go of the right mouse button


Those constants are used in the mouse_event function. You replace the dwFlags parameter with the constant you want and then you fill in the rest of the parameters with 0. All you have to do now is click. And I prefer to make a function to cover up the nasty insides of the native call with these functions:
        public void DoLeftMouseClick()
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); //clicks the left mouse button
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); //releases the left mouse button
        }
        public void DoRightMouseClick()
        {
            mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); //clicks the right mouse button
            mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); //releases the right mouse button
        }


After we do that we are ready to click! All we have to do now, is put DoLeftMouseClick(); or DoRightMouseClick(); wherever we want to click.

Getting and Setting the Mouse Position
Use this to set the mouse position and then use the functions above to click in a position you want.

If you are reading this hopefully you know that the computer screen is basically a grid. It has x and y coordinates and that is how you get and set the position of the mouse.

This part is much easier than the part above. We can make a property to get and set all of this and it is very simple. Here is the property:
        public Point CursorPosition
        {
            get
            {
                return System.Windows.Forms.Cursor.Position; //returns the current cursor position 
            }
            set
            {
                System.Windows.Forms.Cursor.Position = value; //sets the cursor position to the value set by the property
            }
        }


That is also very easy to use. To set the mouse position all you have to do is create a new instance of the point class and make sure that it is within the bounds of your screen. And then all you have to do is set the property with that point instance. Here is the code:
Point point = new Point(0,0); //creates a new point at the origin of the screen (top left corner)
CursorPosition = point; //sets the current cursor position to that point


It's very simple to set the cursor as you can see above but it is even easier to retrieve the cursor position as you can see with this code right here:
Point poing = CursorPosition; //creates a new point and sets it to the current cursor position with the property that we created


As you can see changing and getting the cursor position is much easier than clicking the mouse but hopefully after this tutorial you have a much better understanding of the mouse and how to work it in C# :)/>

Here is what your code should look very similar to in the end without comments and if the project is named DICTutorial:
        private void DICTutorial_Load(object sender, EventArgs e)
        {
            Point point = new Point(20, 20);
            CursorPosition = point;
            DoLeftMouseClick();
            DoLeftMouseClick();
        }

        public Point CursorPosition
        {
            get
            {
                return System.Windows.Forms.Cursor.Position; //returns the current cursor position 
            }
            set
            {
                System.Windows.Forms.Cursor.Position = value; //sets the cursor position to the value set by the property
            }
        }

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        public const int MOUSEEVENTF_LEFTDOWN = 0x02;
        public const int MOUSEEVENTF_LEFTUP = 0x04;
        public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        public const int MOUSEEVENTF_RIGHTUP = 0x10;

        public void DoLeftMouseClick()
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }
        public void DoRightMouseClick()
        {
            mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
            mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
        }

When you run that then the cursor immediately moves to 20,20 on the screen and double clicks. Again I hoped you enjoyed :)

EDIT: Removed dead link to old project.

This post has been edited by MATTtheSEAHAWK: 07 September 2013 - 02:35 PM


Is This A Good Question/Topic? 1
  • +

Replies To: Using the Mouse Virtually

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Posted 27 April 2011 - 11:40 AM

Nice tutorial!
Was This Post Helpful? 0
  • +
  • -

#3 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Posted 27 April 2011 - 04:47 PM

Thanks :)
I enjoyed making it because it was fun to make xD
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Posted 28 April 2011 - 11:07 AM

Just a quick question, do you know if its possible to make the program control the mouse wheel at all. I know you can middle click etc but have found nothing about using the mouse wheel

Thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1