一、Redis
1.1 把Redis安装为Windows服务
打开cmd控制台,进入解压或安装Redis的文件夹
1
cd C:\Redis
安装为Windows服务 执行以下命令,将Redis注册为系统服务
1
redis-server.exe --service-install redis.windows.conf --loglevel verbose
1.2 开发控制面板
Windows没有Linux的daemonize守护进程模式。在Windows上,“后台启动”通常指的就是以系统服务方式运行,因此,先把软件安装成Windows服务,然后编写一个控制台软件控制服务的启停,软件基于需求:纯Windows环境、无需安装额外软件、无需配置环境、控制台按钮控制启停+状态监控),最合适的语言是C(基于.NET Framework,Windows),而C#可以编译成单个exe文件,双击即用,无需安装任何运行环境(Windows 7及以上自带.NET Framework)。
1.3 控制台源码
创建一个新文件,命名为EnvManager.cs,复制以下代码:
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.ServiceProcess;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace 开发环境控制面板
{
public static class Program
{
[STAThread]
public static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
catch (Exception ex)
{
string logFile = Application.StartupPath + "\\error.log";
try
{
string logContent = "========== 错误日志 ==========\n";
logContent += "时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n";
logContent += "错误: " + ex.ToString() + "\n";
//File.WriteAllText(logFile, logContent, Encoding.UTF8);
}
catch { }
MessageBox.Show(
"程序启动失败!\n\n" +
"错误信息: " + ex.Message + "\n\n" +
"详细日志已保存到: " + logFile,
"启动错误",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
}
public class MainForm : Form
{
private FlowLayoutPanel flowPanel;
private Button btnRefresh, btnSettings;
private Label lblStatus, lblTip;
private List<ServiceControl> serviceControls = new List<ServiceControl>();
private string configFilePath;
private List<ServiceConfig> serviceConfigs = new List<ServiceConfig>();
// 统一的右边距(与服务卡片右侧对齐)
private const int RIGHT_MARGIN = 20;
public MainForm()
{
try
{
configFilePath = Application.StartupPath + "\\config.ini";
LoadConfig();
this.Text = "开发环境控制面板";
this.Size = new Size(700, 450);
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimumSize = new Size(650, 350);
this.BackColor = Color.FromArgb(248, 249, 250);
lblStatus = new Label();
lblStatus.Font = new Font("微软雅黑", 12, FontStyle.Bold, GraphicsUnit.Point);
lblStatus.Location = new Point(20, 15);
lblStatus.Size = new Size(400, 30);
lblStatus.Text = "✨ 服务管理面板 (共 0 个服务)";
lblStatus.TextAlign = ContentAlignment.MiddleLeft;
lblStatus.ForeColor = Color.FromArgb(44, 62, 80);
lblStatus.UseCompatibleTextRendering = true;
this.Controls.Add(lblStatus);
// ========== 提示信息 ==========
lblTip = new Label();
lblTip.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
lblTip.Location = new Point(20, 50);
lblTip.Size = new Size(350, 25);
lblTip.Text = "💡 提示:请以管理员身份运行";
lblTip.ForeColor = Color.FromArgb(149, 165, 166);
lblTip.TextAlign = ContentAlignment.MiddleLeft;
lblTip.UseCompatibleTextRendering = true;
this.Controls.Add(lblTip);
// ========== 配置按钮 - 多巴胺黄色 ==========
btnSettings = new Button();
btnSettings.Text = "配置";
btnSettings.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnSettings.Size = new Size(90, 30);
btnSettings.FlatStyle = FlatStyle.Flat;
btnSettings.FlatAppearance.BorderSize = 0;
btnSettings.BackColor = Color.FromArgb(255, 179, 71); // 多巴胺黄色
btnSettings.ForeColor = Color.White;
btnSettings.Cursor = Cursors.Hand;
btnSettings.Click += BtnSettings_Click;
this.Controls.Add(btnSettings);
// ========== 刷新按钮 - 在配置按钮左边 ==========
btnRefresh = new Button();
btnRefresh.Text = "🔄 刷新";
btnRefresh.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnRefresh.Size = new Size(90, 30);
btnRefresh.FlatStyle = FlatStyle.Flat;
btnRefresh.FlatAppearance.BorderSize = 0;
btnRefresh.BackColor = Color.FromArgb(46, 204, 113);
btnRefresh.ForeColor = Color.White;
btnRefresh.Cursor = Cursors.Hand;
btnRefresh.Click += BtnRefresh_Click;
this.Controls.Add(btnRefresh);
// ========== 流式布局面板 ==========
flowPanel = new FlowLayoutPanel();
flowPanel.Location = new Point(15, 90);
flowPanel.Size = new Size(this.ClientSize.Width - 40, this.ClientSize.Height - 120);
flowPanel.AutoScroll = true;
flowPanel.FlowDirection = FlowDirection.TopDown;
flowPanel.WrapContents = false;
flowPanel.BackColor = Color.Transparent;
this.Controls.Add(flowPanel);
// 按钮圆角
SetButtonRoundCorners(btnRefresh);
SetButtonRoundCorners(btnSettings);
// ========== 窗口大小变化事件 ==========
this.Resize += (s, e) => {
// 计算右侧对齐位置
int rightEdge = this.ClientSize.Width - RIGHT_MARGIN;
// 配置按钮放在最右边
btnSettings.Location = new Point(rightEdge - btnSettings.Width, 47);
// 刷新按钮放在配置按钮左边
btnRefresh.Location = new Point(rightEdge - btnRefresh.Width - btnSettings.Width - 8, 47);
// 更新流式面板大小
flowPanel.Size = new Size(this.ClientSize.Width - 10, this.ClientSize.Height - 120);
// 更新所有服务卡片的宽度
foreach (Control ctrl in flowPanel.Controls)
{
if (ctrl is ServiceControl)
{
ctrl.Width = flowPanel.Width - 25;
}
}
};
LoadServices();
this.Shown += (s, e) => RefreshAllStatus();
// 强制触发一次 Resize 来定位控件
this.PerformLayout();
}
catch (Exception ex)
{
MessageBox.Show("初始化失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
private void SetButtonRoundCorners(Button btn)
{
btn.Paint += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 8);
b.Region = new Region(path);
};
btn.Resize += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 8);
b.Region = new Region(path);
};
}
private GraphicsPath GetRoundedRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
path.CloseFigure();
return path;
}
private void LoadConfig()
{
serviceConfigs.Clear();
if (!File.Exists(configFilePath)) return;
try
{
string[] lines = File.ReadAllLines(configFilePath, Encoding.UTF8);
ServiceConfig current = null;
foreach (string line in lines)
{
string trimmed = line.Trim();
if (string.IsNullOrWhiteSpace(trimmed) || trimmed.StartsWith(";"))
continue;
if (trimmed.StartsWith("[") && trimmed.EndsWith("]"))
{
current = new ServiceConfig();
current.SectionName = trimmed.TrimStart('[').TrimEnd(']');
serviceConfigs.Add(current);
continue;
}
if (current == null) continue;
string[] parts = trimmed.Split(new char[] { '=' }, 2);
if (parts.Length != 2) continue;
string key = parts[0].Trim();
string value = parts[1].Trim();
switch (key)
{
case "ServiceName": current.ServiceName = value; break;
case "DisplayName": current.DisplayName = value; break;
case "StartBtnText": current.StartBtnText = value; break;
case "StopBtnText": current.StopBtnText = value; break;
}
}
serviceConfigs.RemoveAll(s => string.IsNullOrWhiteSpace(s.ServiceName));
}
catch
{
serviceConfigs.Clear();
}
}
private void SaveConfig()
{
try
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("; 开发环境控制面板配置文件");
sb.AppendLine("; 每个服务用 [服务标识] 分隔");
sb.AppendLine("; ServiceName 为 Windows 服务名称(必须)");
sb.AppendLine("; DisplayName 为显示名称(可选)");
sb.AppendLine("; StartBtnText / StopBtnText 为按钮文字(可选)");
sb.AppendLine();
foreach (var svc in serviceConfigs)
{
sb.AppendLine("[" + svc.SectionName + "]");
sb.AppendLine("ServiceName=" + svc.ServiceName);
if (!string.IsNullOrWhiteSpace(svc.DisplayName))
sb.AppendLine("DisplayName=" + svc.DisplayName);
if (!string.IsNullOrWhiteSpace(svc.StartBtnText))
sb.AppendLine("StartBtnText=" + svc.StartBtnText);
if (!string.IsNullOrWhiteSpace(svc.StopBtnText))
sb.AppendLine("StopBtnText=" + svc.StopBtnText);
sb.AppendLine();
}
File.WriteAllText(configFilePath, sb.ToString(), Encoding.UTF8);
}
catch (Exception ex)
{
MessageBox.Show("保存配置失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadServices()
{
flowPanel.Controls.Clear();
serviceControls.Clear();
if (serviceConfigs.Count == 0)
{
Label emptyLabel = new Label();
emptyLabel.Text = "🌸 暂无服务配置,请点击右上角 ⚙ 添加服务";
emptyLabel.Font = new Font("微软雅黑", 11, GraphicsUnit.Point);
emptyLabel.ForeColor = Color.FromArgb(189, 195, 199);
emptyLabel.AutoSize = true;
emptyLabel.UseCompatibleTextRendering = true;
flowPanel.Controls.Add(emptyLabel);
lblStatus.Text = "✨ 服务管理面板 (共 0 个服务)";
return;
}
foreach (var config in serviceConfigs)
{
var control = new ServiceControl(config);
control.Width = flowPanel.Width - 25;
flowPanel.Controls.Add(control);
serviceControls.Add(control);
}
lblStatus.Text = "✨ 服务管理面板 (共 " + serviceConfigs.Count + " 个服务)";
}
private void RefreshAllStatus()
{
foreach (var control in serviceControls)
{
control.RefreshStatus();
}
lblTip.Text = "✅ 已刷新所有服务状态";
lblTip.ForeColor = Color.FromArgb(46, 204, 113);
}
private void BtnRefresh_Click(object sender, EventArgs e)
{
RefreshAllStatus();
}
private void BtnSettings_Click(object sender, EventArgs e)
{
SettingsForm settingsForm = new SettingsForm(serviceConfigs);
if (settingsForm.ShowDialog() == DialogResult.OK)
{
serviceConfigs = settingsForm.ServiceConfigs;
SaveConfig();
LoadServices();
RefreshAllStatus();
MessageBox.Show("✅ 配置已保存,共管理 " + serviceConfigs.Count + " 个服务。", "保存成功",
MessageBoxButtons.OK, MessageBoxIcon.Information);
lblTip.Text = "✅ 配置已更新";
lblTip.ForeColor = Color.FromArgb(46, 204, 113);
}
}
}
public class ServiceConfig
{
public string SectionName;
public string ServiceName;
public string DisplayName;
public string StartBtnText;
public string StopBtnText;
public ServiceConfig()
{
SectionName = "Service";
ServiceName = "";
DisplayName = "";
StartBtnText = "启动";
StopBtnText = "停止";
}
}
public class ServiceControl : Panel
{
private ServiceConfig config;
private Label lblName, lblStatus, lblServiceName;
private Button btnStart, btnStop;
private Color serviceColor;
private static Color[] dopamineColors = new Color[]
{
Color.FromArgb(255, 118, 117),
Color.FromArgb(255, 179, 71),
Color.FromArgb(130, 204, 221),
Color.FromArgb(150, 220, 160),
Color.FromArgb(215, 150, 215),
Color.FromArgb(255, 200, 140),
Color.FromArgb(160, 200, 230),
Color.FromArgb(240, 180, 200),
};
private static int colorIndex = 0;
private const int BTN_WIDTH = 90;
private const int BTN_HEIGHT = 34;
private bool buttonsInitialized = false;
public ServiceControl(ServiceConfig cfg)
{
config = cfg;
serviceColor = dopamineColors[colorIndex % dopamineColors.Length];
colorIndex++;
this.Height = 60;
this.Margin = new Padding(0, 0, 0, 10);
this.BackColor = Color.White;
this.Padding = new Padding(2);
this.Paint += (s, e) => {
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
GraphicsPath path = GetRoundedRectangle(rect, 12);
using (Pen pen = new Pen(Color.FromArgb(230, 230, 235), 1))
{
g.DrawPath(pen, path);
}
};
Panel colorBar = new Panel();
colorBar.Location = new Point(0, 0);
colorBar.Size = new Size(5, this.Height);
colorBar.BackColor = serviceColor;
this.Controls.Add(colorBar);
string displayText = string.IsNullOrWhiteSpace(config.DisplayName) ? config.ServiceName : config.DisplayName;
lblName = new Label();
lblName.Text = displayText;
lblName.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold, GraphicsUnit.Point);
lblName.Location = new Point(20, 8);
lblName.Size = new Size(180, 24);
lblName.TextAlign = ContentAlignment.MiddleLeft;
lblName.ForeColor = Color.FromArgb(44, 62, 80);
lblName.UseCompatibleTextRendering = true;
this.Controls.Add(lblName);
lblServiceName = new Label();
lblServiceName.Text = config.ServiceName;
lblServiceName.Font = new Font("微软雅黑", 8, GraphicsUnit.Point);
lblServiceName.Location = new Point(20, 33);
lblServiceName.Size = new Size(180, 20);
lblServiceName.TextAlign = ContentAlignment.MiddleLeft;
lblServiceName.ForeColor = Color.FromArgb(149, 165, 166);
lblServiceName.UseCompatibleTextRendering = true;
this.Controls.Add(lblServiceName);
lblStatus = new Label();
lblStatus.Text = "⏳ 检测中...";
lblStatus.Font = new Font("微软雅黑", 9.5f, FontStyle.Bold, GraphicsUnit.Point);
lblStatus.Location = new Point(220, 14);
lblStatus.Size = new Size(120, 30);
lblStatus.TextAlign = ContentAlignment.MiddleLeft;
lblStatus.UseCompatibleTextRendering = true;
this.Controls.Add(lblStatus);
int rightMargin = 15;
int stopBtnX = this.Width - BTN_WIDTH - rightMargin;
int startBtnX = stopBtnX - BTN_WIDTH - 10;
btnStart = new Button();
btnStart.Text = string.IsNullOrWhiteSpace(config.StartBtnText) ? "▶ 启动" : "▶ " + config.StartBtnText;
btnStart.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnStart.Location = new Point(startBtnX, 12);
btnStart.Size = new Size(BTN_WIDTH, BTN_HEIGHT);
btnStart.FlatStyle = FlatStyle.Flat;
btnStart.FlatAppearance.BorderSize = 0;
btnStart.BackColor = Color.FromArgb(46, 204, 113);
btnStart.ForeColor = Color.White;
btnStart.Cursor = Cursors.Hand;
btnStart.Click += BtnStart_Click;
btnStart.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(btnStart);
btnStop = new Button();
btnStop.Text = string.IsNullOrWhiteSpace(config.StopBtnText) ? "⏹ 停止" : "⏹ " + config.StopBtnText;
btnStop.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnStop.Location = new Point(stopBtnX, 12);
btnStop.Size = new Size(BTN_WIDTH, BTN_HEIGHT);
btnStop.FlatStyle = FlatStyle.Flat;
btnStop.FlatAppearance.BorderSize = 0;
btnStop.BackColor = Color.FromArgb(231, 76, 60);
btnStop.ForeColor = Color.White;
btnStop.Cursor = Cursors.Hand;
btnStop.Click += BtnStop_Click;
btnStop.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(btnStop);
SetButtonRoundCorners(btnStart);
SetButtonRoundCorners(btnStop);
buttonsInitialized = true;
RefreshStatus();
}
private void SetButtonRoundCorners(Button btn)
{
btn.Paint += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 8);
b.Region = new Region(path);
};
btn.Resize += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 8);
b.Region = new Region(path);
};
}
private GraphicsPath GetRoundedRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
path.CloseFigure();
return path;
}
public void RefreshStatus()
{
try
{
if (!IsServiceInstalled())
{
lblStatus.Text = "❌ 未安装";
lblStatus.ForeColor = Color.FromArgb(149, 165, 166);
btnStart.Enabled = false;
btnStop.Enabled = false;
return;
}
ServiceControllerStatus status = GetStatus();
btnStart.Enabled = true;
btnStop.Enabled = true;
switch (status)
{
case ServiceControllerStatus.Running:
lblStatus.Text = "✅ 运行中";
lblStatus.ForeColor = Color.FromArgb(46, 204, 113);
btnStart.Enabled = false;
break;
case ServiceControllerStatus.Stopped:
lblStatus.Text = "⏹ 已停止";
lblStatus.ForeColor = Color.FromArgb(231, 76, 60);
btnStop.Enabled = false;
break;
case ServiceControllerStatus.StartPending:
lblStatus.Text = "⏳ 启动中...";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
break;
case ServiceControllerStatus.StopPending:
lblStatus.Text = "⏳ 停止中...";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
break;
default:
lblStatus.Text = "❓ " + status.ToString();
lblStatus.ForeColor = Color.FromArgb(149, 165, 166);
break;
}
}
catch
{
lblStatus.Text = "⚠️ 错误";
lblStatus.ForeColor = Color.FromArgb(231, 76, 60);
}
}
private bool IsServiceInstalled()
{
try { new ServiceController(config.ServiceName); return true; }
catch { return false; }
}
private ServiceControllerStatus GetStatus()
{
try { return new ServiceController(config.ServiceName).Status; }
catch { return ServiceControllerStatus.Stopped; }
}
private void BtnStart_Click(object sender, EventArgs e)
{
try
{
ServiceController sc = new ServiceController(config.ServiceName);
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(8));
lblStatus.Text = "✅ 启动成功!";
lblStatus.ForeColor = Color.FromArgb(46, 204, 113);
}
else
{
lblStatus.Text = "⚠️ 已运行中";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
}
RefreshStatus();
}
catch (Exception ex)
{
MessageBox.Show("启动 [" + config.ServiceName + "] 失败:\n" + ex.Message, "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
RefreshStatus();
}
}
private void BtnStop_Click(object sender, EventArgs e)
{
try
{
ServiceController sc = new ServiceController(config.ServiceName);
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(8));
lblStatus.Text = "✅ 停止成功!";
lblStatus.ForeColor = Color.FromArgb(46, 204, 113);
}
else
{
lblStatus.Text = "⚠️ 已停止";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
}
RefreshStatus();
}
catch (Exception ex)
{
MessageBox.Show("停止 [" + config.ServiceName + "] 失败:\n" + ex.Message, "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
RefreshStatus();
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (buttonsInitialized && btnStart != null && btnStop != null)
{
int rightMargin = 15;
int stopBtnX = this.Width - BTN_WIDTH - rightMargin;
int startBtnX = stopBtnX - BTN_WIDTH - 10;
btnStart.Location = new Point(startBtnX, 12);
btnStop.Location = new Point(stopBtnX, 12);
}
this.Invalidate();
}
}
public class SettingsForm : Form
{
private FlowLayoutPanel flowPanel;
private Button btnAdd, btnSave, btnCancel;
private List<ServiceConfig> configs;
private List<ServiceConfigEditor> editors = new List<ServiceConfigEditor>();
public List<ServiceConfig> ServiceConfigs { get; private set; }
public SettingsForm(List<ServiceConfig> existingConfigs)
{
this.Text = "⚙ 服务管理设置";
this.Size = new Size(600, 470);
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.BackColor = Color.FromArgb(248, 249, 250);
configs = new List<ServiceConfig>();
foreach (var cfg in existingConfigs)
{
ServiceConfig newCfg = new ServiceConfig();
newCfg.SectionName = cfg.SectionName;
newCfg.ServiceName = cfg.ServiceName;
newCfg.DisplayName = cfg.DisplayName;
newCfg.StartBtnText = cfg.StartBtnText;
newCfg.StopBtnText = cfg.StopBtnText;
configs.Add(newCfg);
}
if (configs.Count == 0)
{
configs.Add(CreateDefaultConfig());
}
flowPanel = new FlowLayoutPanel();
flowPanel.Location = new Point(12, 12);
flowPanel.Size = new Size(this.ClientSize.Width - 40, this.ClientSize.Height - 105);
flowPanel.AutoScroll = true;
flowPanel.FlowDirection = FlowDirection.TopDown;
flowPanel.WrapContents = false;
flowPanel.BackColor = Color.Transparent;
this.Controls.Add(flowPanel);
int btnY = this.ClientSize.Height - 78;
btnAdd = new Button();
btnAdd.Text = "+ 添加服务";
btnAdd.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnAdd.Location = new Point(12, btnY);
btnAdd.Size = new Size(110, 38);
btnAdd.FlatStyle = FlatStyle.Flat;
btnAdd.FlatAppearance.BorderSize = 0;
btnAdd.BackColor = Color.FromArgb(52, 152, 219);
btnAdd.ForeColor = Color.White;
btnAdd.Cursor = Cursors.Hand;
btnAdd.Click += BtnAdd_Click;
this.Controls.Add(btnAdd);
btnSave = new Button();
btnSave.Text = "✅ 保存配置";
btnSave.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnSave.Location = new Point(this.ClientSize.Width - 220, btnY);
btnSave.Size = new Size(100, 38);
btnSave.FlatStyle = FlatStyle.Flat;
btnSave.FlatAppearance.BorderSize = 0;
btnSave.BackColor = Color.FromArgb(46, 204, 113);
btnSave.ForeColor = Color.White;
btnSave.Cursor = Cursors.Hand;
btnSave.Click += BtnSave_Click;
this.Controls.Add(btnSave);
btnCancel = new Button();
btnCancel.Text = "取消";
btnCancel.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
btnCancel.Location = new Point(this.ClientSize.Width - 110, btnY);
btnCancel.Size = new Size(90, 38);
btnCancel.FlatStyle = FlatStyle.Flat;
btnCancel.FlatAppearance.BorderSize = 0;
btnCancel.BackColor = Color.FromArgb(149, 165, 166);
btnCancel.ForeColor = Color.White;
btnCancel.Cursor = Cursors.Hand;
btnCancel.Click += (s, e) => { this.DialogResult = DialogResult.Cancel; this.Close(); };
this.Controls.Add(btnCancel);
SetButtonRoundCorners(btnAdd);
SetButtonRoundCorners(btnSave);
SetButtonRoundCorners(btnCancel);
this.Resize += (s, e) => {
flowPanel.Size = new Size(this.ClientSize.Width - 40, this.ClientSize.Height - 105);
int newBtnY = this.ClientSize.Height - 78;
btnAdd.Location = new Point(12, newBtnY);
btnSave.Location = new Point(this.ClientSize.Width - 220, newBtnY);
btnCancel.Location = new Point(this.ClientSize.Width - 110, newBtnY);
foreach (ServiceConfigEditor editor in flowPanel.Controls)
{
editor.Width = flowPanel.Width - 20;
}
};
LoadEditors();
this.AcceptButton = btnSave;
}
private void SetButtonRoundCorners(Button btn)
{
btn.Paint += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 10);
b.Region = new Region(path);
};
btn.Resize += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 10);
b.Region = new Region(path);
};
}
private GraphicsPath GetRoundedRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
path.CloseFigure();
return path;
}
private ServiceConfig CreateDefaultConfig()
{
ServiceConfig cfg = new ServiceConfig();
cfg.SectionName = "Service" + (configs.Count + 1);
cfg.ServiceName = "";
cfg.DisplayName = "";
cfg.StartBtnText = "启动";
cfg.StopBtnText = "停止";
return cfg;
}
private void LoadEditors()
{
flowPanel.Controls.Clear();
editors.Clear();
int index = 0;
foreach (var cfg in configs)
{
var editor = new ServiceConfigEditor(cfg, index);
editor.Width = flowPanel.Width - 20;
flowPanel.Controls.Add(editor);
editors.Add(editor);
index++;
}
}
private void BtnAdd_Click(object sender, EventArgs e)
{
var cfg = CreateDefaultConfig();
cfg.SectionName = "Service" + (configs.Count + 1);
configs.Add(cfg);
LoadEditors();
if (flowPanel.Controls.Count > 0)
{
flowPanel.ScrollControlIntoView(flowPanel.Controls[flowPanel.Controls.Count - 1]);
}
}
private void BtnSave_Click(object sender, EventArgs e)
{
List<ServiceConfig> newConfigs = new List<ServiceConfig>();
foreach (var editor in editors)
{
var cfg = editor.GetConfig();
if (string.IsNullOrWhiteSpace(cfg.ServiceName))
{
MessageBox.Show("服务名称不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
newConfigs.Add(cfg);
}
if (newConfigs.Count == 0)
{
MessageBox.Show("至少添加一个服务!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
ServiceConfigs = newConfigs;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
public class ServiceConfigEditor : Panel
{
private ServiceConfig config;
private TextBox txtServiceName, txtDisplayName, txtStartBtn, txtStopBtn;
private Button btnDelete;
private Label lblIndex;
public ServiceConfigEditor(ServiceConfig cfg, int index)
{
config = cfg;
this.Height = 78;
this.Margin = new Padding(0, 0, 0, 8);
this.BackColor = Color.White;
this.Padding = new Padding(1);
this.Paint += (s, e) => {
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
GraphicsPath path = GetRoundedRectangle(rect, 10);
using (Pen pen = new Pen(Color.FromArgb(220, 220, 225), 1))
{
g.DrawPath(pen, path);
}
};
int yPos = 10;
int labelWidth = 75;
int textBoxWidth = 120;
lblIndex = new Label();
lblIndex.Text = (index + 1).ToString();
lblIndex.Font = new Font("微软雅黑", 10, FontStyle.Bold, GraphicsUnit.Point);
lblIndex.Location = new Point(8, yPos + 3);
lblIndex.Size = new Size(25, 22);
lblIndex.TextAlign = ContentAlignment.MiddleCenter;
lblIndex.ForeColor = Color.FromArgb(52, 73, 94);
lblIndex.UseCompatibleTextRendering = true;
this.Controls.Add(lblIndex);
Label lblService = new Label();
lblService.Text = "服务名:";
lblService.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
lblService.Location = new Point(40, yPos + 3);
lblService.Size = new Size(labelWidth, 22);
lblService.UseCompatibleTextRendering = true;
this.Controls.Add(lblService);
txtServiceName = new TextBox();
txtServiceName.Text = config.ServiceName;
txtServiceName.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
txtServiceName.Location = new Point(40 + labelWidth, yPos);
txtServiceName.Size = new Size(textBoxWidth, 24);
this.Controls.Add(txtServiceName);
Label lblDisplay = new Label();
lblDisplay.Text = "显示名:";
lblDisplay.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
lblDisplay.Location = new Point(40 + labelWidth + textBoxWidth + 10, yPos + 3);
lblDisplay.Size = new Size(labelWidth - 10, 22);
lblDisplay.UseCompatibleTextRendering = true;
this.Controls.Add(lblDisplay);
txtDisplayName = new TextBox();
txtDisplayName.Text = config.DisplayName;
txtDisplayName.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
txtDisplayName.Location = new Point(40 + labelWidth + textBoxWidth + 10 + labelWidth - 10, yPos);
txtDisplayName.Size = new Size(textBoxWidth - 15, 24);
this.Controls.Add(txtDisplayName);
yPos += 34;
Label lblStart = new Label();
lblStart.Text = "启动文字:";
lblStart.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
lblStart.Location = new Point(40, yPos + 2);
lblStart.Size = new Size(labelWidth, 22);
lblStart.UseCompatibleTextRendering = true;
this.Controls.Add(lblStart);
txtStartBtn = new TextBox();
txtStartBtn.Text = config.StartBtnText;
txtStartBtn.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
txtStartBtn.Location = new Point(40 + labelWidth, yPos);
txtStartBtn.Size = new Size(100, 24);
this.Controls.Add(txtStartBtn);
Label lblStop = new Label();
lblStop.Text = "停止文字:";
lblStop.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
lblStop.Location = new Point(40 + labelWidth + 110, yPos + 2);
lblStop.Size = new Size(labelWidth, 22);
lblStop.UseCompatibleTextRendering = true;
this.Controls.Add(lblStop);
txtStopBtn = new TextBox();
txtStopBtn.Text = config.StopBtnText;
txtStopBtn.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
txtStopBtn.Location = new Point(40 + labelWidth + 110 + labelWidth, yPos);
txtStopBtn.Size = new Size(100, 24);
this.Controls.Add(txtStopBtn);
btnDelete = new Button();
btnDelete.Text = "✕";
btnDelete.Font = new Font("Segoe UI", 10, FontStyle.Bold, GraphicsUnit.Point);
btnDelete.Location = new Point(this.Width - 38, 8);
btnDelete.Size = new Size(28, 28);
btnDelete.FlatStyle = FlatStyle.Flat;
btnDelete.FlatAppearance.BorderSize = 0;
btnDelete.BackColor = Color.FromArgb(231, 76, 60);
btnDelete.ForeColor = Color.White;
btnDelete.Cursor = Cursors.Hand;
btnDelete.Click += BtnDelete_Click;
this.Controls.Add(btnDelete);
this.Resize += (s, e) => {
btnDelete.Location = new Point(this.Width - 38, 8);
};
}
private GraphicsPath GetRoundedRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
path.CloseFigure();
return path;
}
public ServiceConfig GetConfig()
{
ServiceConfig cfg = new ServiceConfig();
cfg.SectionName = config.SectionName;
cfg.ServiceName = txtServiceName.Text.Trim();
cfg.DisplayName = txtDisplayName.Text.Trim();
cfg.StartBtnText = string.IsNullOrWhiteSpace(txtStartBtn.Text) ? "启动" : txtStartBtn.Text.Trim();
cfg.StopBtnText = string.IsNullOrWhiteSpace(txtStopBtn.Text) ? "停止" : txtStopBtn.Text.Trim();
return cfg;
}
private void BtnDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定要删除此服务配置吗?", "确认删除",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Parent.Controls.Remove(this);
}
}
}
}
使用Windows自带的C#编译器编译代码。将上面的代码保存为EnvManager.cs,打开命令提示符,执行编译命令
1
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:winexe /reference:System.Windows.Forms.dll /reference:System.ServiceProcess.dll EnvManager.cs
编译成功后,会在当前目录生成EnvManager.exe,双击即可运行
二、MySQL
2.1 单例模式启停MySQL
1
2
3
4
# 启动
mysqld --standalone
# 停止
mysqladmin -u用户名 -p密码 shutdown
2.2 为控制面板添加进程模式
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.ServiceProcess;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace 开发环境控制面板
{
public static class Program
{
[STAThread]
public static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
catch (Exception ex)
{
string logFile = Application.StartupPath + "\\error.log";
try
{
string logContent = "========== 错误日志 ==========\n";
logContent += "时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n";
logContent += "错误: " + ex.ToString() + "\n";
File.WriteAllText(logFile, logContent, Encoding.UTF8);
}
catch { }
MessageBox.Show(
"程序启动失败!\n\n" +
"错误信息: " + ex.Message + "\n\n" +
"详细日志已保存到: " + logFile,
"启动错误",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
}
}
}
public class MainForm : Form
{
private FlowLayoutPanel flowPanel;
private Button btnRefresh, btnSettings;
private Label lblStatus, lblTip;
private List<ServiceControl> serviceControls = new List<ServiceControl>();
private string configFilePath;
private List<ServiceConfig> serviceConfigs = new List<ServiceConfig>();
private const int RIGHT_MARGIN = 20;
public MainForm()
{
try
{
configFilePath = Application.StartupPath + "\\config.ini";
LoadConfig();
this.Text = "开发环境控制面板";
this.Size = new Size(750, 450);
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimumSize = new Size(680, 350);
this.BackColor = Color.FromArgb(248, 249, 250);
lblStatus = new Label();
lblStatus.Font = new Font("微软雅黑", 12, FontStyle.Bold, GraphicsUnit.Point);
lblStatus.Location = new Point(20, 15);
lblStatus.Size = new Size(400, 30);
lblStatus.Text = "✨ 服务管理面板 (共 0 个服务)";
lblStatus.TextAlign = ContentAlignment.MiddleLeft;
lblStatus.ForeColor = Color.FromArgb(44, 62, 80);
lblStatus.UseCompatibleTextRendering = true;
this.Controls.Add(lblStatus);
lblTip = new Label();
lblTip.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
lblTip.Location = new Point(20, 50);
lblTip.Size = new Size(400, 25);
lblTip.Text = "💡 提示:请以管理员身份运行";
lblTip.ForeColor = Color.FromArgb(149, 165, 166);
lblTip.TextAlign = ContentAlignment.MiddleLeft;
lblTip.UseCompatibleTextRendering = true;
this.Controls.Add(lblTip);
btnSettings = new Button();
btnSettings.Text = "⚙ 配置";
btnSettings.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnSettings.Size = new Size(90, 30);
btnSettings.FlatStyle = FlatStyle.Flat;
btnSettings.FlatAppearance.BorderSize = 0;
btnSettings.BackColor = Color.FromArgb(255, 179, 71);
btnSettings.ForeColor = Color.White;
btnSettings.Cursor = Cursors.Hand;
btnSettings.Click += BtnSettings_Click;
this.Controls.Add(btnSettings);
btnRefresh = new Button();
btnRefresh.Text = "🔄 刷新";
btnRefresh.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnRefresh.Size = new Size(90, 30);
btnRefresh.FlatStyle = FlatStyle.Flat;
btnRefresh.FlatAppearance.BorderSize = 0;
btnRefresh.BackColor = Color.FromArgb(46, 204, 113);
btnRefresh.ForeColor = Color.White;
btnRefresh.Cursor = Cursors.Hand;
btnRefresh.Click += BtnRefresh_Click;
this.Controls.Add(btnRefresh);
flowPanel = new FlowLayoutPanel();
flowPanel.Location = new Point(15, 90);
flowPanel.Size = new Size(this.ClientSize.Width - 40, this.ClientSize.Height - 120);
flowPanel.AutoScroll = true;
flowPanel.FlowDirection = FlowDirection.TopDown;
flowPanel.WrapContents = false;
flowPanel.BackColor = Color.Transparent;
this.Controls.Add(flowPanel);
SetButtonRoundCorners(btnRefresh);
SetButtonRoundCorners(btnSettings);
this.Resize += (s, e) => {
int rightEdge = this.ClientSize.Width - RIGHT_MARGIN;
btnSettings.Location = new Point(rightEdge - btnSettings.Width, 47);
btnRefresh.Location = new Point(rightEdge - btnRefresh.Width - btnSettings.Width - 8, 47);
flowPanel.Size = new Size(this.ClientSize.Width - 10, this.ClientSize.Height - 120);
foreach (Control ctrl in flowPanel.Controls)
{
if (ctrl is ServiceControl)
{
ctrl.Width = flowPanel.Width - 25;
}
}
};
LoadServices();
this.Shown += (s, e) => RefreshAllStatus();
this.PerformLayout();
}
catch (Exception ex)
{
MessageBox.Show("初始化失败: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
private void SetButtonRoundCorners(Button btn)
{
btn.Paint += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 8);
b.Region = new Region(path);
};
btn.Resize += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 8);
b.Region = new Region(path);
};
}
private GraphicsPath GetRoundedRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
path.CloseFigure();
return path;
}
private void LoadConfig()
{
serviceConfigs.Clear();
if (!File.Exists(configFilePath)) return;
try
{
string[] lines = File.ReadAllLines(configFilePath, Encoding.UTF8);
ServiceConfig current = null;
foreach (string line in lines)
{
string trimmed = line.Trim();
if (string.IsNullOrWhiteSpace(trimmed) || trimmed.StartsWith(";"))
continue;
if (trimmed.StartsWith("[") && trimmed.EndsWith("]"))
{
current = new ServiceConfig();
current.SectionName = trimmed.TrimStart('[').TrimEnd(']');
serviceConfigs.Add(current);
continue;
}
if (current == null) continue;
string[] parts = trimmed.Split(new char[] { '=' }, 2);
if (parts.Length != 2) continue;
string key = parts[0].Trim();
string value = parts[1].Trim();
switch (key)
{
case "ServiceName": current.ServiceName = value; break;
case "DisplayName": current.DisplayName = value; break;
case "StartBtnText": current.StartBtnText = value; break;
case "StopBtnText": current.StopBtnText = value; break;
case "StartMode": current.StartMode = value; break;
case "ProcessPath": current.ProcessPath = value; break;
case "ProcessArgs": current.ProcessArgs = value; break;
case "ProcessName": current.ProcessName = value; break;
}
}
serviceConfigs.RemoveAll(s => string.IsNullOrWhiteSpace(s.ServiceName));
}
catch
{
serviceConfigs.Clear();
}
}
private void SaveConfig()
{
try
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("; 开发环境控制面板配置文件");
sb.AppendLine("; 每个服务用 [服务标识] 分隔");
sb.AppendLine("; StartMode: service(服务模式) 或 process(进程模式)");
sb.AppendLine("; ServiceName: Windows服务名称(服务模式必填)");
sb.AppendLine("; ProcessPath: 可执行文件路径(进程模式必填)");
sb.AppendLine("; ProcessArgs: 启动参数(进程模式可选)");
sb.AppendLine("; ProcessName: 进程名称用于检测运行状态(进程模式必填)");
sb.AppendLine();
foreach (var svc in serviceConfigs)
{
sb.AppendLine("[" + svc.SectionName + "]");
sb.AppendLine("ServiceName=" + svc.ServiceName);
if (!string.IsNullOrWhiteSpace(svc.DisplayName))
sb.AppendLine("DisplayName=" + svc.DisplayName);
if (!string.IsNullOrWhiteSpace(svc.StartBtnText))
sb.AppendLine("StartBtnText=" + svc.StartBtnText);
if (!string.IsNullOrWhiteSpace(svc.StopBtnText))
sb.AppendLine("StopBtnText=" + svc.StopBtnText);
sb.AppendLine("StartMode=" + svc.StartMode);
if (!string.IsNullOrWhiteSpace(svc.ProcessPath))
sb.AppendLine("ProcessPath=" + svc.ProcessPath);
if (!string.IsNullOrWhiteSpace(svc.ProcessArgs))
sb.AppendLine("ProcessArgs=" + svc.ProcessArgs);
if (!string.IsNullOrWhiteSpace(svc.ProcessName))
sb.AppendLine("ProcessName=" + svc.ProcessName);
sb.AppendLine();
}
File.WriteAllText(configFilePath, sb.ToString(), Encoding.UTF8);
}
catch (Exception ex)
{
MessageBox.Show("保存配置失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadServices()
{
flowPanel.Controls.Clear();
serviceControls.Clear();
if (serviceConfigs.Count == 0)
{
Label emptyLabel = new Label();
emptyLabel.Text = "🌸 暂无服务配置,请点击右上角 ⚙ 添加服务";
emptyLabel.Font = new Font("微软雅黑", 11, GraphicsUnit.Point);
emptyLabel.ForeColor = Color.FromArgb(189, 195, 199);
emptyLabel.AutoSize = true;
emptyLabel.UseCompatibleTextRendering = true;
flowPanel.Controls.Add(emptyLabel);
lblStatus.Text = "✨ 服务管理面板 (共 0 个服务)";
return;
}
foreach (var config in serviceConfigs)
{
var control = new ServiceControl(config);
control.Width = flowPanel.Width - 25;
flowPanel.Controls.Add(control);
serviceControls.Add(control);
}
lblStatus.Text = "✨ 服务管理面板 (共 " + serviceConfigs.Count + " 个服务)";
}
private void RefreshAllStatus()
{
foreach (var control in serviceControls)
{
control.RefreshStatus();
}
lblTip.Text = "✅ 已刷新所有服务状态";
lblTip.ForeColor = Color.FromArgb(46, 204, 113);
}
private void BtnRefresh_Click(object sender, EventArgs e)
{
RefreshAllStatus();
}
private void BtnSettings_Click(object sender, EventArgs e)
{
SettingsForm settingsForm = new SettingsForm(serviceConfigs);
if (settingsForm.ShowDialog() == DialogResult.OK)
{
serviceConfigs = settingsForm.ServiceConfigs;
SaveConfig();
LoadServices();
RefreshAllStatus();
MessageBox.Show("✅ 配置已保存,共管理 " + serviceConfigs.Count + " 个服务。", "保存成功",
MessageBoxButtons.OK, MessageBoxIcon.Information);
lblTip.Text = "✅ 配置已更新";
lblTip.ForeColor = Color.FromArgb(46, 204, 113);
}
}
}
public class ServiceConfig
{
public string SectionName;
public string ServiceName;
public string DisplayName;
public string StartBtnText;
public string StopBtnText;
public string StartMode;
public string ProcessPath;
public string ProcessArgs;
public string ProcessName;
public ServiceConfig()
{
SectionName = "Service";
ServiceName = "";
DisplayName = "";
StartBtnText = "启动";
StopBtnText = "停止";
StartMode = "service";
ProcessPath = "";
ProcessArgs = "";
ProcessName = "";
}
}
public class ServiceControl : Panel
{
private ServiceConfig config;
private Label lblName, lblStatus, lblServiceName, lblModeTag;
private Button btnStart, btnStop;
private Color serviceColor;
private Process runningProcess;
private static Color[] dopamineColors = new Color[]
{
Color.FromArgb(255, 118, 117),
Color.FromArgb(255, 179, 71),
Color.FromArgb(130, 204, 221),
Color.FromArgb(150, 220, 160),
Color.FromArgb(215, 150, 215),
Color.FromArgb(255, 200, 140),
Color.FromArgb(160, 200, 230),
Color.FromArgb(240, 180, 200),
};
private static int colorIndex = 0;
private const int BTN_WIDTH = 90;
private const int BTN_HEIGHT = 34;
private bool buttonsInitialized = false;
public ServiceControl(ServiceConfig cfg)
{
config = cfg;
serviceColor = dopamineColors[colorIndex % dopamineColors.Length];
colorIndex++;
this.Height = 60;
this.Margin = new Padding(0, 0, 0, 10);
this.BackColor = Color.White;
this.Padding = new Padding(2);
this.Paint += (s, e) => {
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
GraphicsPath path = GetRoundedRectangle(rect, 12);
using (Pen pen = new Pen(Color.FromArgb(230, 230, 235), 1))
{
g.DrawPath(pen, path);
}
};
Panel colorBar = new Panel();
colorBar.Location = new Point(0, 0);
colorBar.Size = new Size(5, this.Height);
colorBar.BackColor = serviceColor;
this.Controls.Add(colorBar);
string displayText = string.IsNullOrWhiteSpace(config.DisplayName) ? config.ServiceName : config.DisplayName;
lblName = new Label();
lblName.Text = displayText;
lblName.Font = new Font("微软雅黑", 10.5f, FontStyle.Bold, GraphicsUnit.Point);
lblName.Location = new Point(20, 6);
lblName.Size = new Size(160, 24);
lblName.TextAlign = ContentAlignment.MiddleLeft;
lblName.ForeColor = Color.FromArgb(44, 62, 80);
lblName.UseCompatibleTextRendering = true;
this.Controls.Add(lblName);
string modeText = config.StartMode == "process" ? "进程" : "服务";
lblModeTag = new Label();
lblModeTag.Text = modeText;
lblModeTag.Font = new Font("微软雅黑", 7.5f, FontStyle.Regular, GraphicsUnit.Point);
lblModeTag.Location = new Point(172, 8);
lblModeTag.Size = new Size(55, 20);
lblModeTag.TextAlign = ContentAlignment.MiddleCenter;
lblModeTag.ForeColor = Color.White;
lblModeTag.BackColor = config.StartMode == "process" ? Color.FromArgb(52, 152, 219) : Color.FromArgb(155, 89, 182);
lblModeTag.UseCompatibleTextRendering = true;
this.Controls.Add(lblModeTag);
string subText = config.StartMode == "process" ? config.ProcessName : config.ServiceName;
lblServiceName = new Label();
lblServiceName.Text = subText;
lblServiceName.Font = new Font("微软雅黑", 8, GraphicsUnit.Point);
lblServiceName.Location = new Point(20, 33);
lblServiceName.Size = new Size(200, 20);
lblServiceName.TextAlign = ContentAlignment.MiddleLeft;
lblServiceName.ForeColor = Color.FromArgb(149, 165, 166);
lblServiceName.UseCompatibleTextRendering = true;
this.Controls.Add(lblServiceName);
lblStatus = new Label();
lblStatus.Text = "⏳ 检测中...";
lblStatus.Font = new Font("微软雅黑", 9.5f, FontStyle.Bold, GraphicsUnit.Point);
lblStatus.Location = new Point(230, 14);
lblStatus.Size = new Size(120, 30);
lblStatus.TextAlign = ContentAlignment.MiddleLeft;
lblStatus.UseCompatibleTextRendering = true;
this.Controls.Add(lblStatus);
int rightMargin = 15;
int stopBtnX = this.Width - BTN_WIDTH - rightMargin;
int startBtnX = stopBtnX - BTN_WIDTH - 10;
btnStart = new Button();
btnStart.Text = string.IsNullOrWhiteSpace(config.StartBtnText) ? "▶ 启动" : "▶ " + config.StartBtnText;
btnStart.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnStart.Location = new Point(startBtnX, 12);
btnStart.Size = new Size(BTN_WIDTH, BTN_HEIGHT);
btnStart.FlatStyle = FlatStyle.Flat;
btnStart.FlatAppearance.BorderSize = 0;
btnStart.BackColor = Color.FromArgb(46, 204, 113);
btnStart.ForeColor = Color.White;
btnStart.Cursor = Cursors.Hand;
btnStart.Click += BtnStart_Click;
btnStart.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(btnStart);
btnStop = new Button();
btnStop.Text = string.IsNullOrWhiteSpace(config.StopBtnText) ? "⏹ 停止" : "⏹ " + config.StopBtnText;
btnStop.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnStop.Location = new Point(stopBtnX, 12);
btnStop.Size = new Size(BTN_WIDTH, BTN_HEIGHT);
btnStop.FlatStyle = FlatStyle.Flat;
btnStop.FlatAppearance.BorderSize = 0;
btnStop.BackColor = Color.FromArgb(231, 76, 60);
btnStop.ForeColor = Color.White;
btnStop.Cursor = Cursors.Hand;
btnStop.Click += BtnStop_Click;
btnStop.TextAlign = ContentAlignment.MiddleCenter;
this.Controls.Add(btnStop);
SetButtonRoundCorners(btnStart);
SetButtonRoundCorners(btnStop);
buttonsInitialized = true;
RefreshStatus();
}
private void SetButtonRoundCorners(Button btn)
{
btn.Paint += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 8);
b.Region = new Region(path);
};
btn.Resize += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 8);
b.Region = new Region(path);
};
}
private GraphicsPath GetRoundedRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
path.CloseFigure();
return path;
}
public void RefreshStatus()
{
try
{
if (config.StartMode == "process")
{
RefreshProcessStatus();
}
else
{
RefreshServiceStatus();
}
}
catch
{
lblStatus.Text = "⚠️ 错误";
lblStatus.ForeColor = Color.FromArgb(231, 76, 60);
}
}
private void RefreshServiceStatus()
{
if (!IsServiceInstalled())
{
lblStatus.Text = "❌ 未安装";
lblStatus.ForeColor = Color.FromArgb(149, 165, 166);
btnStart.Enabled = false;
btnStop.Enabled = false;
return;
}
ServiceControllerStatus status = GetServiceStatus();
btnStart.Enabled = true;
btnStop.Enabled = true;
switch (status)
{
case ServiceControllerStatus.Running:
lblStatus.Text = "✅ 运行中";
lblStatus.ForeColor = Color.FromArgb(46, 204, 113);
btnStart.Enabled = false;
break;
case ServiceControllerStatus.Stopped:
lblStatus.Text = "⏹ 已停止";
lblStatus.ForeColor = Color.FromArgb(231, 76, 60);
btnStop.Enabled = false;
break;
case ServiceControllerStatus.StartPending:
lblStatus.Text = "⏳ 启动中...";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
break;
case ServiceControllerStatus.StopPending:
lblStatus.Text = "⏳ 停止中...";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
break;
default:
lblStatus.Text = "❓ " + status.ToString();
lblStatus.ForeColor = Color.FromArgb(149, 165, 166);
break;
}
}
private void RefreshProcessStatus()
{
bool isRunning = IsProcessRunning();
if (!isRunning)
{
lblStatus.Text = "⏹ 已停止";
lblStatus.ForeColor = Color.FromArgb(231, 76, 60);
btnStart.Enabled = true;
btnStop.Enabled = false;
}
else
{
lblStatus.Text = "✅ 运行中";
lblStatus.ForeColor = Color.FromArgb(46, 204, 113);
btnStart.Enabled = false;
btnStop.Enabled = true;
}
}
private bool IsServiceInstalled()
{
try { new ServiceController(config.ServiceName); return true; }
catch { return false; }
}
private ServiceControllerStatus GetServiceStatus()
{
try { return new ServiceController(config.ServiceName).Status; }
catch { return ServiceControllerStatus.Stopped; }
}
private bool IsProcessRunning()
{
if (string.IsNullOrWhiteSpace(config.ProcessName))
return false;
try
{
Process[] processes = Process.GetProcessesByName(config.ProcessName);
return processes.Length > 0;
}
catch
{
return false;
}
}
private void KillProcess()
{
if (string.IsNullOrWhiteSpace(config.ProcessName))
return;
try
{
Process[] processes = Process.GetProcessesByName(config.ProcessName);
foreach (Process p in processes)
{
try
{
p.Kill();
p.WaitForExit(3000);
}
catch { }
}
}
catch { }
}
private void BtnStart_Click(object sender, EventArgs e)
{
try
{
if (config.StartMode == "process")
{
StartProcess();
}
else
{
StartService();
}
RefreshStatus();
}
catch (Exception ex)
{
MessageBox.Show("启动 [" + config.ServiceName + "] 失败:\n" + ex.Message, "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
RefreshStatus();
}
}
private void StartService()
{
ServiceController sc = new ServiceController(config.ServiceName);
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(10));
lblStatus.Text = "✅ 启动成功!";
lblStatus.ForeColor = Color.FromArgb(46, 204, 113);
}
else
{
lblStatus.Text = "⚠️ 已运行中";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
}
}
private void StartProcess()
{
if (IsProcessRunning())
{
lblStatus.Text = "⚠️ 已运行中";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
return;
}
if (string.IsNullOrWhiteSpace(config.ProcessPath))
{
throw new Exception("未配置进程路径 (ProcessPath)");
}
if (!File.Exists(config.ProcessPath))
{
throw new Exception("进程文件不存在: " + config.ProcessPath);
}
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = config.ProcessPath;
startInfo.Arguments = config.ProcessArgs ?? "";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
runningProcess = Process.Start(startInfo);
Thread.Sleep(1000);
if (IsProcessRunning())
{
lblStatus.Text = "✅ 启动成功!";
lblStatus.ForeColor = Color.FromArgb(46, 204, 113);
}
else
{
throw new Exception("进程启动后未检测到运行,请检查配置");
}
}
private void BtnStop_Click(object sender, EventArgs e)
{
try
{
if (config.StartMode == "process")
{
StopProcess();
}
else
{
StopService();
}
RefreshStatus();
}
catch (Exception ex)
{
MessageBox.Show("停止 [" + config.ServiceName + "] 失败:\n" + ex.Message, "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
RefreshStatus();
}
}
private void StopService()
{
ServiceController sc = new ServiceController(config.ServiceName);
if (sc.Status == ServiceControllerStatus.Running)
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
lblStatus.Text = "✅ 停止成功!";
lblStatus.ForeColor = Color.FromArgb(46, 204, 113);
}
else
{
lblStatus.Text = "⚠️ 已停止";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
}
}
private void StopProcess()
{
if (!IsProcessRunning())
{
lblStatus.Text = "⚠️ 已停止";
lblStatus.ForeColor = Color.FromArgb(241, 196, 15);
return;
}
KillProcess();
Thread.Sleep(500);
if (!IsProcessRunning())
{
lblStatus.Text = "✅ 停止成功!";
lblStatus.ForeColor = Color.FromArgb(46, 204, 113);
}
else
{
throw new Exception("进程停止失败");
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (buttonsInitialized && btnStart != null && btnStop != null)
{
int rightMargin = 15;
int stopBtnX = this.Width - BTN_WIDTH - rightMargin;
int startBtnX = stopBtnX - BTN_WIDTH - 10;
btnStart.Location = new Point(startBtnX, 12);
btnStop.Location = new Point(stopBtnX, 12);
}
this.Invalidate();
}
}
public class SettingsForm : Form
{
private FlowLayoutPanel flowPanel;
private Button btnAdd, btnSave, btnCancel;
private List<ServiceConfig> configs;
private List<ServiceConfigEditor> editors = new List<ServiceConfigEditor>();
public List<ServiceConfig> ServiceConfigs { get; private set; }
public SettingsForm(List<ServiceConfig> existingConfigs)
{
this.Text = "⚙ 服务管理设置";
this.Size = new Size(680, 520);
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.BackColor = Color.FromArgb(248, 249, 250);
configs = new List<ServiceConfig>();
foreach (var cfg in existingConfigs)
{
ServiceConfig newCfg = new ServiceConfig();
newCfg.SectionName = cfg.SectionName;
newCfg.ServiceName = cfg.ServiceName;
newCfg.DisplayName = cfg.DisplayName;
newCfg.StartBtnText = cfg.StartBtnText;
newCfg.StopBtnText = cfg.StopBtnText;
newCfg.StartMode = cfg.StartMode;
newCfg.ProcessPath = cfg.ProcessPath;
newCfg.ProcessArgs = cfg.ProcessArgs;
newCfg.ProcessName = cfg.ProcessName;
configs.Add(newCfg);
}
if (configs.Count == 0)
{
configs.Add(CreateDefaultConfig());
}
flowPanel = new FlowLayoutPanel();
flowPanel.Location = new Point(12, 12);
flowPanel.Size = new Size(this.ClientSize.Width - 40, this.ClientSize.Height - 105);
flowPanel.AutoScroll = true;
flowPanel.FlowDirection = FlowDirection.TopDown;
flowPanel.WrapContents = false;
flowPanel.BackColor = Color.Transparent;
this.Controls.Add(flowPanel);
int btnY = this.ClientSize.Height - 78;
btnAdd = new Button();
btnAdd.Text = "+ 添加服务";
btnAdd.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnAdd.Location = new Point(12, btnY);
btnAdd.Size = new Size(110, 38);
btnAdd.FlatStyle = FlatStyle.Flat;
btnAdd.FlatAppearance.BorderSize = 0;
btnAdd.BackColor = Color.FromArgb(52, 152, 219);
btnAdd.ForeColor = Color.White;
btnAdd.Cursor = Cursors.Hand;
btnAdd.Click += BtnAdd_Click;
this.Controls.Add(btnAdd);
btnSave = new Button();
btnSave.Text = "✅ 保存配置";
btnSave.Font = new Font("微软雅黑", 9, FontStyle.Bold, GraphicsUnit.Point);
btnSave.Location = new Point(this.ClientSize.Width - 220, btnY);
btnSave.Size = new Size(100, 38);
btnSave.FlatStyle = FlatStyle.Flat;
btnSave.FlatAppearance.BorderSize = 0;
btnSave.BackColor = Color.FromArgb(46, 204, 113);
btnSave.ForeColor = Color.White;
btnSave.Cursor = Cursors.Hand;
btnSave.Click += BtnSave_Click;
this.Controls.Add(btnSave);
btnCancel = new Button();
btnCancel.Text = "取消";
btnCancel.Font = new Font("微软雅黑", 9, GraphicsUnit.Point);
btnCancel.Location = new Point(this.ClientSize.Width - 110, btnY);
btnCancel.Size = new Size(90, 38);
btnCancel.FlatStyle = FlatStyle.Flat;
btnCancel.FlatAppearance.BorderSize = 0;
btnCancel.BackColor = Color.FromArgb(149, 165, 166);
btnCancel.ForeColor = Color.White;
btnCancel.Cursor = Cursors.Hand;
btnCancel.Click += (s, e) => { this.DialogResult = DialogResult.Cancel; this.Close(); };
this.Controls.Add(btnCancel);
SetButtonRoundCorners(btnAdd);
SetButtonRoundCorners(btnSave);
SetButtonRoundCorners(btnCancel);
this.Resize += (s, e) => {
flowPanel.Size = new Size(this.ClientSize.Width - 40, this.ClientSize.Height - 105);
int newBtnY = this.ClientSize.Height - 78;
btnAdd.Location = new Point(12, newBtnY);
btnSave.Location = new Point(this.ClientSize.Width - 220, newBtnY);
btnCancel.Location = new Point(this.ClientSize.Width - 110, newBtnY);
foreach (ServiceConfigEditor editor in flowPanel.Controls)
{
editor.Width = flowPanel.Width - 20;
}
};
LoadEditors();
this.AcceptButton = btnSave;
}
private void SetButtonRoundCorners(Button btn)
{
btn.Paint += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 10);
b.Region = new Region(path);
};
btn.Resize += (s, e) => {
Button b = s as Button;
GraphicsPath path = GetRoundedRectangle(new Rectangle(0, 0, b.Width, b.Height), 10);
b.Region = new Region(path);
};
}
private GraphicsPath GetRoundedRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
path.CloseFigure();
return path;
}
private ServiceConfig CreateDefaultConfig()
{
ServiceConfig cfg = new ServiceConfig();
cfg.SectionName = "Service" + (configs.Count + 1);
cfg.ServiceName = "";
cfg.DisplayName = "";
cfg.StartBtnText = "启动";
cfg.StopBtnText = "停止";
cfg.StartMode = "service";
cfg.ProcessPath = "";
cfg.ProcessArgs = "";
cfg.ProcessName = "";
return cfg;
}
private void LoadEditors()
{
flowPanel.Controls.Clear();
editors.Clear();
int index = 0;
foreach (var cfg in configs)
{
var editor = new ServiceConfigEditor(cfg, index);
editor.Width = flowPanel.Width - 20;
flowPanel.Controls.Add(editor);
editors.Add(editor);
index++;
}
}
private void BtnAdd_Click(object sender, EventArgs e)
{
var cfg = CreateDefaultConfig();
cfg.SectionName = "Service" + (configs.Count + 1);
configs.Add(cfg);
LoadEditors();
if (flowPanel.Controls.Count > 0)
{
flowPanel.ScrollControlIntoView(flowPanel.Controls[flowPanel.Controls.Count - 1]);
}
}
private void BtnSave_Click(object sender, EventArgs e)
{
List<ServiceConfig> newConfigs = new List<ServiceConfig>();
foreach (var editor in editors)
{
var cfg = editor.GetConfig();
if (string.IsNullOrWhiteSpace(cfg.ServiceName))
{
MessageBox.Show("服务名称不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (cfg.StartMode == "process")
{
if (string.IsNullOrWhiteSpace(cfg.ProcessPath))
{
MessageBox.Show("进程模式需要填写进程路径!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrWhiteSpace(cfg.ProcessName))
{
MessageBox.Show("进程模式需要填写进程名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
}
newConfigs.Add(cfg);
}
if (newConfigs.Count == 0)
{
MessageBox.Show("至少添加一个服务!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
ServiceConfigs = newConfigs;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
// ============ ServiceConfigEditor(修复版) ============
public class ServiceConfigEditor : Panel
{
private ServiceConfig config;
private TextBox txtServiceName, txtDisplayName, txtStartBtn, txtStopBtn;
private TextBox txtProcessPath, txtProcessArgs, txtProcessName;
private ComboBox cmbStartMode;
private Button btnDelete;
private Label lblIndex;
public ServiceConfigEditor(ServiceConfig cfg, int index)
{
config = cfg;
this.Height = 135;
this.Margin = new Padding(0, 0, 0, 8);
this.BackColor = Color.White;
this.Padding = new Padding(1);
this.Paint += (s, e) => {
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
GraphicsPath path = GetRoundedRectangle(rect, 10);
using (Pen pen = new Pen(Color.FromArgb(220, 220, 225), 1))
{
g.DrawPath(pen, path);
}
};
int yPos = 8;
int labelWidth = 75;
int textBoxWidth = 120;
lblIndex = new Label();
lblIndex.Text = (index + 1).ToString();
lblIndex.Font = new Font("微软雅黑", 10, FontStyle.Bold, GraphicsUnit.Point);
lblIndex.Location = new Point(8, yPos + 2);
lblIndex.Size = new Size(25, 22);
lblIndex.TextAlign = ContentAlignment.MiddleCenter;
lblIndex.ForeColor = Color.FromArgb(52, 73, 94);
lblIndex.UseCompatibleTextRendering = true;
this.Controls.Add(lblIndex);
// 第一行:服务名 + 显示名 + 启动模式
Label lblService = new Label();
lblService.Text = "服务名:";
lblService.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
lblService.Location = new Point(40, yPos + 2);
lblService.Size = new Size(labelWidth, 22);
lblService.UseCompatibleTextRendering = true;
this.Controls.Add(lblService);
txtServiceName = new TextBox();
txtServiceName.Text = config.ServiceName;
txtServiceName.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
txtServiceName.Location = new Point(40 + labelWidth, yPos);
txtServiceName.Size = new Size(textBoxWidth, 24);
this.Controls.Add(txtServiceName);
Label lblDisplay = new Label();
lblDisplay.Text = "显示名:";
lblDisplay.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
lblDisplay.Location = new Point(40 + labelWidth + textBoxWidth + 8, yPos + 2);
lblDisplay.Size = new Size(labelWidth - 10, 22);
lblDisplay.UseCompatibleTextRendering = true;
this.Controls.Add(lblDisplay);
txtDisplayName = new TextBox();
txtDisplayName.Text = config.DisplayName;
txtDisplayName.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
txtDisplayName.Location = new Point(40 + labelWidth + textBoxWidth + 8 + labelWidth - 10, yPos);
txtDisplayName.Size = new Size(textBoxWidth - 10, 24);
this.Controls.Add(txtDisplayName);
// 启动模式
Label lblMode = new Label();
lblMode.Text = "模式:";
lblMode.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
lblMode.Location = new Point(40 + labelWidth + textBoxWidth + 8 + labelWidth - 10 + textBoxWidth - 10 + 8, yPos + 2);
lblMode.Size = new Size(40, 22);
lblMode.UseCompatibleTextRendering = true;
this.Controls.Add(lblMode);
cmbStartMode = new ComboBox();
cmbStartMode.Items.Add("service");
cmbStartMode.Items.Add("process");
cmbStartMode.Text = config.StartMode;
cmbStartMode.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
cmbStartMode.Location = new Point(40 + labelWidth + textBoxWidth + 8 + labelWidth - 10 + textBoxWidth - 10 + 8 + 40, yPos);
cmbStartMode.Size = new Size(80, 24);
cmbStartMode.DropDownStyle = ComboBoxStyle.DropDownList;
cmbStartMode.SelectedIndexChanged += (s, e) => {
bool isProcess = cmbStartMode.Text == "process";
txtProcessPath.Enabled = isProcess;
txtProcessArgs.Enabled = isProcess;
txtProcessName.Enabled = isProcess;
};
this.Controls.Add(cmbStartMode);
yPos += 30;
// 第二行:进程路径
Label lblPath = new Label();
lblPath.Text = "进程路径:";
lblPath.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
lblPath.Location = new Point(40, yPos + 2);
lblPath.Size = new Size(labelWidth, 22);
lblPath.UseCompatibleTextRendering = true;
this.Controls.Add(lblPath);
txtProcessPath = new TextBox();
txtProcessPath.Text = config.ProcessPath;
txtProcessPath.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
txtProcessPath.Location = new Point(40 + labelWidth, yPos);
txtProcessPath.Size = new Size(250, 24);
txtProcessPath.Enabled = config.StartMode == "process";
this.Controls.Add(txtProcessPath);
// 第三行:启动按钮文字 + 停止按钮文字
yPos += 30;
Label lblStart = new Label();
lblStart.Text = "启动文字:";
lblStart.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
lblStart.Location = new Point(40, yPos + 2);
lblStart.Size = new Size(labelWidth, 22);
lblStart.UseCompatibleTextRendering = true;
this.Controls.Add(lblStart);
txtStartBtn = new TextBox();
txtStartBtn.Text = config.StartBtnText;
txtStartBtn.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
txtStartBtn.Location = new Point(40 + labelWidth, yPos);
txtStartBtn.Size = new Size(90, 24);
this.Controls.Add(txtStartBtn);
Label lblStop = new Label();
lblStop.Text = "停止文字:";
lblStop.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
lblStop.Location = new Point(40 + labelWidth + 100, yPos + 2);
lblStop.Size = new Size(labelWidth, 22);
lblStop.UseCompatibleTextRendering = true;
this.Controls.Add(lblStop);
txtStopBtn = new TextBox();
txtStopBtn.Text = config.StopBtnText;
txtStopBtn.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
txtStopBtn.Location = new Point(40 + labelWidth + 100 + labelWidth, yPos);
txtStopBtn.Size = new Size(90, 24);
this.Controls.Add(txtStopBtn);
// 第四行:进程名 + 启动参数
yPos += 30;
Label lblProcName = new Label();
lblProcName.Text = "进程名:";
lblProcName.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
lblProcName.Location = new Point(40, yPos + 2);
lblProcName.Size = new Size(labelWidth, 22);
lblProcName.UseCompatibleTextRendering = true;
this.Controls.Add(lblProcName);
txtProcessName = new TextBox();
txtProcessName.Text = config.ProcessName;
txtProcessName.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
txtProcessName.Location = new Point(40 + labelWidth, yPos);
txtProcessName.Size = new Size(120, 24);
txtProcessName.Enabled = config.StartMode == "process";
this.Controls.Add(txtProcessName);
Label lblArgs = new Label();
lblArgs.Text = "启动参数:";
lblArgs.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
lblArgs.Location = new Point(40 + labelWidth + 130, yPos + 2);
lblArgs.Size = new Size(labelWidth - 10, 22);
lblArgs.UseCompatibleTextRendering = true;
this.Controls.Add(lblArgs);
txtProcessArgs = new TextBox();
txtProcessArgs.Text = config.ProcessArgs;
txtProcessArgs.Font = new Font("微软雅黑", 8.5f, GraphicsUnit.Point);
txtProcessArgs.Location = new Point(40 + labelWidth + 130 + labelWidth - 10, yPos);
txtProcessArgs.Size = new Size(140, 24);
txtProcessArgs.Enabled = config.StartMode == "process";
this.Controls.Add(txtProcessArgs);
// 删除按钮
btnDelete = new Button();
btnDelete.Text = "✕";
btnDelete.Font = new Font("Segoe UI", 10, FontStyle.Bold, GraphicsUnit.Point);
btnDelete.Location = new Point(this.Width - 38, 8);
btnDelete.Size = new Size(28, 28);
btnDelete.FlatStyle = FlatStyle.Flat;
btnDelete.FlatAppearance.BorderSize = 0;
btnDelete.BackColor = Color.FromArgb(231, 76, 60);
btnDelete.ForeColor = Color.White;
btnDelete.Cursor = Cursors.Hand;
btnDelete.Click += BtnDelete_Click;
this.Controls.Add(btnDelete);
this.Resize += (s, e) => {
btnDelete.Location = new Point(this.Width - 38, 8);
};
}
private GraphicsPath GetRoundedRectangle(Rectangle rect, int radius)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.X + rect.Width - radius, rect.Y + rect.Height - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Y + rect.Height - radius, radius, radius, 90, 90);
path.CloseFigure();
return path;
}
public ServiceConfig GetConfig()
{
ServiceConfig cfg = new ServiceConfig();
cfg.SectionName = config.SectionName;
cfg.ServiceName = txtServiceName.Text.Trim();
cfg.DisplayName = txtDisplayName.Text.Trim();
cfg.StartBtnText = string.IsNullOrWhiteSpace(txtStartBtn.Text) ? "启动" : txtStartBtn.Text.Trim();
cfg.StopBtnText = string.IsNullOrWhiteSpace(txtStopBtn.Text) ? "停止" : txtStopBtn.Text.Trim();
cfg.StartMode = cmbStartMode.Text;
cfg.ProcessPath = txtProcessPath.Text.Trim();
cfg.ProcessArgs = txtProcessArgs.Text.Trim();
cfg.ProcessName = txtProcessName.Text.Trim();
return cfg;
}
private void BtnDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show("确定要删除此服务配置吗?", "确认删除",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
this.Parent.Controls.Remove(this);
}
}
}
}
2.3 为控制面板添加图标
在编译时添加
1
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /target:winexe /win32icon:app.ico /reference:System.Windows.Forms.dll /reference:System.ServiceProcess.dll EnvManager.cs