Repository navigation
Expand file tree
/
Copy pathMainForm.cs
More file actions
741 lines (658 loc) · 26.2 KB
/
Copy pathMainForm.cs
File metadata and controls
741 lines (658 loc) · 26.2 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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SquadTools;
internal sealed class MainForm : Form
{
private const int WsExToolWindow = 0x00000080;
private const int WsExAppWindow = 0x00040000;
private const int F9HotKeyId = 1;
private const int F10HotKeyId = 2;
private const int F8HotKeyId = 3;
private readonly BuildAssistController buildAssist = new();
private readonly RapidPasteService rapidPaste = new();
private readonly AutoRunController autoRun = new();
private readonly SquadLogReader squadLogReader;
private readonly MapGuessForm mapGuessForm;
private readonly ProxySettings proxySettings;
private readonly bool webView2Available;
private readonly CheckBox buildSwitch = new();
private readonly CheckBox rapidPasteSwitch = new();
private readonly CheckBox autoRunSwitch = new();
private readonly Label buildStatus = new();
private readonly Label rapidPasteStatus = new();
private readonly Label autoRunStatus = new();
private readonly Label mapGuessStatus = new();
private readonly CheckBox proxySwitch = new();
private readonly ComboBox proxyTypeBox = new();
private readonly TextBox proxyHostBox = new();
private readonly NumericUpDown proxyPortBox = new();
private readonly Label proxyStatus = new();
private readonly TextBox squadNameBox = new();
private readonly NotifyIcon trayIcon;
private readonly Icon applicationIcon;
private readonly bool serviceMode;
private int pasteIntervalMilliseconds = 50;
private bool allowClose;
internal MainForm(bool webView2Available, bool serviceMode = false)
{
this.serviceMode = serviceMode;
this.webView2Available = webView2Available;
Text = "Squad小帮手";
ShowInTaskbar = false;
StartPosition = FormStartPosition.CenterScreen;
ClientSize = new Size(560, 430);
MinimumSize = new Size(560, 430);
MaximumSize = new Size(560, 430);
FormBorderStyle = FormBorderStyle.FixedSingle;
MaximizeBox = false;
WindowState = FormWindowState.Minimized;
Opacity = 0;
Font = new Font("Microsoft YaHei UI", 10F, FontStyle.Regular, GraphicsUnit.Point);
applicationIcon = Icon.ExtractAssociatedIcon(Application.ExecutablePath) ?? (Icon)SystemIcons.Application.Clone();
Icon = applicationIcon;
proxySettings = ProxySettingsStore.Load();
squadLogReader = new SquadLogReader();
string mapHostDirectory = Path.Combine(Application.StartupPath, "MapHost");
mapGuessForm = new MapGuessForm(applicationIcon, proxySettings, mapHostDirectory);
TabControl tabs = new()
{
Dock = DockStyle.Fill,
Padding = new Point(16, 7)
};
tabs.TabPages.Add(CreateBuildPage());
tabs.TabPages.Add(CreateRapidPastePage());
tabs.TabPages.Add(CreateAutoRunPage());
tabs.TabPages.Add(CreateMapToolPage());
tabs.TabPages.Add(CreateSettingsPage());
Label footer = new()
{
Dock = DockStyle.Fill,
Text = "作者: lyl-103 版本号: 1.5.1",
TextAlign = ContentAlignment.MiddleRight,
Padding = new Padding(0, 0, 12, 0),
ForeColor = Color.FromArgb(105, 110, 115),
Font = new Font("Microsoft YaHei UI", 8.5F, FontStyle.Regular, GraphicsUnit.Point)
};
TableLayoutPanel shell = new()
{
Dock = DockStyle.Fill,
ColumnCount = 1,
RowCount = 2,
Margin = Padding.Empty,
Padding = Padding.Empty
};
shell.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
shell.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
shell.RowStyles.Add(new RowStyle(SizeType.Absolute, 28F));
shell.Controls.Add(tabs, 0, 0);
shell.Controls.Add(footer, 0, 1);
Controls.Add(shell);
ContextMenuStrip trayMenu = new();
trayMenu.Items.Add("显示主界面", null, (_, _) =>
{
if (serviceMode) LegacyRuntime.ShowMainWindow(); else ShowMainWindow();
});
ToolStripMenuItem mapToolMenuItem = new("地图工具")
{
Enabled = webView2Available
};
mapToolMenuItem.Click += (_, _) => mapGuessForm.ShowWindow();
trayMenu.Items.Add(mapToolMenuItem);
trayMenu.Items.Add("退出", null, (_, _) =>
{
if (serviceMode) LegacyRuntime.ExitApplication(); else ExitApplication();
});
trayIcon = new NotifyIcon
{
Icon = (Icon)applicationIcon.Clone(),
Text = "Squad小帮手",
ContextMenuStrip = trayMenu,
Visible = true
};
if (serviceMode)
{
trayIcon.Visible = false;
}
trayIcon.DoubleClick += (_, _) => ShowMainWindow();
buildAssist.StatusChanged += message => UpdateControl(buildStatus, $"当前状态:{message}(F8 切换)");
buildAssist.Error += message => ShowBuildError(message);
rapidPaste.Error += ShowRapidPasteError;
autoRun.StatusChanged += message => UpdateControl(autoRunStatus, $"当前状态:{message}(F10 切换)");
autoRun.Error += ShowAutoRunError;
autoRun.Stopped += SynchronizeStoppedAutoRun;
squadLogReader.MapLayerChanged += selection =>
{
mapGuessForm.ApplyMapLayer(selection);
UpdateControl(mapGuessStatus, DescribeSelection(selection));
};
squadLogReader.StatusChanged += message =>
{
UpdateControl(mapGuessStatus, message);
mapGuessForm.UpdateLogStatus(message);
};
mapGuessForm.WindowShown += () => squadLogReader.ScanNow();
squadLogReader.ScanNow();
PrepareClipboard();
FormClosing += OnFormClosing;
FormClosed += (_, _) => DisposeServices();
}
// Keep the background WinForms host out of the Windows task switcher.
protected override CreateParams CreateParams
{
get
{
CreateParams parameters = base.CreateParams;
parameters.ExStyle |= WsExToolWindow;
parameters.ExStyle &= ~WsExAppWindow;
return parameters;
}
}
private TabPage CreateBuildPage()
{
TabPage page = new("自动铲子") { BackColor = Color.FromArgb(250, 250, 250) };
ConfigureToggle(buildSwitch, "自动铲子:关闭", new Point(28, 30));
buildSwitch.CheckedChanged += (_, _) => SetBuildAssist(buildSwitch.Checked);
buildStatus.AutoSize = false;
buildStatus.Text = "当前状态:未启用(F8 切换)";
buildStatus.Location = new Point(28, 106);
buildStatus.Size = new Size(480, 30);
buildStatus.ForeColor = Color.FromArgb(55, 60, 65);
Label description = new()
{
AutoSize = false,
Text = "长按左键,建造工事;长按右键,刨除工事",
Location = new Point(28, 160),
Size = new Size(480, 48),
ForeColor = Color.FromArgb(85, 90, 95)
};
page.Controls.Add(buildSwitch);
page.Controls.Add(buildStatus);
page.Controls.Add(description);
return page;
}
private TabPage CreateRapidPastePage()
{
TabPage page = new("极速抢车") { BackColor = Color.FromArgb(250, 250, 250) };
Label nameLabel = new() { Text = "小队名称", AutoSize = true, Location = new Point(28, 38) };
squadNameBox.Text = "TANK";
squadNameBox.Location = new Point(124, 32);
squadNameBox.Size = new Size(200, 29);
squadNameBox.TextChanged += (_, _) => PrepareClipboard();
Label intervalLabel = new() { Text = "发送间隔", AutoSize = true, Location = new Point(28, 91) };
FlowLayoutPanel intervalPanel = new()
{
Location = new Point(124, 82),
Size = new Size(385, 38),
FlowDirection = FlowDirection.LeftToRight,
WrapContents = false,
BackColor = Color.Transparent
};
intervalPanel.Controls.Add(CreateIntervalOption("超快 10ms", 10, false));
intervalPanel.Controls.Add(CreateIntervalOption("快速 50ms", 50, true));
intervalPanel.Controls.Add(CreateIntervalOption("标准 100ms", 100, false));
ConfigureToggle(rapidPasteSwitch, "极速抢车:关闭", new Point(28, 142));
rapidPasteSwitch.CheckedChanged += (_, _) => SetRapidPaste(rapidPasteSwitch.Checked);
rapidPasteStatus.AutoSize = false;
rapidPasteStatus.Text = "当前状态:已停止(F9 切换)";
rapidPasteStatus.Location = new Point(28, 218);
rapidPasteStatus.Size = new Size(480, 30);
rapidPasteStatus.ForeColor = Color.FromArgb(55, 60, 65);
Label modeDescription = new()
{
AutoSize = false,
Text = "仅在 Squad 位于前台时循环粘贴建队命令",
Location = new Point(28, 270),
Size = new Size(480, 40),
ForeColor = Color.FromArgb(85, 90, 95)
};
page.Controls.Add(nameLabel);
page.Controls.Add(squadNameBox);
page.Controls.Add(intervalLabel);
page.Controls.Add(intervalPanel);
page.Controls.Add(rapidPasteSwitch);
page.Controls.Add(rapidPasteStatus);
page.Controls.Add(modeDescription);
return page;
}
private TabPage CreateAutoRunPage()
{
TabPage page = new("自动奔跑") { BackColor = Color.FromArgb(250, 250, 250) };
ConfigureToggle(autoRunSwitch, "自动奔跑:关闭", new Point(28, 30));
autoRunSwitch.CheckedChanged += (_, _) => SetAutoRun(autoRunSwitch.Checked);
autoRunStatus.AutoSize = false;
autoRunStatus.Text = "当前状态:未启用(F10 切换)";
autoRunStatus.Location = new Point(28, 106);
autoRunStatus.Size = new Size(480, 30);
autoRunStatus.ForeColor = Color.FromArgb(55, 60, 65);
Label description = new()
{
AutoSize = false,
Text = "适用于无限体力的服务器,开启后自动奔跑",
Location = new Point(28, 160),
Size = new Size(480, 48),
ForeColor = Color.FromArgb(85, 90, 95)
};
page.Controls.Add(autoRunSwitch);
page.Controls.Add(autoRunStatus);
page.Controls.Add(description);
return page;
}
private TabPage CreateMapToolPage()
{
TabPage page = new("地图工具") { BackColor = Color.FromArgb(250, 250, 250) };
Button openButton = new()
{
Text = "打开地图工具窗口",
Location = new Point(28, 30),
Size = new Size(220, 48),
FlatStyle = FlatStyle.Flat,
BackColor = Color.FromArgb(236, 239, 241),
ForeColor = Color.FromArgb(32, 37, 41),
Font = new Font("Microsoft YaHei UI", 10F, FontStyle.Bold, GraphicsUnit.Point)
};
openButton.Enabled = webView2Available;
openButton.Click += (_, _) => mapGuessForm.ShowWindow();
mapGuessStatus.AutoSize = false;
mapGuessStatus.Text = webView2Available
? "等待 Squad 地图日志"
: "未安装 WebView2 Runtime,地图工具不可用";
mapGuessStatus.Location = new Point(28, 106);
mapGuessStatus.Size = new Size(480, 30);
mapGuessStatus.ForeColor = Color.FromArgb(55, 60, 65);
Label description = new()
{
AutoSize = false,
Text = "支持地图点位刷新预测,以及迫击炮等远程火力坐标计算",
Location = new Point(28, 160),
Size = new Size(480, 48),
ForeColor = Color.FromArgb(85, 90, 95)
};
page.Controls.Add(openButton);
page.Controls.Add(mapGuessStatus);
page.Controls.Add(description);
return page;
}
private TabPage CreateSettingsPage()
{
TabPage page = new("设置") { BackColor = Color.FromArgb(250, 250, 250) };
proxySwitch.Appearance = Appearance.Button;
proxySwitch.AutoSize = false;
proxySwitch.Text = proxySettings.Enabled ? "网络代理:开启" : "网络代理:关闭";
proxySwitch.TextAlign = ContentAlignment.MiddleCenter;
proxySwitch.Size = new Size(220, 48);
proxySwitch.Location = new Point(28, 30);
proxySwitch.BackColor = proxySettings.Enabled
? Color.FromArgb(210, 241, 224)
: Color.FromArgb(236, 239, 241);
proxySwitch.FlatStyle = FlatStyle.Flat;
proxySwitch.FlatAppearance.BorderSize = 1;
proxySwitch.FlatAppearance.BorderColor = proxySettings.Enabled
? Color.FromArgb(91, 162, 119)
: Color.FromArgb(174, 181, 187);
proxySwitch.Checked = proxySettings.Enabled;
proxySwitch.CheckedChanged += (_, _) =>
{
proxySwitch.Text = proxySwitch.Checked ? "网络代理:开启" : "网络代理:关闭";
proxySwitch.BackColor = proxySwitch.Checked
? Color.FromArgb(210, 241, 224)
: Color.FromArgb(236, 239, 241);
proxySwitch.FlatAppearance.BorderColor = proxySwitch.Checked
? Color.FromArgb(91, 162, 119)
: Color.FromArgb(174, 181, 187);
};
Label typeLabel = new() { Text = "代理类型", AutoSize = true, Location = new Point(28, 108) };
proxyTypeBox.DropDownStyle = ComboBoxStyle.DropDownList;
proxyTypeBox.Items.AddRange(["HTTP", "SOCKS5"]);
proxyTypeBox.SelectedIndex = proxySettings.Type == ProxyType.Socks5 ? 1 : 0;
proxyTypeBox.Location = new Point(124, 102);
proxyTypeBox.Size = new Size(140, 29);
Label hostLabel = new() { Text = "IP 地址", AutoSize = true, Location = new Point(28, 155) };
proxyHostBox.Text = proxySettings.Host;
proxyHostBox.PlaceholderText = "例如 127.0.0.1";
proxyHostBox.Location = new Point(124, 149);
proxyHostBox.Size = new Size(220, 29);
Label portLabel = new() { Text = "端口", AutoSize = true, Location = new Point(28, 202) };
proxyPortBox.Minimum = 1;
proxyPortBox.Maximum = 65535;
proxyPortBox.Value = Math.Clamp(proxySettings.Port, 1, 65535);
proxyPortBox.Location = new Point(124, 196);
proxyPortBox.Size = new Size(140, 29);
Button saveButton = new()
{
Text = "保存代理设置",
Location = new Point(28, 250),
Size = new Size(180, 42),
FlatStyle = FlatStyle.Flat,
BackColor = Color.FromArgb(236, 239, 241),
ForeColor = Color.FromArgb(32, 37, 41)
};
saveButton.Click += (_, _) => SaveProxySettings();
proxyStatus.AutoSize = false;
proxyStatus.Location = new Point(28, 315);
proxyStatus.Size = new Size(480, 48);
proxyStatus.ForeColor = Color.FromArgb(55, 60, 65);
proxyStatus.Text = "代理设置将在首次创建地图工具网页时生效;已打开过网页时请重启程序。";
page.Controls.Add(proxySwitch);
page.Controls.Add(typeLabel);
page.Controls.Add(proxyTypeBox);
page.Controls.Add(hostLabel);
page.Controls.Add(proxyHostBox);
page.Controls.Add(portLabel);
page.Controls.Add(proxyPortBox);
page.Controls.Add(saveButton);
page.Controls.Add(proxyStatus);
return page;
}
private void SaveProxySettings()
{
string host = proxyHostBox.Text.Trim();
int port = (int)proxyPortBox.Value;
if (proxySwitch.Checked && host.Length == 0)
{
proxyStatus.Text = "启用代理时必须填写 IP 地址或主机名。";
return;
}
proxySettings.Enabled = proxySwitch.Checked;
proxySettings.Type = proxyTypeBox.SelectedIndex == 1 ? ProxyType.Socks5 : ProxyType.Http;
proxySettings.Host = host;
proxySettings.Port = port;
if (!ProxySettingsStore.Save(proxySettings, out string error))
{
proxyStatus.Text = error;
return;
}
proxyStatus.Text = proxySettings.Enabled
? $"代理设置已保存:{proxySettings.Type} {proxySettings.Host}:{proxySettings.Port},首次创建网页时生效。"
: "网络代理已关闭,设置已保存。";
}
private static string DescribeSelection(MapLayerSelection selection)
{
string team1 = MapLayerSelection.FactionName(selection.Team1Unit);
string team2 = MapLayerSelection.FactionName(selection.Team2Unit);
string factions = team1.Length == 0 && team2.Length == 0
? string.Empty
: $" 阵营:{team1} vs {team2}";
return $"当前地图:{selection.Map} Layer:{selection.Layer}{factions}";
}
private static void ConfigureToggle(CheckBox toggle, string text, Point location)
{
toggle.Appearance = Appearance.Button;
toggle.AutoSize = false;
toggle.Text = text;
toggle.TextAlign = ContentAlignment.MiddleCenter;
toggle.Size = new Size(220, 48);
toggle.Location = location;
toggle.BackColor = Color.FromArgb(236, 239, 241);
toggle.ForeColor = Color.FromArgb(32, 37, 41);
toggle.Font = new Font("Microsoft YaHei UI", 10F, FontStyle.Bold, GraphicsUnit.Point);
toggle.FlatStyle = FlatStyle.Flat;
toggle.FlatAppearance.BorderSize = 1;
toggle.FlatAppearance.BorderColor = Color.FromArgb(174, 181, 187);
toggle.FlatAppearance.MouseOverBackColor = Color.FromArgb(225, 230, 234);
toggle.FlatAppearance.CheckedBackColor = Color.FromArgb(210, 241, 224);
}
private RadioButton CreateIntervalOption(string text, int milliseconds, bool isChecked)
{
RadioButton option = new()
{
AutoSize = true,
Text = text,
Checked = isChecked,
Margin = new Padding(0, 4, 14, 0)
};
option.CheckedChanged += (_, _) =>
{
if (!option.Checked)
{
return;
}
pasteIntervalMilliseconds = milliseconds;
if (rapidPaste.IsRunning)
{
rapidPaste.Start(pasteIntervalMilliseconds);
UpdateRapidPasteStatus();
}
};
return option;
}
private void SetBuildAssist(bool enabled)
{
buildAssist.SetEnabled(enabled);
buildSwitch.Text = enabled ? "自动铲子:开启" : "自动铲子:关闭";
buildSwitch.BackColor = enabled ? Color.FromArgb(210, 241, 224) : Color.FromArgb(236, 239, 241);
buildSwitch.FlatAppearance.BorderColor = enabled ? Color.FromArgb(91, 162, 119) : Color.FromArgb(174, 181, 187);
}
private void SetRapidPaste(bool enabled)
{
if (!enabled)
{
rapidPaste.Stop();
rapidPasteSwitch.Text = "极速抢车:关闭";
rapidPasteSwitch.BackColor = Color.FromArgb(236, 239, 241);
rapidPasteSwitch.FlatAppearance.BorderColor = Color.FromArgb(174, 181, 187);
rapidPasteStatus.Text = "当前状态:已停止(F9 切换)";
return;
}
string squadName = squadNameBox.Text.Trim();
if (squadName.Length == 0)
{
rapidPasteSwitch.Checked = false;
MessageBox.Show(this, "请输入有效的小队名称。", "极速抢车", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (!PrepareClipboard())
{
rapidPasteSwitch.Checked = false;
MessageBox.Show(this, "无法访问剪贴板。", "极速抢车", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
rapidPaste.Start(pasteIntervalMilliseconds);
rapidPasteSwitch.Text = "极速抢车:开启";
rapidPasteSwitch.BackColor = Color.FromArgb(210, 241, 224);
rapidPasteSwitch.FlatAppearance.BorderColor = Color.FromArgb(91, 162, 119);
UpdateRapidPasteStatus();
}
private void SetAutoRun(bool enabled)
{
autoRun.SetEnabled(enabled);
bool active = autoRun.Enabled;
autoRunSwitch.Text = active ? "自动奔跑:开启" : "自动奔跑:关闭";
autoRunSwitch.BackColor = active ? Color.FromArgb(210, 241, 224) : Color.FromArgb(236, 239, 241);
autoRunSwitch.FlatAppearance.BorderColor = active
? Color.FromArgb(91, 162, 119)
: Color.FromArgb(174, 181, 187);
}
private bool PrepareClipboard()
{
string squadName = squadNameBox.Text.Trim();
if (squadName.Length == 0)
{
return false;
}
try
{
Clipboard.SetText($"createsquad {squadName} 1");
return true;
}
catch
{
return false;
}
}
private void UpdateRapidPasteStatus()
{
rapidPasteStatus.Text = $"当前状态:运行中,{squadNameBox.Text.Trim()},间隔 {pasteIntervalMilliseconds}ms";
}
private void UpdateControl(Control control, string text)
{
if (IsDisposed || !IsHandleCreated)
{
return;
}
if (InvokeRequired)
{
BeginInvoke(() => UpdateControl(control, text));
return;
}
control.Text = text;
}
private void ShowBuildError(string message)
{
if (IsDisposed || !IsHandleCreated)
{
return;
}
BeginInvoke(() =>
{
if (buildSwitch.Checked)
{
buildSwitch.Checked = false;
}
MessageBox.Show(this, message, "自动铲子", MessageBoxButtons.OK, MessageBoxIcon.Warning);
});
}
private void ShowRapidPasteError(string message)
{
if (IsDisposed || !IsHandleCreated)
{
return;
}
BeginInvoke(() =>
{
if (rapidPasteSwitch.Checked)
{
rapidPasteSwitch.Checked = false;
}
MessageBox.Show(this, message, "极速抢车", MessageBoxButtons.OK, MessageBoxIcon.Warning);
});
}
private void ShowAutoRunError(string message)
{
if (IsDisposed || !IsHandleCreated)
{
return;
}
BeginInvoke(() =>
{
if (autoRunSwitch.Checked)
{
autoRunSwitch.Checked = false;
}
MessageBox.Show(this, message, "自动奔跑", MessageBoxButtons.OK, MessageBoxIcon.Warning);
});
}
private void SynchronizeStoppedAutoRun(string status)
{
if (IsDisposed || !IsHandleCreated)
{
return;
}
BeginInvoke(() =>
{
if (autoRunSwitch.Checked)
{
autoRunSwitch.Checked = false;
}
autoRunStatus.Text = $"当前状态:{status}(F10 切换)";
});
}
protected override void WndProc(ref Message message)
{
if (message.Msg == NativeMethods.WmHotKey && message.WParam.ToInt32() == F8HotKeyId)
{
buildSwitch.Checked = !buildSwitch.Checked;
}
if (message.Msg == NativeMethods.WmHotKey && message.WParam.ToInt32() == F9HotKeyId)
{
rapidPasteSwitch.Checked = !rapidPasteSwitch.Checked;
}
if (message.Msg == NativeMethods.WmHotKey && message.WParam.ToInt32() == F10HotKeyId)
{
autoRunSwitch.Checked = !autoRunSwitch.Checked;
}
base.WndProc(ref message);
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (!NativeMethods.RegisterHotKey(Handle, F9HotKeyId, 0, NativeMethods.VkF9))
{
int error = Marshal.GetLastWin32Error();
rapidPasteStatus.Text = $"当前状态:F9 热键注册失败(Win32 {error}),可能已被占用";
}
if (!NativeMethods.RegisterHotKey(Handle, F8HotKeyId, 0, NativeMethods.VkF8))
{
int error = Marshal.GetLastWin32Error();
buildStatus.Text = $"当前状态:F8 热键注册失败(Win32 {error}),可能已被占用";
}
if (!NativeMethods.RegisterHotKey(Handle, F10HotKeyId, 0, NativeMethods.VkF10))
{
int error = Marshal.GetLastWin32Error();
autoRunStatus.Text = $"当前状态:F10 热键注册失败(Win32 {error}),可能已被占用";
}
}
protected override void OnHandleDestroyed(EventArgs e)
{
if (Handle != IntPtr.Zero)
{
NativeMethods.UnregisterHotKey(Handle, F8HotKeyId);
NativeMethods.UnregisterHotKey(Handle, F9HotKeyId);
NativeMethods.UnregisterHotKey(Handle, F10HotKeyId);
}
base.OnHandleDestroyed(e);
}
private void OnFormClosing(object? sender, FormClosingEventArgs e)
{
if (!allowClose)
{
e.Cancel = true;
Hide();
}
}
private void ShowMainWindow()
{
Show();
WindowState = FormWindowState.Normal;
Activate();
}
private void ExitApplication()
{
allowClose = true;
Close();
}
private void DisposeServices()
{
mapGuessForm.CloseWindow();
squadLogReader.Dispose();
rapidPaste.Dispose();
autoRun.Dispose();
buildAssist.Dispose();
trayIcon.Visible = false;
trayIcon.Icon?.Dispose();
trayIcon.Dispose();
applicationIcon.Dispose();
}
internal void ToggleBuildFromShell() => buildSwitch.Checked = !buildSwitch.Checked;
internal void ToggleRapidPasteFromShell() => rapidPasteSwitch.Checked = !rapidPasteSwitch.Checked;
internal void ToggleAutoRunFromShell() => autoRunSwitch.Checked = !autoRunSwitch.Checked;
internal void ConfigureRapidPasteFromShell(string squadName, int intervalMilliseconds)
{
squadNameBox.Text = squadName;
pasteIntervalMilliseconds = intervalMilliseconds;
PrepareClipboard();
}
internal void ShowMapToolFromShell()
{
if (webView2Available)
{
mapGuessForm.ShowWindow();
}
}
internal void ShutdownFromShell()
{
allowClose = true;
Close();
}
}