实现:鼠标自动点击,设置鼠标点击频率和位置,获取鼠标当前位置的坐标,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace automatic
{
public partial class zidong : Form
{
public zidong()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
LB_Numbers.Text = ClickCount.ToString();
}
private void btn_exit_Click(object sender, EventArgs e)
{
try
{
if (controlThread != null)
controlThread.Abort();//停止线程
}
catch (Exception)
{
Application.DoEvents();
}
this.Close();
}
[DllImport("User32")]
public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
[DllImport("User32")]
public extern static void SetCursorPos(int x, int y);
[DllImport("User32")]
public extern static bool GetCursorPos(out POINT p);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
public enum MouseEventFlags //鼠标按键的ASCLL码
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
Wheel = 0x0800,
Absolute = 0x8000
}
private void AutoClick(int x, int y)
{
POINT p = new POINT();
GetCursorPos(out p);
try
{
SetCursorPos(x, y);//设置光标位置
mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);//自动按下的按键
mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);
}
finally
{
SetCursorPos(p.X, p.Y);
}
}
Point CursorPosition = new Point(0, 0);//存放着当前鼠标的位置坐标
int ClickCount = 0;//存放着鼠标左键点击次数
Thread controlThread;//主线控制程对象
int x=480;
int y = 483;
int span = 2000;
//线程主要处理的函数
private void ThreadRunMethod()
{
while (true)
{
//CursorPosition = Cursor.Position;//自动获取鼠标的位置
AutoClick(x, y); //(CursorPosition.X, CursorPosition.Y);设置鼠标点击的位置
Thread.Sleep(span);
}
}
private void ThreadRunMethod2()
{
while (true)
{
CursorPosition = Cursor.Position;//自动获取鼠标的位置
AutoClick(CursorPosition.X, CursorPosition.Y);//设置鼠标点击的位置
Thread.Sleep(span);
}
}
//点击开始,开始鼠标左键自动点击
private void BT_Start_Click(object sender, EventArgs e)
{
if (txt_span.Text.Length != 0)
span = Convert.ToInt32(txt_span.Text.Trim());
try
{
if (controlThread == null)
{
controlThread = new Thread(new ThreadStart(ThreadRunMethod2));
controlThread.Start();
}
}
catch (Exception)
{
Application.DoEvents();
}
BT_Stop.Enabled = true;
BT_Start.Enabled = false;
button2.Enabled = false;
}
private void BT_TestClick_Click(object sender, EventArgs e)
{
ClickCount++;
LB_Numbers.Text = ClickCount.ToString();
}
private void BT_Stop_Click(object sender, EventArgs e)
{
try
{
if (controlThread != null)
controlThread.Abort();
controlThread = null;
}
catch (Exception)
{
Application.DoEvents();
}
BT_Start.Enabled = true;
button2.Enabled = true;
BT_Stop.Enabled = false;
}
//设置位置
private void button1_Click(object sender, EventArgs e)
{
if (txt_x.Text.Length != 0)
x = Convert.ToInt32(txt_x.Text.Trim());
if (txt_y.Text.Length != 0)
y = Convert.ToInt32(txt_y.Text.Trim());
if (txt_span.Text.Length != 0)
span = Convert.ToInt32(txt_span.Text.Trim());
try
{
if (controlThread == null)
{
controlThread = new Thread(new ThreadStart(ThreadRunMethod));
controlThread.Start();
}
}
catch (Exception)
{
Application.DoEvents();
}
BT_Stop.Enabled = true;
BT_Start.Enabled = false;
}
//开始按钮
private void button2_Click(object sender, EventArgs e)
{
span = 2000;
try
{
if (controlThread == null)
{
controlThread = new Thread(new ThreadStart(ThreadRunMethod));
controlThread.Start();
}
}
catch (Exception)
{
Application.DoEvents();
}
BT_Stop.Enabled = true;
BT_Start.Enabled = false;
button1.Enabled = true;
}
/// <summary>
/// 显示当前鼠标位置
/// </summary>
private void ShowCursorPosition()
{
string str=null ;
str += "X:" + Cursor.Position.X.ToString() + " ";
str += "Y:" + Cursor.Position.Y.ToString() ;
txt_position.Text = str;
}
// 显示当前鼠标位置的按钮
private void btn_position_Click(object sender, EventArgs e)
{
ShowCursorPosition();
}
private void zidong_Load(object sender, EventArgs e)
{
ToolTip tooltip = new ToolTip();//鼠标悬浮时显示的提示
tooltip.ShowAlways = true;
tooltip.AutoPopDelay = 2000;
tooltip.InitialDelay = 50;
tooltip.ReshowDelay = 50;
tooltip.SetToolTip(this.btn_position, "按Alt+d显示当前鼠标坐标");
tooltip.SetToolTip(this.button2, "开始");
}
}
}