创建简单的自动点击器






4.28/5 (24投票s)
让我们创建为我们点击的软件

引言
有些电脑游戏或应用程序需要你反复点击屏幕上的同一位置。 想象一下,你通过点击一棵树来砍倒它。然后下一棵树出现在同一位置,所以过了一段时间,你需要再次点击那里。
作为人类,我们喜欢简化无聊的任务。 作为程序员,我们可以通过创建软件来简化这个任务,为我们做这件事。
编码
在自动点击器中我们需要做什么?为了简单起见,我们只设置两件事:点击的位置和点击的频率。 点击的位置可以存储在 Point
类型的变量中,时间间隔将在我们的 Timer
上设置。
第一件事是创建一个新的 Windows Form。然后我们添加我们的变量来保存点击位置。
//this will hold the location where to click
Point clickLocation = new Point(0,0);
接下来,我们需要设置点击的位置。其中一种方法是开始倒计时。在倒计时运行期间,用户可以将鼠标指向所需位置,倒计时结束后,我们可以获取其坐标。
我们可以通过激活适当的超时(例如 5 秒)来实现这一点,然后收集鼠标位置。所以,我们可以添加一个按钮,并添加以下 OnClick
事件处理程序
private void btnSetPoint_Click(object sender, EventArgs e)
{
timerPoint.Interval = 5000;
timerPoint.Start();
}
我们还需要创建一个计时器来帮助我们获取鼠标位置。我们可以从 Cursor
类的 Position
属性中获取鼠标位置。设置的位置可以例如在窗口标题上显示。
所以,让我们创建第二个计时器,并向其添加以下 Tick
处理程序
private void timerPoint_Tick(object sender, EventArgs e)
{
clickLocation = Cursor.Position;
//show the location on window title
this.Text = "autoclicker " + clickLocation.ToString();
timerPoint.Stop();
}
现在我们想设置主计时器的时间间隔(我们可以使用 NumericUpDown
控件)。 请记住,其时间间隔的下限应大于零,因为我们不能将计时器时间间隔设置为零毫秒。
现在开始点击本身。 每次我们的主计时器流逝时(在窗体中添加另一个 Timer
控件),我们都希望点击一个指定的位置。 我们无法在纯托管代码中执行此操作,我们必须使用 user32.dll 库的导入方法 SendInput
。 我们将使用它来合成鼠标点击。 当您使用 DllImport
时,不要忘记在代码的开头放置 using System.Runtime.InteropServices;
。
[DllImport("User32.dll", SetLastError = true)]
public static extern int SendInput(int nInputs, ref INPUT pInputs,
int cbSize);
方法 SendInput
使用三个参数
nInputs
指定pInputs
指向多少个结构pInputs
是 INPUT 结构数组的引用cbSize
是 INPUT 结构的大小
为了简单起见,我们将只使用一个 INPUT
结构。 我们也需要定义这个结构和一些常量
//mouse event constants
const int MOUSEEVENTF_LEFTDOWN = 2;
const int MOUSEEVENTF_LEFTUP = 4;
//input type constant
const int INPUT_MOUSE = 0;
public struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
public struct INPUT
{
public uint type;
public MOUSEINPUT mi;
};
每次我们的计时器流逝时,我们都想点击。 我们通过将光标移动到记忆的位置,然后设置 INPUT
结构并用所需的值填充它来完成它。 每次点击都包括按下鼠标按钮和释放鼠标按钮,因此我们需要发送两条消息 - 一条用于按钮按下(按下),另一条用于按钮弹起(释放)。
因此,使用此代码将句柄添加到主计时器的 Tick
事件
private void timer1_Tick(object sender, EventArgs e)
{
//set cursor position to memorized location
Cursor.Position = clickLocation;
//set up the INPUT struct and fill it for the mouse down
INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi.dx = 0;
i.mi.dy = 0;
i.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
i.mi.dwExtraInfo = IntPtr.Zero;
i.mi.mouseData = 0;
i.mi.time = 0;
//send the input
SendInput(1, ref i, Marshal.SizeOf(i));
//set the INPUT for mouse up and send it
i.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, ref i, Marshal.SizeOf(i));
}
最后 - 我们可以使用一个按钮来启动/停止自动点击功能。 让我们创建一个带有以下处理程序的按钮
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Interval = (int)numericUpDown1.Value;
if (!timer1.Enabled)
{
timer1.Start();
this.Text = "autoclicker - started";
}
else
{
timer1.Stop();
this.Text = "autoclicker - stopped";
}
}
后续步骤
就是这样。 这是一个非常简单的过程。 我们如何进一步改进这个程序?
我们可以...
- 使用随机的点击间隔
- 添加鼠标的随机移动,然后返回到我们点击的点
- 在“按住”按钮的同时移动鼠标
- 捕获屏幕上显示的内容并点击特定颜色
- 点击一系列点(创建某种宏)
历史
- 2006年8月30日:初始帖子