// 鼠标连点器 - 绿色工具箱 // 单文件零依赖,C# 5.0 语法 // 功能:按设定间隔自动点击;可记多个点位循环点击;左/右/中键、单击/双击;随机抖动更接近真人 // 边界:只做本机鼠标模拟(SendInput),不注入进程、不读写他人内存、不联网 // 安全:F6 全局热键随时停止;停止后立刻停止所有定时器 using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; namespace GreenToolbox { public struct ClickPoint { public int X; public int Y; public ClickPoint(int x, int y) { X = x; Y = y; } public override string ToString() { return X + "," + Y; } } public static class ClickOps { public const int MinInterval = 10; public const int MaxInterval = 3600000; // 间隔:纯数字毫秒;支持 0.5s / 200ms / 2s 写法 public static bool TryParseInterval(string s, out int ms) { ms = 100; if (s == null) return false; s = s.Trim().ToLowerInvariant().Replace("毫秒", "ms").Replace("秒", "s").Replace(" ", ""); if (s.Length == 0) return false; bool times = false; if (s.EndsWith("ms")) { s = s.Substring(0, s.Length - 2); } else if (s.EndsWith("s")) { s = s.Substring(0, s.Length - 1); times = true; } double v; if (!double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out v)) return false; if (times) v = v * 1000.0; if (v < MinInterval || v > MaxInterval) return false; ms = (int)Math.Round(v); return true; } public static string IntervalText(int ms) { if (ms >= 1000 && ms % 1000 == 0) return (ms / 1000) + " 秒"; if (ms >= 1000) return (ms / 1000.0).ToString("0.0", CultureInfo.InvariantCulture) + " 秒"; return ms + " 毫秒"; } public static bool TryParsePoint(string s, out ClickPoint p) { p = new ClickPoint(0, 0); if (s == null) return false; s = s.Trim().Replace(",", ",").Replace(" ", ""); string[] parts = s.Split(','); if (parts.Length != 2) return false; int x, y; if (!int.TryParse(parts[0], NumberStyles.Integer, CultureInfo.InvariantCulture, out x)) return false; if (!int.TryParse(parts[1], NumberStyles.Integer, CultureInfo.InvariantCulture, out y)) return false; // 多显示器可以有负坐标;限制在合理范围防手滑 if (x < -100000 || x > 100000 || y < -100000 || y > 100000) return false; p = new ClickPoint(x, y); return true; } // 生成点击序列:点位循环使用,count<=0 视为无限(自测时按 limit 截断) public static List BuildSequence(List points, int count, int limit) { List r = new List(); if (points == null || points.Count == 0) return r; int total = count > 0 ? count : limit; if (total > limit) total = limit; for (int i = 0; i < total; i++) r.Add(points[i % points.Count]); return r; } // 固定种子的伪随机抖动,便于自测 public static int Jitter(int value, int range, ref uint seed) { if (range <= 0) return value; seed = seed * 1664525u + 1013904223u; int span = range * 2 + 1; int off = (int)((seed >> 16) % (uint)span) - range; return value + off; } public static string ClickKindText(int btn, bool dbl) { string b = btn == 1 ? "右键" : (btn == 2 ? "中键" : "左键"); return b + (dbl ? "双击" : "单击"); } } public class ClickForm : Form { List _points = new List(); ListBox _list; TextBox _interval, _count, _jitter, _x, _y; ComboBox _btn; CheckBox _dbl, _topMost, _restore; Button _add, _pick, _del, _clear, _start, _stop; Label _status, _hint; System.Windows.Forms.Timer _timer; System.Windows.Forms.Timer _hotkeyPoll; int _fired, _need; uint _seed = 20261019u; bool _running; bool _hotDown; [DllImport("user32.dll")] static extern bool RegisterHotKey(IntPtr hWnd, int id, uint mods, uint vk); [DllImport("user32.dll")] static extern bool UnregisterHotKey(IntPtr hWnd, int id); [DllImport("user32.dll")] static extern bool SetCursorPos(int x, int y); [DllImport("user32.dll")] static extern bool GetCursorPos(out Point p); [DllImport("user32.dll")] static extern void mouse_event(uint flags, uint dx, uint dy, uint data, UIntPtr extra); const int WM_HOTKEY = 0x0312; const int HotId = 0x4A21; const uint MOUSEEVENTF_LEFTDOWN = 0x0002, MOUSEEVENTF_LEFTUP = 0x0004; const uint MOUSEEVENTF_RIGHTDOWN = 0x0008, MOUSEEVENTF_RIGHTUP = 0x0010; const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020, MOUSEEVENTF_MIDDLEUP = 0x0040; string StorePath { get { string dir = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "连点器配置"); try { Directory.CreateDirectory(dir); } catch { dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GreenToolbox/连点器配置"); Directory.CreateDirectory(dir); } return Path.Combine(dir, "points.txt"); } } public ClickForm() { Text = "鼠标连点器 - 绿色工具箱"; ClientSize = new Size(560, 520); MinimumSize = new Size(520, 470); StartPosition = FormStartPosition.CenterScreen; Font = new Font("Microsoft YaHei UI", 9f); BackColor = Color.White; Label title = new Label(); title.Text = "鼠标连点器"; title.Font = new Font("Microsoft YaHei UI", 14f, FontStyle.Bold); title.SetBounds(16, 12, 300, 30); _hint = new Label(); _hint.Text = "F6 开始 / 停止(全局生效) 点「取当前鼠标位置」再摆动鼠标即可记录点位"; _hint.ForeColor = Color.FromArgb(110, 110, 110); _hint.SetBounds(16, 44, 520, 20); GroupBox gPoints = new GroupBox(); gPoints.Text = "点击点位(按顺序循环)"; gPoints.SetBounds(16, 70, 528, 210); _list = new ListBox(); _list.SetBounds(12, 24, 330, 140); _list.IntegralHeight = false; Label lx = new Label(); lx.Text = "X"; lx.SetBounds(354, 28, 16, 20); _x = new TextBox(); _x.SetBounds(372, 25, 60, 24); _x.Text = "0"; Label ly = new Label(); ly.Text = "Y"; ly.SetBounds(440, 28, 16, 20); _y = new TextBox(); _y.SetBounds(458, 25, 58, 24); _y.Text = "0"; _add = new Button(); _add.Text = "添加到列表"; _add.SetBounds(354, 56, 162, 28); _add.Click += delegate { AddTypedPoint(); }; _pick = new Button(); _pick.Text = "取当前鼠标位置"; _pick.SetBounds(354, 90, 162, 28); _pick.Click += delegate { Point p; if (GetCursorPos(out p)) { _x.Text = p.X.ToString(); _y.Text = p.Y.ToString(); AddTypedPoint(); } }; _del = new Button(); _del.Text = "删除选中"; _del.SetBounds(354, 124, 78, 28); _del.Click += delegate { RemoveSelected(); }; _clear = new Button(); _clear.Text = "清空"; _clear.SetBounds(438, 124, 78, 28); _clear.Click += delegate { _points.Clear(); RefreshList(); }; Label tip = new Label(); tip.Text = "提示:点位可以只留一个(就在当前位置连点);留空则点鼠标当前位置。"; tip.ForeColor = Color.FromArgb(120, 120, 120); tip.SetBounds(12, 170, 500, 20); GroupBox gSet = new GroupBox(); gSet.Text = "点击设置"; gSet.SetBounds(16, 288, 528, 122); Label l1 = new Label(); l1.Text = "间隔"; l1.SetBounds(14, 26, 36, 20); _interval = new TextBox(); _interval.SetBounds(52, 23, 90, 24); _interval.Text = "100"; Label l1b = new Label(); l1b.Text = "毫秒(可写 0.5s)"; l1b.SetBounds(148, 26, 150, 20); l1b.ForeColor = Color.FromArgb(120, 120, 120); Label l2 = new Label(); l2.Text = "次数"; l2.SetBounds(300, 26, 36, 20); _count = new TextBox(); _count.SetBounds(338, 23, 70, 24); _count.Text = "0"; Label l2b = new Label(); l2b.Text = "0 = 一直点"; l2b.SetBounds(414, 26, 90, 20); l2b.ForeColor = Color.FromArgb(120, 120, 120); Label l3 = new Label(); l3.Text = "按键"; l3.SetBounds(14, 60, 36, 20); _btn = new ComboBox(); _btn.DropDownStyle = ComboBoxStyle.DropDownList; _btn.Items.AddRange(new object[] { "左键", "右键", "中键" }); _btn.SelectedIndex = 0; _btn.SetBounds(52, 57, 80, 24); _dbl = new CheckBox(); _dbl.Text = "双击"; _dbl.SetBounds(144, 58, 70, 22); Label l4 = new Label(); l4.Text = "随机抖动"; l4.SetBounds(220, 60, 64, 20); _jitter = new TextBox(); _jitter.SetBounds(288, 57, 44, 24); _jitter.Text = "0"; Label l4b = new Label(); l4b.Text = "像素(0 = 精确位置)"; l4b.SetBounds(338, 60, 170, 20); l4b.ForeColor = Color.FromArgb(120, 120, 120); _topMost = new CheckBox(); _topMost.Text = "窗口置顶"; _topMost.SetBounds(14, 90, 90, 22); _topMost.Checked = true; _restore = new CheckBox(); _restore.Text = "停止后把鼠标挪回原位"; _restore.SetBounds(110, 90, 200, 22); _restore.Checked = true; _start = new Button(); _start.Text = "开始(F6)"; _start.SetBounds(16, 422, 150, 40); _start.Font = new Font("Microsoft YaHei UI", 10.5f, FontStyle.Bold); _start.BackColor = Color.FromArgb(37, 148, 92); _start.ForeColor = Color.White; _start.FlatStyle = FlatStyle.Flat; _start.Click += delegate { StartClicking(); }; _stop = new Button(); _stop.Text = "停止(F6)"; _stop.SetBounds(178, 422, 130, 40); _stop.Enabled = false; _stop.Click += delegate { StopClicking("已停止"); }; _status = new Label(); _status.SetBounds(320, 430, 224, 24); _status.Text = "待机"; _status.ForeColor = Color.FromArgb(110, 110, 110); Controls.Add(title); Controls.Add(_hint); Controls.Add(gPoints); gPoints.Controls.Add(_list); gPoints.Controls.Add(lx); gPoints.Controls.Add(_x); gPoints.Controls.Add(ly); gPoints.Controls.Add(_y); gPoints.Controls.Add(_add); gPoints.Controls.Add(_pick); gPoints.Controls.Add(_del); gPoints.Controls.Add(_clear); gPoints.Controls.Add(tip); Controls.Add(gSet); gSet.Controls.Add(l1); gSet.Controls.Add(_interval); gSet.Controls.Add(l1b); gSet.Controls.Add(l2); gSet.Controls.Add(_count); gSet.Controls.Add(l2b); gSet.Controls.Add(l3); gSet.Controls.Add(_btn); gSet.Controls.Add(_dbl); gSet.Controls.Add(l4); gSet.Controls.Add(_jitter); gSet.Controls.Add(l4b); gSet.Controls.Add(_topMost); gSet.Controls.Add(_restore); Controls.Add(_start); Controls.Add(_stop); Controls.Add(_status); // 停靠布局按 z-order 逆序计算(注意:_list 的父是 gPoints,不能拿 Form 的 Controls 调) Controls.SetChildIndex(gPoints, 0); Controls.SetChildIndex(gSet, 1); Controls.SetChildIndex(_status, 2); Controls.SetChildIndex(_stop, 3); Controls.SetChildIndex(_start, 4); _timer = new System.Windows.Forms.Timer(); _timer.Tick += delegate { DoClick(); }; // F6 用 RegisterHotKey 更可靠;注册失败则退化为轮询(部分环境会拦截) Load += delegate { LoadPoints(); if (_topMost.Checked) TopMost = true; if (!RegisterHotKey(Handle, HotId, 0, 0x75)) StartHotkeyPoll(); }; FormClosing += delegate { StopClicking("已退出"); UnregisterHotKey(Handle, HotId); }; _topMost.CheckedChanged += delegate { TopMost = _topMost.Checked; }; } void StartHotkeyPoll() { _hotkeyPoll = new System.Windows.Forms.Timer(); _hotkeyPoll.Interval = 60; _hotkeyPoll.Tick += delegate { bool down = (Control.ModifierKeys == Keys.None) && GetAsyncKeyState(0x75) < 0; if (down && !_hotDown) { _hotDown = true; ToggleRunning(); } else if (!down && _hotDown) { _hotDown = false; } }; _hotkeyPoll.Start(); } [DllImport("user32.dll")] static extern short GetAsyncKeyState(int vk); protected override void WndProc(ref Message m) { if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == HotId) { ToggleRunning(); return; } base.WndProc(ref m); } void ToggleRunning() { if (_running) StopClicking("已停止"); else StartClicking(); } void AddTypedPoint() { ClickPoint p; if (!ClickOps.TryParsePoint(_x.Text + "," + _y.Text, out p)) { MessageBox.Show("点位格式不对,请填整数坐标。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } _points.Add(p); RefreshList(); SavePoints(); } void RemoveSelected() { int i = _list.SelectedIndex; if (i < 0 || i >= _points.Count) return; _points.RemoveAt(i); RefreshList(); SavePoints(); } void RefreshList() { _list.BeginUpdate(); _list.Items.Clear(); for (int i = 0; i < _points.Count; i++) _list.Items.Add((i + 1) + ". " + _points[i].X + ", " + _points[i].Y); _list.EndUpdate(); } void SavePoints() { try { StringBuilder sb = new StringBuilder(); foreach (ClickPoint p in _points) sb.AppendLine(p.ToString()); File.WriteAllText(StorePath, sb.ToString(), new UTF8Encoding(false)); } catch { } } void LoadPoints() { try { if (!File.Exists(StorePath)) return; foreach (string line in File.ReadAllLines(StorePath, Encoding.UTF8)) { ClickPoint p; if (ClickOps.TryParsePoint(line, out p)) _points.Add(p); } RefreshList(); } catch { } } void StartClicking() { if (_running) return; int interval; if (!ClickOps.TryParseInterval(_interval.Text, out interval)) { MessageBox.Show("间隔请填 10 到 3600000 之间的毫秒数,或写成 0.5s。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); _interval.Focus(); return; } int count = 0; if (!int.TryParse(_count.Text.Trim(), out count) || count < 0) { MessageBox.Show("次数请填 0(一直点)或正整数。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); _count.Focus(); return; } int jitter = 0; if (!int.TryParse(_jitter.Text.Trim(), out jitter) || jitter < 0 || jitter > 200) { MessageBox.Show("抖动请填 0 到 200 之间的像素数。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); _jitter.Focus(); return; } if (count > 0 && count < 1000) { // 小次数确认一下,避免误触发 } _need = count; _fired = 0; _running = true; _start.Enabled = false; _stop.Enabled = true; _status.Text = "运行中:第 0 次"; _status.ForeColor = Color.FromArgb(37, 148, 92); _timer.Interval = interval; _timer.Start(); } void DoClick() { int btn = _btn.SelectedIndex; bool dbl = _dbl.Checked; int jitter = 0; int.TryParse(_jitter.Text.Trim(), out jitter); Point origin; GetCursorPos(out origin); ClickPoint target; if (_points.Count > 0) target = _points[_fired % _points.Count]; else { target = new ClickPoint(origin.X, origin.Y); } int x = ClickOps.Jitter(target.X, jitter, ref _seed); int y = ClickOps.Jitter(target.Y, jitter, ref _seed); SetCursorPos(x, y); DoOne(btn); if (dbl) { Thread.Sleep(30); DoOne(btn); } _fired++; if (_need > 0 && _fired >= _need) { StopClicking("完成 " + _fired + " 次点击"); return; } _status.Text = "运行中:第 " + _fired + " 次" + (_need > 0 ? " / " + _need : ""); } void DoOne(int btn) { if (btn == 1) { mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, UIntPtr.Zero); } else if (btn == 2) { mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, UIntPtr.Zero); } else { mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, UIntPtr.Zero); mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, UIntPtr.Zero); } } void StopClicking(string msg) { if (_timer != null) _timer.Stop(); bool was = _running; _running = false; _start.Enabled = true; _stop.Enabled = false; _status.Text = msg + (was ? "(共 " + _fired + " 次)" : ""); _status.ForeColor = Color.FromArgb(110, 110, 110); } } public static class ClickSelfTest { static int _pass, _fail; static List _lines = new List(); static void Check(string name, object got, object want) { bool ok = string.Equals(got == null ? "" : got.ToString(), want == null ? "" : want.ToString()); if (ok) { _pass++; _lines.Add(" [ok] " + name); } else { _fail++; _lines.Add(" [!!] " + name + " got=" + got + " want=" + want); } } public static int Run(string dir) { _pass = 0; _fail = 0; _lines = new List(); _lines.Add("鼠标连点器 自测 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); try { int ms; Check("默认 100 毫秒", ClickOps.TryParseInterval("100", out ms) && ms == 100, true); Check("最小 10 毫秒", ClickOps.TryParseInterval("10", out ms) && ms == 10, true); Check("小于 10 不合法", ClickOps.TryParseInterval("9", out ms), false); Check("0.5s 等于 500 毫秒", ClickOps.TryParseInterval("0.5s", out ms) && ms == 500, true); Check("200ms 写法", ClickOps.TryParseInterval("200ms", out ms) && ms == 200, true); Check("2秒 写法", ClickOps.TryParseInterval("2秒", out ms) && ms == 2000, true); Check("乱写不合法", ClickOps.TryParseInterval("abc", out ms), false); Check("空串不合法", ClickOps.TryParseInterval("", out ms), false); Check("超上限不合法", ClickOps.TryParseInterval("3600001", out ms), false); Check("间隔文本 500 毫秒", ClickOps.IntervalText(500), "500 毫秒"); Check("间隔文本 2 秒", ClickOps.IntervalText(2000), "2 秒"); Check("间隔文本 1.5 秒", ClickOps.IntervalText(1500), "1.5 秒"); ClickPoint p; Check("解析 100,200", ClickOps.TryParsePoint("100,200", out p) && p.X == 100 && p.Y == 200, true); Check("中文逗号也认", ClickOps.TryParsePoint("100,200", out p) && p.Y == 200, true); Check("带空格也认", ClickOps.TryParsePoint(" 10 , 20 ", out p) && p.X == 10, true); Check("负坐标(多屏)合法", ClickOps.TryParsePoint("-1920,100", out p) && p.X == -1920, true); Check("只有一段不合法", ClickOps.TryParsePoint("100", out p), false); Check("三段不合法", ClickOps.TryParsePoint("1,2,3", out p), false); Check("非数字不合法", ClickOps.TryParsePoint("a,b", out p), false); Check("超大坐标不合法", ClickOps.TryParsePoint("999999,1", out p), false); Check("点位转文本", new ClickPoint(7, 8).ToString(), "7,8"); List pts = new List(); pts.Add(new ClickPoint(10, 10)); pts.Add(new ClickPoint(20, 20)); pts.Add(new ClickPoint(30, 30)); List seq = ClickOps.BuildSequence(pts, 7, 100); Check("7 次点击长度", seq.Count, 7); Check("第 1 次用第 1 点", seq[0].X, 10); Check("第 3 次用第 3 点", seq[2].X, 30); Check("第 4 次回到第 1 点", seq[3].X, 10); Check("第 7 次是第 1 点", seq[6].X, 10); Check("无限次按下限截断", ClickOps.BuildSequence(pts, 0, 5).Count, 5); Check("空点位返回空", ClickOps.BuildSequence(new List(), 5, 5).Count, 0); Check("次数上限保护", ClickOps.BuildSequence(pts, 100000, 9).Count, 9); uint seed = 12345u; bool inRange = true; for (int i = 0; i < 500; i++) { int v = ClickOps.Jitter(500, 5, ref seed); if (v < 495 || v > 505) inRange = false; } Check("抖动不越界", inRange, true); uint s1 = 42u, s2 = 42u; Check("同种子同结果(可复现)", ClickOps.Jitter(100, 8, ref s1), ClickOps.Jitter(100, 8, ref s2)); uint s3 = 42u; Check("抖动 0 就是原值", ClickOps.Jitter(100, 0, ref s3), 100); Check("左键单击文本", ClickOps.ClickKindText(0, false), "左键单击"); Check("右键双击文本", ClickOps.ClickKindText(1, true), "右键双击"); Check("中键文本", ClickOps.ClickKindText(2, false), "中键单击"); } catch (Exception ex) { _fail++; _lines.Add(" [!!] 异常:" + ex.Message); } _lines.Add("通过 " + _pass + " / " + (_pass + _fail)); string all = string.Join("\r\n", _lines.ToArray()) + "\r\n"; Console.WriteLine(all); try { string root = string.IsNullOrEmpty(dir) ? Directory.GetCurrentDirectory() : dir; Directory.CreateDirectory(root); File.WriteAllText(Path.Combine(root, "selftest-autoclick.txt"), all, new UTF8Encoding(true)); } catch { } return _fail == 0 ? 0 : 1; } } static class Program { [STAThread] static void Main(string[] args) { if (args != null && args.Length > 0 && args[0] == "--selftest") { Environment.Exit(ClickSelfTest.Run(args.Length >= 2 ? args[1] : null)); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new ClickForm()); } } }