用C#模拟的鼠标点击
程序代码:using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 鼠标单击模拟
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Start();
}
[DllImport("user32.dll")]
public static extern void mouse_event
(
int dwFlags,
int dx,
int dy,
int dwData,
int dwExtraInfo
);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
const int MOUSEEVENTF_MOVE = 0x0001;
const int MOUSEEVENTF_LEFTDOWN = 0x0002;
const int MOUSEEVENTF_LEFTUP = 0x0004;
const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
const int MOUSEEVENTF_RIGHTUP = 0x0010;
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const int MOUSEEVENTF_MIDDLEUP = 0x0040;
const int MOUSEEVENTF_ABSOLUTE = 0x8000;
private void button1_Click(object sender, EventArgs e)
{
timer2.Start();
if (textBox3.Text != "")
timer3.Start();
}
private void timer2_Tick(object sender, EventArgs e)
{
int x,y;
string[] str;
str=textBox2.Text.Split(',');
x=Convert.ToInt32(str[0]);
y=Convert.ToInt32(str[1]);
SetCursorPos(x, y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
private void timer1_Tick(object sender, EventArgs e)
{
Point pos = Cursor.Position;
textBox1.Text = pos.X.ToString() +','+ pos.Y.ToString();
}
private void timer3_Tick(object sender, EventArgs e)
{
int x=0, y=0;
string[] sta;
sta = textBox3.Text.Split(',');
x = Convert.ToInt32(sta[0]);
y = Convert.ToInt32(sta[1]);
SetCursorPos(x, y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
private void button2_Click(object sender, EventArgs e)
{
timer2.Stop();
timer3.Stop();
}
}
}输入2个坐标可以在屏幕的那2个坐标位置重复点击。









