[C#]mouse_event and SetCursorPos

Posts 13 of 3 · Page 1 of 1
[C#]mouse_event and SetCursorPos
Lets start with the tuturial.

Since we're going to use imports from user32.dll, we'll need to let MSVC# know we want to be able to import functions from Dll's.
So add the following under the other 'using' statements:
Code:
using System.Runtime.InteropServices;
Now that is done we can do some defining and importing, the following code goes in 'public partial class Form1 : Form'
Code:
        private const int MOUSEEVENTF_LEFTDOWN = 0x2;
        private const int MOUSEEVENTF_LEFTUP = 0x4;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x8;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;

        [DllImport("USER32.dll", CallingConvention=CallingConvention.StdCall)]
        static extern void mouse_event(int dwFlags, int dx , int dy, int cButtons, int dwExtraInfo);

        [DllImport("USER32.dll", CallingConvention = CallingConvention.StdCall)]
        static extern void SetCursorPos(int X, int Y);
Now that you have those declared and the functions imported add a button to your form, name it whatever you like.

Double click it to have the OnClick event generated, and put the following code in it:

Code:
        Random myRand = new Random();
            int CursorX = myRand.Next(800);
            int CursorY = myRand.Next(600);
            SetCursorPos(CursorX, CursorY);
            mouse_event(MOUSEEVENTF_LEFTDOWN, CursorX, CursorY, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, CursorX, CursorY, 0, 0);
The above code will generate a random number between 0 and 800 for the X coordinate, and between 0 and 600 for the Y coordinate, then it sets the cursor's position to it and makes the user click there.

Now if you'd want to right click you'd use:
Code:
            mouse_event(MOUSEEVENTF_RIGHTDOWN, CursorX, CursorY, 0, 0);
            mouse_event(MOUSEEVENTF_RIGHTUP, CursorX, CursorY, 0, 0);
Have fun

Credits:
Unkn0wn_h4cker

Attached is the sample project
CursorStuff.rarattachment deleted · 22 downloads before removal
Code:
 Random myRand = new Random();
            int CursorX = myRand.Next(800);
            int CursorY = myRand.Next(600);
            SetCursorPos(CursorX, CursorY);
            mouse_event(MOUSEEVENTF_LEFTDOWN, CursorX, CursorY, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, CursorX, CursorY, 0, 0);
The code in red isn't necccessary is it? I always used it w/o it. And it'll probably be more efficient w/o it... less variable reads.
Quote Originally Posted by User1 View Post
Code:
 Random myRand = new Random();
            int CursorX = myRand.Next(800);
            int CursorY = myRand.Next(600);
            SetCursorPos(CursorX, CursorY);
            mouse_event(MOUSEEVENTF_LEFTDOWN, CursorX, CursorY, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, CursorX, CursorY, 0, 0);
The code in red isn't necccessary is it? I always used it w/o it. And it'll probably be more efficient w/o it... less variable reads.
if you comment out SetCursorPos it will perform silent clicks at those locations(as in clicking there without moving cursor).
Posts 13 of 3 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?