下面是完整代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace AutoMouseClick
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public const int MOUSEEVENTF_LEFTDOWN = 0x2;
public const int MOUSEEVENTF_LEFTUP = 0x4;
}
[DllImport("user32.dll", EntryPoint="mouse_event")]
public static extern void mouse_event (
int dwFlags,
int dx,
int dy,
int cButtons,
int dwExtraInfo
);
private void timer1_Tick(object sender, System.EventArgs e)
{
Random rand = new Random();
Cursor.Position = new Point(rand.Next(1024),rand.Next(768));
if(checkBox1.Checked)
{
mouse_event(MOUSEEVENTF_LEFTDOWN,Cursor.Position.X,Cursor.Position.Y,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,Cursor.Position.X,Cursor.Position.Y,0,0);
}
}