-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
449 lines (371 loc) · 12.6 KB
/
Copy pathMainForm.cs
File metadata and controls
449 lines (371 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Windows.Forms;
namespace MouseClicker
{
public partial class MainForm : Form
{
#region 私有字段
private Thread _clickThread;
private volatile bool _isRunning;
private volatile bool _isBinding;
private long _clickCount;
private Stopwatch _stopwatch;
private System.Windows.Forms.Timer _statusTimer;
private Random _random;
// 快捷键配置
private bool _hotkeyIsMouse;
private Keys _hotkeyKey;
private MouseButton _hotkeyMouseButton;
#endregion
#region 配置保存/加载
private class AppConfig
{
public int Interval { get; set; } = 100;
public int ButtonIndex { get; set; } = 0;
public bool Infinite { get; set; } = true;
public int ClickCount { get; set; } = 100;
public int RandomRange { get; set; } = 0;
public bool HotkeyIsMouse { get; set; } = false;
public int HotkeyKey { get; set; } = (int)Keys.F8;
public int HotkeyMouseButton { get; set; } = 0;
}
private string ConfigPath => Path.Combine(Application.StartupPath, "config.json");
private void SaveConfig()
{
try
{
var config = new AppConfig
{
Interval = (int)numInterval.Value,
ButtonIndex = cboButton.SelectedIndex,
Infinite = chkInfinite.Checked,
ClickCount = (int)numCount.Value,
RandomRange = (int)numRandom.Value,
HotkeyIsMouse = _hotkeyIsMouse,
HotkeyKey = (int)_hotkeyKey,
HotkeyMouseButton = (int)_hotkeyMouseButton
};
string json = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(ConfigPath, json);
}
catch { }
}
private void LoadConfig()
{
try
{
if (!File.Exists(ConfigPath)) return;
string json = File.ReadAllText(ConfigPath);
var config = JsonSerializer.Deserialize<AppConfig>(json);
if (config == null) return;
numInterval.Value = Math.Clamp(config.Interval, (int)numInterval.Minimum, (int)numInterval.Maximum);
cboButton.SelectedIndex = Math.Clamp(config.ButtonIndex, 0, cboButton.Items.Count - 1);
chkInfinite.Checked = config.Infinite;
numCount.Enabled = !config.Infinite;
numCount.Value = Math.Clamp(config.ClickCount, (int)numCount.Minimum, (int)numCount.Maximum);
numRandom.Value = Math.Clamp(config.RandomRange, (int)numRandom.Minimum, (int)numRandom.Maximum);
_hotkeyIsMouse = config.HotkeyIsMouse;
_hotkeyKey = (Keys)config.HotkeyKey;
_hotkeyMouseButton = (MouseButton)config.HotkeyMouseButton;
UpdateHotkeyDisplay();
}
catch { }
}
private void UpdateHotkeyDisplay()
{
if (_hotkeyIsMouse)
txtHotkey.Text = $"鼠标{GetButtonName(_hotkeyMouseButton)}";
else
txtHotkey.Text = _hotkeyKey.ToString();
btnStart.Text = _isRunning ? $"停止({txtHotkey.Text})" : $"开始({txtHotkey.Text})";
}
private string GetButtonName(MouseButton button)
{
return button switch
{
MouseButton.Left => "左键",
MouseButton.Right => "右键",
MouseButton.Middle => "中键",
MouseButton.X1 => "侧键1",
MouseButton.X2 => "侧键2",
_ => "未知"
};
}
#endregion
#region 构造函数
public MainForm()
{
InitializeComponent();
InitializeEvents();
InitializeDefaults();
}
#endregion
#region 初始化
private void InitializeEvents()
{
// 无限点击切换
chkInfinite.CheckedChanged += ChkInfinite_CheckedChanged;
// 按钮事件
btnStart.Click += BtnStart_Click;
btnBind.Click += BtnBind_Click;
// 窗体事件
this.Load += MainForm_Load;
this.FormClosing += MainForm_FormClosing;
// 全局钩子事件
MouseHelper.GlobalMouseDown += MouseHelper_GlobalMouseDown;
MouseHelper.GlobalKeyDown += MouseHelper_GlobalKeyDown;
// 状态定时器
_statusTimer = new System.Windows.Forms.Timer();
_statusTimer.Interval = 200;
_statusTimer.Tick += StatusTimer_Tick;
}
private void InitializeDefaults()
{
// 默认快捷键 F8
_hotkeyIsMouse = false;
_hotkeyKey = Keys.F8;
_hotkeyMouseButton = MouseButton.Left;
_clickCount = 0;
_stopwatch = new Stopwatch();
_random = new Random();
}
private void MainForm_Load(object sender, EventArgs e)
{
// 先加载配置
LoadConfig();
// 安装全局钩子
MouseHelper.InstallHooks();
// 提高计时器精度
MouseHelper.BeginHighResolution();
// 启动状态更新定时器
_statusTimer.Start();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// 保存配置
SaveConfig();
// 停止连点
StopClicking();
// 卸载钩子
MouseHelper.UninstallHooks();
// 恢复计时器精度
MouseHelper.EndHighResolution();
// 停止定时器
_statusTimer.Stop();
}
#endregion
#region 无限点击切换
private void ChkInfinite_CheckedChanged(object sender, EventArgs e)
{
numCount.Enabled = !chkInfinite.Checked;
}
#endregion
#region 开始/停止
private void BtnStart_Click(object sender, EventArgs e)
{
if (_isRunning)
StopClicking();
else
StartClicking();
}
private void StartClicking()
{
if (_isRunning) return;
_isRunning = true;
_clickCount = 0;
_stopwatch.Restart();
UpdateUIState(true);
_clickThread = new Thread(ClickLoop);
_clickThread.IsBackground = true;
_clickThread.Priority = ThreadPriority.Highest;
_clickThread.Start();
}
private void StopClicking()
{
_isRunning = false;
_stopwatch.Stop();
if (_clickThread != null && _clickThread.IsAlive)
{
_clickThread.Join(1000);
}
UpdateUIState(false);
}
private void UpdateUIState(bool running)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<bool>(UpdateUIState), running);
return;
}
if (running)
{
btnStart.Text = $"停止({txtHotkey.Text})";
lblStatus.Text = "运行中...";
lblStatus.ForeColor = Color.SeaGreen;
}
else
{
btnStart.Text = $"开始({txtHotkey.Text})";
lblStatus.Text = "已停止";
lblStatus.ForeColor = Color.Gray;
}
}
#endregion
#region 连点核心循环
private void ClickLoop()
{
int baseInterval = (int)numInterval.Value;
int randomRange = (int)numRandom.Value;
MouseButton button = (MouseButton)cboButton.SelectedIndex;
bool isInfinite = chkInfinite.Checked;
int targetCount = (int)numCount.Value;
double nextTime = GetTimestampMs();
while (_isRunning)
{
// 计算本次实际间隔(基础间隔 ± 随机扰动)
int actualInterval = baseInterval;
if (randomRange > 0)
{
actualInterval = baseInterval - randomRange + _random.Next(randomRange * 2 + 1);
actualInterval = Math.Max(1, actualInterval);
}
// 执行点击
MouseHelper.Click(button);
_clickCount++;
// 检查次数限制
if (!isInfinite && _clickCount >= targetCount)
{
break;
}
// 高精度等待
nextTime += actualInterval;
double currentTime = GetTimestampMs();
double sleepTime = nextTime - currentTime;
if (sleepTime > 0)
{
if (sleepTime > 5)
{
Thread.Sleep((int)(sleepTime - 2));
while (GetTimestampMs() < nextTime) { }
}
else
{
while (GetTimestampMs() < nextTime) { }
}
}
else
{
nextTime = GetTimestampMs();
}
}
if (_isRunning)
{
StopClicking();
}
}
private double GetTimestampMs()
{
return Stopwatch.GetTimestamp() * 1000.0 / Stopwatch.Frequency;
}
#endregion
#region 快捷键绑定
private void BtnBind_Click(object sender, EventArgs e)
{
if (_isBinding)
{
CancelBinding();
}
else
{
_isBinding = true;
txtHotkey.Text = "等待按键...";
txtHotkey.ForeColor = Color.OrangeRed;
btnBind.Text = "取消";
}
}
private void CancelBinding()
{
_isBinding = false;
txtHotkey.ForeColor = SystemColors.WindowText;
btnBind.Text = "绑定";
UpdateHotkeyDisplay();
}
private void SetHotkey(Keys key)
{
_hotkeyIsMouse = false;
_hotkeyKey = key;
CancelBinding();
}
private void SetHotkey(MouseButton button)
{
_hotkeyIsMouse = true;
_hotkeyMouseButton = button;
CancelBinding();
}
#endregion
#region 全局钩子事件处理
private void MouseHelper_GlobalMouseDown(object sender, MouseButtonEventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<object, MouseButtonEventArgs>(MouseHelper_GlobalMouseDown), sender, e);
return;
}
if (_isBinding)
{
SetHotkey(e.Button);
return;
}
if (_hotkeyIsMouse && e.Button == _hotkeyMouseButton)
{
ToggleClicking();
}
}
private void MouseHelper_GlobalKeyDown(object sender, KeyEventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<object, KeyEventArgs>(MouseHelper_GlobalKeyDown), sender, e);
return;
}
if (_isBinding)
{
if (e.KeyCode == Keys.Escape)
{
CancelBinding();
}
else
{
SetHotkey(e.KeyCode);
}
return;
}
if (!_hotkeyIsMouse && e.KeyCode == _hotkeyKey)
{
ToggleClicking();
}
}
private void ToggleClicking()
{
if (_isRunning)
StopClicking();
else
StartClicking();
}
#endregion
#region 状态更新
private void StatusTimer_Tick(object sender, EventArgs e)
{
if (_stopwatch.IsRunning)
{
lblStatus.Text = $"运行中 | {_clickCount:N0} 次";
}
}
#endregion
}
}