-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
660 lines (581 loc) · 28.3 KB
/
Form1.cs
File metadata and controls
660 lines (581 loc) · 28.3 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Microsoft.VisualBasic;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text.Json;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace MouseClicker
{
// 用于显示点击区域的透明覆盖窗口。
public class OverlayForm : Form
{
private const int WS_EX_TRANSPARENT = 0x20;
public List<ClickArea> AreasToDraw { get; set; } = new List<ClickArea>();
public OverlayForm()
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.BackColor = Color.Magenta;
this.TransparencyKey = this.BackColor;
this.TopMost = true;
this.ShowInTaskbar = false;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
}
// 使窗口透明,允许鼠标穿透。
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TRANSPARENT;
return cp;
}
}
// 在覆盖层上绘制所有点击区域的边框和标签。
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (AreasToDraw == null) return;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
foreach (var area in AreasToDraw)
{
using (var pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, area.Area);
}
using (var font = new Font("Arial", 16, FontStyle.Bold))
using (var brush = new SolidBrush(Color.FromArgb(150, 255, 255, 0)))
{
var textSize = e.Graphics.MeasureString(area.Label, font);
var textX = area.Area.X + (area.Area.Width - textSize.Width) / 2;
var textY = area.Area.Y + (area.Area.Height - textSize.Height) / 2;
e.Graphics.DrawString(area.Label, font, brush, textX, textY);
}
}
}
}
// 代表一个可点击的区域,包含矩形范围和标签。
public class ClickArea
{
public Rectangle Area { get; set; }
public string Label { get; set; } = string.Empty;
public override string ToString()
{
return $"'{Label}' @ (X:{Area.X}, Y:{Area.Y}, W:{Area.Width}, H:{Area.Height})";
}
}
public partial class Form1 : Form
{
// --- 全局键盘钩子,用于监听停止热键 ---
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static Form1 _instance = null!;
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
// --- 核心逻辑字段 ---
private readonly MouseController _mouseController;
private List<ClickArea> _clickAreas = new List<ClickArea>();
private OverlayForm _overlayForm;
// --- 定时器和状态字段 ---
private System.Windows.Forms.Timer _clickTimer;
private Random _random = new Random();
private bool _isClicking = false;
private int _currentClickIndex = 0;
// --- UI 控件字段 ---
private MenuStrip menuStrip = null!;
private ToolStripMenuItem fileMenuItem = null!, languageMenuItem = null!, helpMenuItem = null!, aboutMenuItem = null!;
private ToolStripMenuItem saveConfigMenuItem = null!, loadConfigMenuItem = null!, viewHelpMenuItem = null!;
private ListBox areasListBox = null!;
private Button addAreaButton = null!, removeAreaButton = null!, moveUpButton = null!, moveDownButton = null!;
private Label intervalLabel = null!, jitterLabel = null!, sequenceLabel = null!;
private NumericUpDown intervalNumericUpDown = null!, jitterNumericUpDown = null!;
private ComboBox sequenceComboBox = null!;
private CheckBox showOverlayCheckBox = null!;
private Button startButton = null!;
// 主窗口构造函数
public Form1()
{
InitializeComponent();
_instance = this;
_mouseController = new MouseController();
_clickTimer = new System.Windows.Forms.Timer();
_clickTimer.Tick += new EventHandler(clickTimer_Tick);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
// 设置窗口图标
try
{
this.Icon = Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
}
catch (Exception)
{
// 忽略图标加载失败
}
InitializeCustomComponents();
UpdateUIStrings();
_overlayForm = new OverlayForm();
_overlayForm.Show();
this.FormClosing += (s, e) =>
{
if (_hookID != IntPtr.Zero) UnhookWindowsHookEx(_hookID);
_overlayForm.Close();
};
}
// 键盘钩子回调,用于监听停止热键 (ESC)。
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
if (_instance != null && _instance._isClicking && (Keys)vkCode == Keys.Escape)
{
_instance.BeginInvoke(new Action(() => _instance.StopClicking()));
return (IntPtr)1;
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
// 安装全局键盘钩子。
private void SetHook()
{
if (_hookID != IntPtr.Zero) return;
using (Process curProcess = Process.GetCurrentProcess())
{
using (ProcessModule? curModule = curProcess.MainModule)
{
if (curModule != null && curModule.ModuleName != null)
{
_hookID = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
}
}
// 初始化通过代码创建的UI组件。
private void InitializeCustomComponents()
{
// 创建控件
this.menuStrip = new MenuStrip();
fileMenuItem = new ToolStripMenuItem();
saveConfigMenuItem = new ToolStripMenuItem();
loadConfigMenuItem = new ToolStripMenuItem();
languageMenuItem = new ToolStripMenuItem();
var zhMenuItem = new ToolStripMenuItem("中文");
var enMenuItem = new ToolStripMenuItem("English");
helpMenuItem = new ToolStripMenuItem();
viewHelpMenuItem = new ToolStripMenuItem();
aboutMenuItem = new ToolStripMenuItem();
this.areasListBox = new ListBox();
this.addAreaButton = new Button();
this.removeAreaButton = new Button();
this.moveUpButton = new Button();
this.moveDownButton = new Button();
this.intervalLabel = new Label { AutoSize = true };
this.intervalNumericUpDown = new NumericUpDown { Maximum = 600000, Minimum = 10, Value = 1000 };
this.jitterLabel = new Label { AutoSize = true };
this.jitterNumericUpDown = new NumericUpDown { Maximum = 10000, Value = 100 };
this.sequenceLabel = new Label { AutoSize = true };
this.sequenceComboBox = new ComboBox { DropDownStyle = ComboBoxStyle.DropDownList };
this.showOverlayCheckBox = new CheckBox { AutoSize = true, Checked = true };
this.startButton = new Button { Font = new Font(this.Font.FontFamily, 10, FontStyle.Bold) };
// 将控件添加到窗体
this.Controls.Add(this.menuStrip);
this.MainMenuStrip = this.menuStrip;
fileMenuItem.DropDownItems.Add(saveConfigMenuItem);
fileMenuItem.DropDownItems.Add(loadConfigMenuItem);
languageMenuItem.DropDownItems.Add(zhMenuItem);
languageMenuItem.DropDownItems.Add(enMenuItem);
helpMenuItem.DropDownItems.Add(viewHelpMenuItem);
this.menuStrip.Items.Add(fileMenuItem);
this.menuStrip.Items.Add(languageMenuItem);
this.menuStrip.Items.Add(helpMenuItem);
this.menuStrip.Items.Add(aboutMenuItem);
this.Controls.Add(this.areasListBox);
this.Controls.Add(this.addAreaButton);
this.Controls.Add(this.removeAreaButton);
this.Controls.Add(this.moveUpButton);
this.Controls.Add(this.moveDownButton);
this.Controls.Add(this.intervalLabel);
this.Controls.Add(this.intervalNumericUpDown);
this.Controls.Add(this.jitterLabel);
this.Controls.Add(this.jitterNumericUpDown);
this.Controls.Add(this.sequenceLabel);
this.Controls.Add(this.sequenceComboBox);
this.Controls.Add(this.showOverlayCheckBox);
this.Controls.Add(this.startButton);
// 附加事件处理器
saveConfigMenuItem.Click += new EventHandler(saveConfigMenuItem_Click);
loadConfigMenuItem.Click += new EventHandler(loadConfigMenuItem_Click);
zhMenuItem.Click += (s, e) => { ChangeLanguage("zh-CN"); };
enMenuItem.Click += (s, e) => { ChangeLanguage("en-US"); };
viewHelpMenuItem.Click += new EventHandler(viewHelpMenuItem_Click);
aboutMenuItem.Click += new EventHandler(aboutMenuItem_Click);
addAreaButton.Click += new EventHandler(addAreaButton_Click);
removeAreaButton.Click += new EventHandler(removeAreaButton_Click);
moveUpButton.Click += new EventHandler(moveUpButton_Click);
moveDownButton.Click += new EventHandler(moveDownButton_Click);
startButton.Click += new EventHandler(startButton_Click);
}
// 动态调整UI控件的布局。
private void PerformLayoutLayout()
{
int controlPadding = 5;
int rowHeight = 30;
int formPadding = 10;
int currentY = menuStrip.Height + formPadding;
int formWidth = 430;
areasListBox.Location = new Point(formPadding, currentY);
areasListBox.Size = new Size(formWidth - (formPadding * 2), 120);
currentY += areasListBox.Height + controlPadding;
int buttonWidth = (formWidth - (formPadding * 2) - (controlPadding * 3)) / 4;
addAreaButton.Location = new Point(formPadding, currentY);
addAreaButton.Size = new Size(buttonWidth, rowHeight);
removeAreaButton.Location = new Point(addAreaButton.Right + controlPadding, currentY);
removeAreaButton.Size = new Size(buttonWidth, rowHeight);
moveUpButton.Location = new Point(removeAreaButton.Right + controlPadding, currentY);
moveUpButton.Size = new Size(buttonWidth, rowHeight);
moveDownButton.Location = new Point(moveUpButton.Right + controlPadding, currentY);
moveDownButton.Size = new Size(buttonWidth, rowHeight);
currentY += rowHeight + (controlPadding * 2);
intervalLabel.Location = new Point(formPadding, currentY + 4);
intervalNumericUpDown.Location = new Point(intervalLabel.Right + controlPadding, currentY);
intervalNumericUpDown.Size = new Size(100, rowHeight);
jitterLabel.Location = new Point(intervalNumericUpDown.Right + controlPadding, currentY + 4);
jitterNumericUpDown.Location = new Point(jitterLabel.Right + controlPadding, currentY);
jitterNumericUpDown.Size = new Size(100, rowHeight);
currentY += rowHeight + controlPadding;
sequenceLabel.Location = new Point(formPadding, currentY + 4);
sequenceComboBox.Location = new Point(sequenceLabel.Right + controlPadding, currentY);
sequenceComboBox.Size = new Size(180, rowHeight);
showOverlayCheckBox.Location = new Point(sequenceComboBox.Right + controlPadding, currentY + 4);
currentY += rowHeight + (controlPadding * 2);
int requiredWidth = Math.Max(moveDownButton.Right, jitterNumericUpDown.Right);
requiredWidth = Math.Max(requiredWidth, showOverlayCheckBox.Right) + formPadding;
formWidth = Math.Max(formWidth, requiredWidth);
areasListBox.Width = formWidth - (formPadding * 2);
startButton.Width = formWidth - (formPadding * 2);
int buttonWidthNew = (formWidth - (formPadding * 2) - (controlPadding * 3)) / 4;
addAreaButton.Width = removeAreaButton.Width = moveUpButton.Width = moveDownButton.Width = buttonWidthNew;
removeAreaButton.Left = addAreaButton.Right + controlPadding;
moveUpButton.Left = removeAreaButton.Right + controlPadding;
moveDownButton.Left = moveUpButton.Right + controlPadding;
startButton.Location = new Point(formPadding, currentY);
startButton.Size = new Size(formWidth - (formPadding * 2), 40);
this.ClientSize = new Size(formWidth, startButton.Bottom + formPadding);
}
// 更改应用程序语言。
private void ChangeLanguage(string lang)
{
Localization.Language = lang;
UpdateUIStrings();
}
// 根据当前语言更新UI文本。
private void UpdateUIStrings()
{
fileMenuItem.Text = Localization.Get("File");
saveConfigMenuItem.Text = Localization.Get("SaveConfig");
loadConfigMenuItem.Text = Localization.Get("LoadConfig");
languageMenuItem.Text = Localization.Get("Language");
helpMenuItem.Text = Localization.Get("Help");
viewHelpMenuItem.Text = Localization.Get("ViewHelp");
aboutMenuItem.Text = Localization.Get("About");
addAreaButton.Text = Localization.Get("Add");
removeAreaButton.Text = Localization.Get("Remove");
moveUpButton.Text = Localization.Get("MoveUp");
moveDownButton.Text = Localization.Get("MoveDown");
intervalLabel.Text = Localization.Get("IntervalLabel");
jitterLabel.Text = Localization.Get("JitterLabel");
sequenceLabel.Text = Localization.Get("SequenceLabel");
showOverlayCheckBox.Text = Localization.Get("ShowOverlay");
startButton.Text = _isClicking ? Localization.Get("StopClicking") : Localization.Get("StartClicking");
this.Text = Localization.Get("WindowTitle");
int selectedIndex = sequenceComboBox.SelectedIndex;
sequenceComboBox.Items.Clear();
sequenceComboBox.Items.AddRange(new string[] {
Localization.Get("SequenceLoop"),
Localization.Get("SequenceRandom"),
Localization.Get("SequenceOnce")
});
if (selectedIndex >= 0) sequenceComboBox.SelectedIndex = selectedIndex; else if (sequenceComboBox.Items.Count > 0) { sequenceComboBox.SelectedIndex = 0; }
PerformLayoutLayout();
}
// “查看帮助”菜单项的点击事件。
private void viewHelpMenuItem_Click(object? sender, EventArgs e)
{
using (Form tutorialForm = new Form())
{
tutorialForm.Text = Localization.Get("HelpTutorialTitle");
tutorialForm.Size = new Size(600, 500);
tutorialForm.StartPosition = FormStartPosition.CenterParent;
tutorialForm.FormBorderStyle = FormBorderStyle.Sizable;
tutorialForm.MaximizeBox = true;
tutorialForm.MinimizeBox = true;
Panel buttonPanel = new Panel();
buttonPanel.Height = 40;
buttonPanel.Dock = DockStyle.Bottom;
Button closeButton = new Button();
closeButton.Text = Localization.Get("CloseButton");
closeButton.DialogResult = DialogResult.OK;
closeButton.Size = new Size(100, 25);
closeButton.Location = new Point((buttonPanel.ClientSize.Width - closeButton.Width) / 2, (buttonPanel.ClientSize.Height - closeButton.Height) / 2);
closeButton.Anchor = AnchorStyles.None;
buttonPanel.Controls.Add(closeButton);
RichTextBox helpTextBox = new RichTextBox();
helpTextBox.Dock = DockStyle.Fill;
helpTextBox.ReadOnly = true;
helpTextBox.Text = Localization.Get("HelpTutorialContent");
helpTextBox.ScrollBars = RichTextBoxScrollBars.Vertical;
helpTextBox.Font = new Font("Microsoft YaHei UI", 9.75F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
helpTextBox.BorderStyle = BorderStyle.None;
tutorialForm.Controls.Add(helpTextBox);
tutorialForm.Controls.Add(buttonPanel);
tutorialForm.AcceptButton = closeButton;
tutorialForm.ShowDialog(this);
}
}
// “保存配置”菜单项的点击事件。
private void saveConfigMenuItem_Click(object? sender, EventArgs e)
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = Localization.Get("JsonFilter");
saveFileDialog.Title = Localization.Get("SaveConfigTitle");
if (saveFileDialog.ShowDialog() == DialogResult.OK && !string.IsNullOrWhiteSpace(saveFileDialog.FileName))
{
var configData = new ConfigData
{
ClickAreas = _clickAreas,
Interval = intervalNumericUpDown.Value,
Jitter = jitterNumericUpDown.Value,
SequenceIndex = sequenceComboBox.SelectedIndex,
ShowOverlay = showOverlayCheckBox.Checked
};
try
{
string jsonString = JsonSerializer.Serialize(configData, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(saveFileDialog.FileName, jsonString);
MessageBox.Show(Localization.Get("ConfigSavedSuccess"), Localization.Get("Success"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(string.Format(Localization.Get("SaveConfigError"), ex.Message), Localization.Get("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
// “加载配置”菜单项的点击事件。
private void loadConfigMenuItem_Click(object? sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = Localization.Get("JsonFilter");
openFileDialog.Title = Localization.Get("LoadConfigTitle");
if (openFileDialog.ShowDialog() == DialogResult.OK && !string.IsNullOrWhiteSpace(openFileDialog.FileName))
{
try
{
string jsonString = File.ReadAllText(openFileDialog.FileName);
var configData = JsonSerializer.Deserialize<ConfigData>(jsonString);
if (configData != null)
{
_clickAreas = configData.ClickAreas ?? new List<ClickArea>();
intervalNumericUpDown.Value = configData.Interval;
jitterNumericUpDown.Value = configData.Jitter;
sequenceComboBox.SelectedIndex = configData.SequenceIndex;
showOverlayCheckBox.Checked = configData.ShowOverlay;
RefreshAreaList();
MessageBox.Show(Localization.Get("ConfigLoadedSuccess"), Localization.Get("Success"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(string.Format(Localization.Get("LoadConfigError"), "Invalid config file format."), Localization.Get("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format(Localization.Get("LoadConfigError"), ex.Message), Localization.Get("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
// “关于”菜单项的点击事件。
private void aboutMenuItem_Click(object? sender, EventArgs e)
{
MessageBox.Show(Localization.Get("AboutMessage"), Localization.Get("About"));
}
// 刷新点击区域列表的显示。
private void RefreshAreaList()
{
int selectedIndex = areasListBox.SelectedIndex;
areasListBox.Items.Clear();
foreach (var area in _clickAreas)
{
areasListBox.Items.Add(area);
}
if(selectedIndex != -1 && selectedIndex < areasListBox.Items.Count)
{
areasListBox.SelectedIndex = selectedIndex;
}
_overlayForm.AreasToDraw = new List<ClickArea>(_clickAreas);
_overlayForm.Invalidate();
}
// “添加”按钮的点击事件。
private void addAreaButton_Click(object? sender, EventArgs e)
{
this.Hide();
_overlayForm.Hide();
System.Threading.Thread.Sleep(200);
using (var selectionForm = new SelectionForm())
{
if (selectionForm.ShowDialog() == DialogResult.OK)
{
this.Show();
string label = Interaction.InputBox(Localization.Get("SetLabelPrompt"), Localization.Get("SetLabelTitle"), Localization.Get("AreaLabelDefault") + (_clickAreas.Count + 1));
if (!string.IsNullOrWhiteSpace(label))
{
_clickAreas.Add(new ClickArea { Area = selectionForm.SelectedArea, Label = label });
RefreshAreaList();
}
}
else
{
this.Show();
}
}
_overlayForm.Show();
_overlayForm.Invalidate();
}
// “移除”按钮的点击事件。
private void removeAreaButton_Click(object? sender, EventArgs e)
{
if (areasListBox.SelectedItem != null)
{
_clickAreas.Remove((ClickArea)areasListBox.SelectedItem);
RefreshAreaList();
}
}
// “上移”按钮的点击事件。
private void moveUpButton_Click(object? sender, EventArgs e)
{
int index = areasListBox.SelectedIndex;
if (index > 0)
{
var item = _clickAreas[index];
_clickAreas.RemoveAt(index);
_clickAreas.Insert(index - 1, item);
RefreshAreaList();
areasListBox.SelectedIndex = index - 1;
}
}
// “下移”按钮的点击事件。
private void moveDownButton_Click(object? sender, EventArgs e)
{
int index = areasListBox.SelectedIndex;
if (index != -1 && index < _clickAreas.Count - 1)
{
var item = _clickAreas[index];
_clickAreas.RemoveAt(index);
_clickAreas.Insert(index + 1, item);
RefreshAreaList();
areasListBox.SelectedIndex = index + 1;
}
}
// “开始/停止”按钮的点击事件。
private void startButton_Click(object? sender, EventArgs e)
{
if (_isClicking) { StopClicking(); } else { StartClicking(); }
}
// 开始自动点击。
private void StartClicking()
{
if (_clickAreas.Count == 0) { MessageBox.Show(Localization.Get("AddAreaFirst")); return; }
_isClicking = true;
_currentClickIndex = 0;
startButton.Text = Localization.Get("StopClicking");
SetUIEnabled(false);
if (!showOverlayCheckBox.Checked)
{
_overlayForm.Hide();
}
SetHook();
clickTimer_Tick(this, EventArgs.Empty);
}
// 停止自动点击。
private void StopClicking()
{
if (!_isClicking) return;
if (_hookID != IntPtr.Zero)
{
UnhookWindowsHookEx(_hookID);
_hookID = IntPtr.Zero;
}
_clickTimer.Stop();
_isClicking = false;
startButton.Text = Localization.Get("StartClicking");
SetUIEnabled(true);
_overlayForm.Show();
_overlayForm.Invalidate();
}
// 在点击过程中禁用或启用UI控件。
private void SetUIEnabled(bool isEnabled)
{
areasListBox.Enabled = isEnabled;
addAreaButton.Enabled = isEnabled;
removeAreaButton.Enabled = isEnabled;
moveUpButton.Enabled = isEnabled;
moveDownButton.Enabled = isEnabled;
intervalNumericUpDown.Enabled = isEnabled;
jitterNumericUpDown.Enabled = isEnabled;
sequenceComboBox.Enabled = isEnabled;
showOverlayCheckBox.Enabled = isEnabled;
menuStrip.Enabled = isEnabled;
}
// 定时器事件,用于执行点击。
private void clickTimer_Tick(object? sender, EventArgs e)
{
if (!_isClicking) return;
_clickTimer.Stop();
ClickArea? areaToClick = GetNextArea();
if (areaToClick == null) { StopClicking(); return; }
_mouseController.PerformRandomClickInArea(areaToClick.Area);
if (_isClicking)
{
int baseInterval = (int)intervalNumericUpDown.Value;
int jitter = (int)jitterNumericUpDown.Value;
int randomJitter = _random.Next(-jitter, jitter + 1);
_clickTimer.Interval = Math.Max(10, baseInterval + randomJitter);
_clickTimer.Start();
}
}
// 根据序列模式获取下一个点击区域。
private ClickArea? GetNextArea()
{
if (_clickAreas.Count == 0) return null;
string mode = sequenceComboBox.Text;
if (mode == Localization.Get("SequenceRandom"))
{
return _clickAreas[_random.Next(0, _clickAreas.Count)];
}
if (_currentClickIndex >= _clickAreas.Count)
{
if (mode == Localization.Get("SequenceLoop")) { _currentClickIndex = 0; } else { return null; }
}
var area = _clickAreas[_currentClickIndex];
_currentClickIndex++;
return area;
}
}
}