In this article I am going to show that how you can simulate mouse click programmatically (without mouse, physically) at a certain position on the monitor screen.
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class formMouseClick : Form
{
//To change the current position
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
//To raise mouse event
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int MOUSEEVENT_LEFTDOWN = 0x02;
private const int MOUSEEVENT_LEFTUP = 0x04;
private const int MOUSEEVENT_RIGHTDOWN = 0x08;
private const int MOUSEEVENT_RIGHTUP = 0x10;
public void DoMouseClick(int xPos, int yPos)
{
//Changing current position of mouse on the screen
SetCursorPos(xPos, yPos);
//Push mouse left button down
mouse_event(MOUSEEVENT_LEFTDOWN, (uint)xPos, (uint)yPos, 0, 0);
//Following line is to hold the button down for 1 second. You can skip this step.
System.Threading.Thread.Sleep(1000);
//Release mouse left button up
mouse_event(MOUSEEVENT_LEFTUP, (uint)xPos, (uint)yPos, 0, 0);
//Mouse click process have been completed
}
}
Posted Article in Programming