How can I simulate a mouse click at a certain position on the screen?

97,775

Solution 1

Here's a code that is using unmanaged functions to simulate mouse clicks :

//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);

[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;

//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
    SetCursorPos(xpos, ypos);
    mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}

To keep the mouse pressed for a specific duration you can Sleep() the thread that is executing this function, for example :

mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
System.Threading.Thread.Sleep(1000);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);

The above code will keep the mouse pressed for 1 second unless the user presses the releases the mouse button. Also, make sure to not execute this code on the main UI thread as it will cause it to hang.

Solution 2

You can move by XY position. Example below:

windows.Forms.Cursor.Position = New System.Drawing.Point(Button1.Location.X + Me.Location.X + 50, Button1.Location.Y + Me.Location.Y + 30)

To click, you can use the below code:

using System.Runtime.InteropServices;

private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
[DllImport("user32.dll")]
    private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,             uint dwExtraInf);
private void btnSet_Click(object sender, EventArgs e)
    {
        int x = Convert.ToInt16(txtX.Text);//set x position 
        int y = Convert.ToInt16(txtY.Text);//set y position 
        Cursor.Position = new Point(x, y);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);//make left button down
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);//make left button up
    }

Credit to JOHNYKUTTY

Share:
97,775
MonsterMMORPG
Author by

MonsterMMORPG

Hello. I am the only owner and developer of web based online MMORPG game MonsterMMORPG. I am a computer engineer from Turkey and i am currently doing MA at computer engineering. I am specialized with C# & ASP.net. http://www.monstermmorpg.com/ MonsterMMORPG is a Free To Play Browser Based Online Monster MMORPG Game Better Than Online Pokemon Games You will love it's awesome Monsters We have many different unique features. So i suggest you to check them out. Our game is similiar with Pokemon games but it is unique. Like diablo and torch light. You should visit following sites related to us MonsterMMORPG Facebook Pokemon Games Lovers Facebook Application MonsterMMORPG Youtube Channel Monster Game Forum Canavar Oyunu Forum Pokemon Fakemon DeviantArt MonsterMMORPG Google Plus MonsterMMORPG Twitter MonsterMMORPG Review On Browsergamez MonsterMMORPG Review On mmohuts MonsterMMORPG Developer Blog At MMORPG.com MonsterMMORPG Review On onrpg MonsterMMORPG On GameSpot MonsterMMORPG Wiki MonsterMMORPG On 1UP MonsterMMORPG Digg MonsterMMORPG Official Google Plus Page

Updated on July 23, 2022

Comments

  • MonsterMMORPG
    MonsterMMORPG almost 2 years

    What I want to do is to manipulate the mouse. It will be a simple macro for my own purposes. So it will move my mouse to certain position on the screen and click like I am clicking with certain interval.

  • Vlad
    Vlad over 12 years
    The question is on WPF, not Forms.
  • MonsterMMORPG
    MonsterMMORPG over 12 years
    does not work : windows.Forms.Cursor.Position = New System.Drawing.Point(Button1.Location.X + Me.Location.X + 50, Button1.Location.Y + Me.Location.Y + 30) . also i tried this at forms not mention to will not work at wpf
  • Yablargo
    Yablargo over 10 years
    This is FAR simplier than other solutions I have seen here. Thanks
  • john
    john over 9 years
    This code works fine to bring the pointer to the desired coordinates on the screen but for some reason the click does not work for me. Any idea why?
  • Li3ro
    Li3ro about 8 years
    Here you can have the codes for other mouse buttons : msdn.microsoft.com/en-us/library/windows/desktop/…