从零到成品——用AI辅助造程序的完整操作流程

本文将逐步详解在现在(2026年)这个时间点上,如何用网上免费的AI大模型以及VS编译器从零开始造一个Windows桌面端应用程序,所需成本和技术几乎都可以忽略不计,适合所有人使用

前言:它有什么用?

可在不会任何编程语言的情况下,自己造出自己的Windows桌面端窗体应用程序。我没有使用“AI写代码”或者“氛围编程”这样常见的描述,而是强调“造出来”,因为对于一般人来说,光写出代码是没有用的,必须把它变成我们熟悉的可执行程序才能够正常使用。故夸张点说,本文将是一个如何实现从“零”到“产品”的完整操作手册,因篇幅有限,将很大程度上忽略掉一些不是那么重要的原理的讨论和技术细节上的解释说明,而重点强调执行步骤和操作流程。

我们将以制作一个“倒计时闹钟”程序为例来详解这个过程。该程序较为简单,且具有一定的实用价值。

准备工作

本次将使用免费的AI大模型Deepseek完成代码的编写,Deepseek的使用较为简单,我们只需要打开www.deepseek.com/ ,即可进入Deepseek官网,登录账号进入对话界面后即可使用,对话只需要使用“快速模式”,不需要使用“专家模式”(这可能会在未来变成收费项目);“深度思考”酌情开启,我们本次均不开启。AI的使用相当无脑简单,一句话说,就是写让人能看懂的汉语小作文,要点是必须提到关键性词语,例如用何种计算机语言、何种框架来实现,就必须明确提到这个语言和框架的准确词语。语句要使用书面语,避免口语化,尽最大可能不出现歧义。

本次使用Microsoft Visual Studio(以下简称VS)编译代码生成程序。语言使用C#,用Winform框架实现窗体应用程序。这里我们将首先介绍VS的安装和基本使用。

进入https://visualstudio.microsoft.com/ ,下载Visual Studio 2026免费版(即Community版),获得该程序的安装包,安装时,在“工作负荷”中勾选【.NET桌面开发】即可,在“安装位置”中,建议设置如下:如我这里是首先在D盘的新建了Visual Studio 2026文件夹,在这个文件夹内又新建了Community文件夹、Packages文件夹、Shared三个文件夹。然后在安装程序中,将“产品”路径指定为Community文件夹,将“下载缓存”路径指定为Packages文件夹,将“共享组件、工具和SDK”路径指定为Shared文件夹。但要说明的是,即使是这样,该程序仍有一部分内容被强制安装在C盘里,所以需要在“工作负荷”页面确认是否有足够的空间,特别是系统驱动器(C)的空间是否足够。我这里因为已经安装过了,所以不会显示需要多少G的容量。

安装完VS以后,启动它。我写这段的时候是在旧设备上写的,这个设备安装的是VS2022,所以这里我将以VS2022的界面来演示,基本上是完全相同的。

首先点击【创建新项目】,然后会看到很多项目模板,这里我们选择【Windows窗体应用(.NET Framework)】这一选项,如果找不到,在上方搜索框搜索Winform即可找到。然后点击下一步。

在这个界面按照下图内容操作即可。主要是指定项目名称和项目存放路径,这里我是在D盘根目录新建了一个“NewForm”的文件夹存放这些程序的项目,当然你可以自己指定一个目录。因为我们第一个程序叫做“倒计时闹钟”,故这里的项目名称就叫做CountDownAlarm。点击创建。

点击创建后,将打开这样的界面。

如果你打开的界面和这个长得不太一样甚至区别很大,都没有关系,只需要注意到屏幕右侧的“解决方案资源管理器”,其中有一个名为“CountDownAlarm”的项目,这个就叫做“解决方案”,在VS中这代表整个程序项目。应当注意到这个“CountDownAlarm”的项目之下有三个文件,即:

Form1.cs
Form1.Designer.cs
Program.cs

默认情况下,Form1.Designer.cs藏在Form1.cs里面,需要点击Form1.cs左侧的箭头才能显示出来。三个文件都是C#语言的代码文件,在这里,我们可以尝试查看这三个文件的代码。查看Form1.cs的代码需要先右键Form1.cs,然后点击“查看代码”,即可在VS的主界面窗口看到Form1.cs的代码。它的内容应该是这样:

然后我们可以左键单击解决方案资源管理器中的Form1.Designer.cs,即可看到它的内容,应该是这样:

左键单击Program.cs,应该是这样:

通过点击VS主窗口上方的页面标签,应当可以在这些页面之间切换:

知道了这些后我们就可以关闭VS了。这里简单介绍一下这些文件都是控制什么的:Form1.cs里是程序默认的窗体1的业务逻辑代码,通俗点说就是实现功能的代码;Form1.Designer.cs是窗体1的元素设计器,控制Form1里的各种控件的设计、布局等;Program.cs是程序的入口,也就是Main函数,是程序运行的起点。Winform中这三类代码通常是分开写在这三个文件中的,当然因为C#本身是一门面向对象编程的语言,故也可以全写到一个文件中。

我们这里再打开D盘根目录下的NewForm文件夹,查看一下这些文件是怎么存储在硬盘上的。可以看到,文件的目录结构和我们在VS中观察到的类似,也能看到这三个文件(绿框中的三个文件),红框中的文件名为CountDownAlarm.sln,此为解决方案文件,可以尝试双击这个文件,如果提示选择打开方式的话就选择我们刚装的Visual Studio应用程序,应该是能够直接打开该项目的。当然我们也可以先打开VS→打开项目或解决方案,再找到CountDownAlarm.sln的位置,也可以重新打开该项目。

到这里我们应该已经熟悉VS的基本使用了。接下来我们让AI编写代码,再将代码粘贴进VS对应的位置,即可编译生成程序。

示例程序:倒计时闹钟

现在我们来造这个程序。这个程序的目的是为了实现倒计时提醒的功能,可以设想一个使用场景:例如我现在在电脑上看电视剧(很入迷),但是打算在3分钟后准时喝一口水,那么为了在三分钟后准时喝到水,就必须有一个闹钟来打断我被电视剧吸引住的状态。故我们可以用一个倒计时提醒的软件来实现这个功能。我们可以设想一下该软件的使用细节,并让AI生成代码,小作文可以如下:

给我用C#和Winform写一个倒计时闹钟程序,这个程序应当有一个明显的时钟界面,用于显示倒计时,形如00:00:00,三个00分别代表小时、分钟、秒。在倒计时界面的下方,应当有一个用户可以输入指定时间的位置,用户应当可以指定小时、分钟、秒这三种数字的值,作为倒计时的总时间。当用户指定时间的数值后,应当有一个名为“启动”的按钮可以被使用,当用户点击这个按钮后,程序将以这个时间开始倒计时。应当还有一个“暂停”按钮,当用户在倒计时过程中按下这个按钮后,倒计时将暂停,暂停状态下再次点击暂停按钮将不做出任何动作;暂停状态下,如果用户点击“启动”按钮,将恢复并继续该倒计时。应当还有一个“停止”按钮,无论用户任何时候按下这个按钮,程序都将被重置到初始状态。倒计时结束后,应当立即有一个响亮的闹铃声音效果被播放,该铃声将一直被循环播放,直到用户点击“停止”按钮或者关闭程序。同时,在倒计时结束时,该程序在Windows任务栏上的图标应当不断闪烁,直到用户点击“停止”按钮或者关闭程序。给我写出完整的代码,并把这个项目分别写成Form1.cs、Form1.Designer.cs、Program.cs。给出每一个文件的完整代码

(注:以上内容中加粗的是最核心的语句,是必须要有的)

我们将其输入进Deepseek的对话界面,等待一段时间后,得到以下效果:

点击他代码框右上角的“复制”按钮,即可复制整块代码,我们需要将三块代码(Form1.cs、Form1.Designer.cs、Program.cs)分别复制进VS里的对应的文件里,覆盖掉原有的默认代码,注意一定要对号入座,不要复制错了。

以下是这次对话后,三个代码的实际情况:

点此展开Form1.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
using System;
using System.Drawing;
using System.Media;
using System.Windows.Forms;

namespace CountdownTimer
{
public partial class Form1 : Form
{
private Timer countdownTimer;
private TimeSpan remainingTime;
private bool isRunning = false;
private bool isTimeUp = false;
private SoundPlayer alarmPlayer;
private bool isFlashing = false;
private Timer flashTimer;
private Icon originalIcon;
private Icon flashingIcon;

public Form1()
{
InitializeComponent();
InitializeCustomComponents();
}

private void InitializeCustomComponents()
{
countdownTimer = new Timer();
countdownTimer.Interval = 1000; // 1秒间隔
countdownTimer.Tick += CountdownTimer_Tick;

flashTimer = new Timer();
flashTimer.Interval = 500; // 0.5秒闪烁一次
flashTimer.Tick += FlashTimer_Tick;

// 设置闪烁图标(可以使用系统默认图标,这里创建一个简单的图标效果)
originalIcon = this.Icon;

// 创建一个简单的红色图标用于闪烁(实际项目中可以使用自定义图标)
flashingIcon = SystemIcons.Warning;
}

private void CountdownTimer_Tick(object sender, EventArgs e)
{
if (isRunning && remainingTime.TotalSeconds > 0)
{
remainingTime = remainingTime.Subtract(TimeSpan.FromSeconds(1));
UpdateDisplay();

if (remainingTime.TotalSeconds <= 0)
{
StopCountdown();
OnTimeUp();
}
}
}

private void UpdateDisplay()
{
lblTimeDisplay.Text = string.Format("{0:D2}:{1:D2}:{2:D2}",
remainingTime.Hours,
remainingTime.Minutes,
remainingTime.Seconds);

// 当剩余时间少于10秒时,改变颜色提醒
if (remainingTime.TotalSeconds <= 10 && remainingTime.TotalSeconds > 0)
{
lblTimeDisplay.ForeColor = Color.Red;
lblTimeDisplay.Font = new Font(lblTimeDisplay.Font, FontStyle.Bold);
}
else
{
lblTimeDisplay.ForeColor = Color.Black;
lblTimeDisplay.Font = new Font(lblTimeDisplay.Font, FontStyle.Regular);
}
}

private void OnTimeUp()
{
isTimeUp = true;
isRunning = false;

// 播放闹铃
PlayAlarm();

// 开始闪烁任务栏图标
StartFlashing();

// 显示消息框
MessageBox.Show("倒计时结束!", "时间到", MessageBoxButtons.OK, MessageBoxIcon.Information);

// 更新按钮状态
btnStart.Enabled = false;
btnPause.Enabled = false;
btnStop.Enabled = true;
}

private void PlayAlarm()
{
try
{
// 创建简单的蜂鸣声作为闹铃(循环播放)
alarmPlayer = new SoundPlayer();

// 使用系统内置的蜂鸣声,并设置为循环播放
// 注意:这种方法比较简单,如果需要更响亮的铃声,可以使用自定义的wav文件
string alarmSoundPath = System.IO.Path.GetTempFileName() + ".wav";
GenerateBeepSound(alarmSoundPath);
alarmPlayer.SoundLocation = alarmSoundPath;
alarmPlayer.Load();
alarmPlayer.PlayLooping();
}
catch (Exception ex)
{
// 如果无法播放声音文件,使用系统蜂鸣声
Console.WriteLine($"播放闹铃失败: {ex.Message}");
// 每2秒发出一次蜂鸣声
Timer beepTimer = new Timer();
beepTimer.Interval = 2000;
beepTimer.Tick += (s, e) => { Console.Beep(1000, 500); };
beepTimer.Start();
}
}

private void GenerateBeepSound(string filePath)
{
// 生成一个简单的WAV文件(400Hz频率,持续0.5秒)
// 这里简化处理,实际上可以生成更复杂的铃声
try
{
using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
// 简单的WAV文件头
byte[] wavHeader = new byte[44];
// 这里简化WAV文件生成,实际应用中可以使用更专业的音频库
// 或者直接使用系统声音
}
}
catch
{
// 如果生成失败,忽略
}
}

private void StartFlashing()
{
isFlashing = true;
flashTimer.Start();
}

private void StopFlashing()
{
isFlashing = false;
flashTimer.Stop();
this.Icon = originalIcon;
}

private void FlashTimer_Tick(object sender, EventArgs e)
{
if (isFlashing)
{
// 切换任务栏图标来产生闪烁效果
this.Icon = this.Icon == originalIcon ? flashingIcon : originalIcon;
}
}

private void btnStart_Click(object sender, EventArgs e)
{
if (isTimeUp)
{
// 如果倒计时已结束,重置后再启动
ResetToInitialState();
}

if (!isRunning)
{
// 如果是暂停状态恢复运行,或者是新的倒计时
if (remainingTime.TotalSeconds <= 0)
{
// 获取用户输入的时间
int hours = (int)nudHours.Value;
int minutes = (int)nudMinutes.Value;
int seconds = (int)nudSeconds.Value;

remainingTime = new TimeSpan(hours, minutes, seconds);

if (remainingTime.TotalSeconds <= 0)
{
MessageBox.Show("请设置一个有效的时间!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

UpdateDisplay();
}

isRunning = true;
countdownTimer.Start();

// 更新按钮状态
btnStart.Enabled = false;
btnPause.Enabled = true;
btnStop.Enabled = true;
}
}

private void btnPause_Click(object sender, EventArgs e)
{
if (isRunning)
{
isRunning = false;
countdownTimer.Stop();

// 更新按钮状态
btnStart.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = true;
}
}

private void btnStop_Click(object sender, EventArgs e)
{
ResetToInitialState();
}

private void ResetToInitialState()
{
// 停止计时器
isRunning = false;
countdownTimer.Stop();

// 停止闹铃
if (alarmPlayer != null)
{
alarmPlayer.Stop();
alarmPlayer.Dispose();
alarmPlayer = null;
}

// 停止闪烁
StopFlashing();

// 重置标志
isTimeUp = false;

// 重置显示
remainingTime = TimeSpan.Zero;
lblTimeDisplay.Text = "00:00:00";
lblTimeDisplay.ForeColor = Color.Black;
lblTimeDisplay.Font = new Font(lblTimeDisplay.Font, FontStyle.Regular);

// 重置输入控件
nudHours.Value = 0;
nudMinutes.Value = 0;
nudSeconds.Value = 0;

// 更新按钮状态
btnStart.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = false;
}

private void nudHours_ValueChanged(object sender, EventArgs e)
{
if (!isRunning && !isTimeUp)
{
UpdateTimeFromInput();
}
}

private void nudMinutes_ValueChanged(object sender, EventArgs e)
{
if (!isRunning && !isTimeUp)
{
UpdateTimeFromInput();
}
}

private void nudSeconds_ValueChanged(object sender, EventArgs e)
{
if (!isRunning && !isTimeUp)
{
UpdateTimeFromInput();
}
}

private void UpdateTimeFromInput()
{
int hours = (int)nudHours.Value;
int minutes = (int)nudMinutes.Value;
int seconds = (int)nudSeconds.Value;

remainingTime = new TimeSpan(hours, minutes, seconds);
UpdateDisplay();
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
// 清理资源
if (alarmPlayer != null)
{
alarmPlayer.Stop();
alarmPlayer.Dispose();
}

countdownTimer.Stop();
countdownTimer.Dispose();

flashTimer.Stop();
flashTimer.Dispose();

base.OnFormClosing(e);
}
}
}

点此展开Form.Designer.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
namespace CountdownTimer
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Label lblTimeDisplay;
private System.Windows.Forms.NumericUpDown nudHours;
private System.Windows.Forms.NumericUpDown nudMinutes;
private System.Windows.Forms.NumericUpDown nudSeconds;
private System.Windows.Forms.Label lblHours;
private System.Windows.Forms.Label lblMinutes;
private System.Windows.Forms.Label lblSeconds;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnPause;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Panel panelTimeInput;

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

private void InitializeComponent()
{
this.lblTimeDisplay = new System.Windows.Forms.Label();
this.panelTimeInput = new System.Windows.Forms.Panel();
this.nudHours = new System.Windows.Forms.NumericUpDown();
this.nudMinutes = new System.Windows.Forms.NumericUpDown();
this.nudSeconds = new System.Windows.Forms.NumericUpDown();
this.lblHours = new System.Windows.Forms.Label();
this.lblMinutes = new System.Windows.Forms.Label();
this.lblSeconds = new System.Windows.Forms.Label();
this.btnStart = new System.Windows.Forms.Button();
this.btnPause = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.panelTimeInput.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.nudHours)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudMinutes)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.nudSeconds)).BeginInit();
this.SuspendLayout();
//
// lblTimeDisplay
//
this.lblTimeDisplay.Font = new System.Drawing.Font("微软雅黑", 48F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblTimeDisplay.Location = new System.Drawing.Point(12, 20);
this.lblTimeDisplay.Name = "lblTimeDisplay";
this.lblTimeDisplay.Size = new System.Drawing.Size(460, 100);
this.lblTimeDisplay.TabIndex = 0;
this.lblTimeDisplay.Text = "00:00:00";
this.lblTimeDisplay.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// panelTimeInput
//
this.panelTimeInput.Controls.Add(this.nudHours);
this.panelTimeInput.Controls.Add(this.nudMinutes);
this.panelTimeInput.Controls.Add(this.nudSeconds);
this.panelTimeInput.Controls.Add(this.lblHours);
this.panelTimeInput.Controls.Add(this.lblMinutes);
this.panelTimeInput.Controls.Add(this.lblSeconds);
this.panelTimeInput.Location = new System.Drawing.Point(12, 140);
this.panelTimeInput.Name = "panelTimeInput";
this.panelTimeInput.Size = new System.Drawing.Size(460, 80);
this.panelTimeInput.TabIndex = 1;
//
// nudHours
//
this.nudHours.Font = new System.Drawing.Font("微软雅黑", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.nudHours.Location = new System.Drawing.Point(30, 25);
this.nudHours.Maximum = new decimal(new int[] {
99,
0,
0,
0});
this.nudHours.Name = "nudHours";
this.nudHours.Size = new System.Drawing.Size(80, 34);
this.nudHours.TabIndex = 0;
this.nudHours.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.nudHours.ValueChanged += new System.EventHandler(this.nudHours_ValueChanged);
//
// nudMinutes
//
this.nudMinutes.Font = new System.Drawing.Font("微软雅黑", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.nudMinutes.Location = new System.Drawing.Point(190, 25);
this.nudMinutes.Maximum = new decimal(new int[] {
59,
0,
0,
0});
this.nudMinutes.Name = "nudMinutes";
this.nudMinutes.Size = new System.Drawing.Size(80, 34);
this.nudMinutes.TabIndex = 1;
this.nudMinutes.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.nudMinutes.ValueChanged += new System.EventHandler(this.nudMinutes_ValueChanged);
//
// nudSeconds
//
this.nudSeconds.Font = new System.Drawing.Font("微软雅黑", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.nudSeconds.Location = new System.Drawing.Point(350, 25);
this.nudSeconds.Maximum = new decimal(new int[] {
59,
0,
0,
0});
this.nudSeconds.Name = "nudSeconds";
this.nudSeconds.Size = new System.Drawing.Size(80, 34);
this.nudSeconds.TabIndex = 2;
this.nudSeconds.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.nudSeconds.ValueChanged += new System.EventHandler(this.nudSeconds_ValueChanged);
//
// lblHours
//
this.lblHours.AutoSize = true;
this.lblHours.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblHours.Location = new System.Drawing.Point(40, 60);
this.lblHours.Name = "lblHours";
this.lblHours.Size = new System.Drawing.Size(58, 21);
this.lblHours.TabIndex = 3;
this.lblHours.Text = "小时";
//
// lblMinutes
//
this.lblMinutes.AutoSize = true;
this.lblMinutes.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblMinutes.Location = new System.Drawing.Point(200, 60);
this.lblMinutes.Name = "lblMinutes";
this.lblMinutes.Size = new System.Drawing.Size(58, 21);
this.lblMinutes.TabIndex = 4;
this.lblMinutes.Text = "分钟";
//
// lblSeconds
//
this.lblSeconds.AutoSize = true;
this.lblSeconds.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblSeconds.Location = new System.Drawing.Point(360, 60);
this.lblSeconds.Name = "lblSeconds";
this.lblSeconds.Size = new System.Drawing.Size(58, 21);
this.lblSeconds.TabIndex = 5;
this.lblSeconds.Text = "秒";
//
// btnStart
//
this.btnStart.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnStart.Location = new System.Drawing.Point(50, 240);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(100, 40);
this.btnStart.TabIndex = 2;
this.btnStart.Text = "启动";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnPause
//
this.btnPause.Enabled = false;
this.btnPause.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnPause.Location = new System.Drawing.Point(185, 240);
this.btnPause.Name = "btnPause";
this.btnPause.Size = new System.Drawing.Size(100, 40);
this.btnPause.TabIndex = 3;
this.btnPause.Text = "暂停";
this.btnPause.UseVisualStyleBackColor = true;
this.btnPause.Click += new System.EventHandler(this.btnPause_Click);
//
// btnStop
//
this.btnStop.Enabled = false;
this.btnStop.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnStop.Location = new System.Drawing.Point(320, 240);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(100, 40);
this.btnStop.TabIndex = 4;
this.btnStop.Text = "停止";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(484, 311);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnPause);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.panelTimeInput);
this.Controls.Add(this.lblTimeDisplay);
this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "倒计时闹钟";
this.panelTimeInput.ResumeLayout(false);
this.panelTimeInput.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.nudHours)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudMinutes)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.nudSeconds)).EndInit();
this.ResumeLayout(false);
}
}
}

点此展开Program.cs代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Windows.Forms;

namespace CountdownTimer
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

全部复制进去后,我们发现Form1.cs中,有一行提示报错了,在VS的下方的代码检查器中就能看到这个错误。原因是AI犯蠢了,它写的代码中没有给出StopCountdown()这个函数的定义:

于是我们再次发问。为了使生成更快,这里只需要重写Form1.cs的代码:

上下文并不存在StopCountdown()这个方法,导致无法编译,请修改代码,并再次给出完整的Form1.cs代码。

本次对话后的Form1.cs代码如下:

点此展开Form1.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
using System;
using System.Drawing;
using System.Media;
using System.Windows.Forms;

namespace CountdownTimer
{
public partial class Form1 : Form
{
private Timer countdownTimer;
private TimeSpan remainingTime;
private bool isRunning = false;
private bool isTimeUp = false;
private SoundPlayer alarmPlayer;
private bool isFlashing = false;
private Timer flashTimer;
private Icon originalIcon;
private Icon flashingIcon;

public Form1()
{
InitializeComponent();
InitializeCustomComponents();
}

private void InitializeCustomComponents()
{
countdownTimer = new Timer();
countdownTimer.Interval = 1000; // 1秒间隔
countdownTimer.Tick += CountdownTimer_Tick;

flashTimer = new Timer();
flashTimer.Interval = 500; // 0.5秒闪烁一次
flashTimer.Tick += FlashTimer_Tick;

// 设置闪烁图标
originalIcon = this.Icon;

// 创建一个简单的警告图标用于闪烁
flashingIcon = SystemIcons.Warning;
}

private void CountdownTimer_Tick(object sender, EventArgs e)
{
if (isRunning && remainingTime.TotalSeconds > 0)
{
remainingTime = remainingTime.Subtract(TimeSpan.FromSeconds(1));
UpdateDisplay();

if (remainingTime.TotalSeconds <= 0)
{
StopCountdown();
OnTimeUp();
}
}
}

private void StopCountdown()
{
isRunning = false;
countdownTimer.Stop();
}

private void UpdateDisplay()
{
lblTimeDisplay.Text = string.Format("{0:D2}:{1:D2}:{2:D2}",
remainingTime.Hours,
remainingTime.Minutes,
remainingTime.Seconds);

// 当剩余时间少于10秒时,改变颜色提醒
if (remainingTime.TotalSeconds <= 10 && remainingTime.TotalSeconds > 0)
{
lblTimeDisplay.ForeColor = Color.Red;
lblTimeDisplay.Font = new Font(lblTimeDisplay.Font, FontStyle.Bold);
}
else
{
lblTimeDisplay.ForeColor = Color.Black;
lblTimeDisplay.Font = new Font(lblTimeDisplay.Font, FontStyle.Regular);
}
}

private void OnTimeUp()
{
isTimeUp = true;
isRunning = false;

// 播放闹铃
PlayAlarm();

// 开始闪烁任务栏图标
StartFlashing();

// 显示消息框
MessageBox.Show("倒计时结束!", "时间到", MessageBoxButtons.OK, MessageBoxIcon.Information);

// 更新按钮状态
btnStart.Enabled = false;
btnPause.Enabled = false;
btnStop.Enabled = true;
}

private void PlayAlarm()
{
try
{
// 创建简单的蜂鸣声作为闹铃(循环播放)
alarmPlayer = new SoundPlayer();

// 使用系统内置的蜂鸣声
string alarmSoundPath = System.IO.Path.GetTempFileName() + ".wav";
GenerateBeepSound(alarmSoundPath);
alarmPlayer.SoundLocation = alarmSoundPath;
alarmPlayer.Load();
alarmPlayer.PlayLooping();
}
catch (Exception ex)
{
// 如果无法播放声音文件,使用系统蜂鸣声
Console.WriteLine($"播放闹铃失败: {ex.Message}");
// 每2秒发出一次蜂鸣声
Timer beepTimer = new Timer();
beepTimer.Interval = 2000;
beepTimer.Tick += (s, e) => { Console.Beep(1000, 500); };
beepTimer.Start();
}
}

private void GenerateBeepSound(string filePath)
{
// 生成一个简单的WAV文件
try
{
using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
// 简单的WAV文件头
byte[] wavHeader = new byte[44];
// 这里简化WAV文件生成,实际应用中可以使用更专业的音频库
// 或者直接使用系统声音
}
}
catch
{
// 如果生成失败,忽略
}
}

private void StartFlashing()
{
isFlashing = true;
flashTimer.Start();
}

private void StopFlashing()
{
isFlashing = false;
flashTimer.Stop();
this.Icon = originalIcon;
}

private void FlashTimer_Tick(object sender, EventArgs e)
{
if (isFlashing)
{
// 切换任务栏图标来产生闪烁效果
this.Icon = this.Icon == originalIcon ? flashingIcon : originalIcon;
}
}

private void btnStart_Click(object sender, EventArgs e)
{
if (isTimeUp)
{
// 如果倒计时已结束,重置后再启动
ResetToInitialState();
}

if (!isRunning)
{
// 如果是暂停状态恢复运行,或者是新的倒计时
if (remainingTime.TotalSeconds <= 0)
{
// 获取用户输入的时间
int hours = (int)nudHours.Value;
int minutes = (int)nudMinutes.Value;
int seconds = (int)nudSeconds.Value;

remainingTime = new TimeSpan(hours, minutes, seconds);

if (remainingTime.TotalSeconds <= 0)
{
MessageBox.Show("请设置一个有效的时间!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

UpdateDisplay();
}

isRunning = true;
countdownTimer.Start();

// 更新按钮状态
btnStart.Enabled = false;
btnPause.Enabled = true;
btnStop.Enabled = true;
}
}

private void btnPause_Click(object sender, EventArgs e)
{
if (isRunning)
{
isRunning = false;
countdownTimer.Stop();

// 更新按钮状态
btnStart.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = true;
}
}

private void btnStop_Click(object sender, EventArgs e)
{
ResetToInitialState();
}

private void ResetToInitialState()
{
// 停止计时器
isRunning = false;
countdownTimer.Stop();

// 停止闹铃
if (alarmPlayer != null)
{
alarmPlayer.Stop();
alarmPlayer.Dispose();
alarmPlayer = null;
}

// 停止闪烁
StopFlashing();

// 重置标志
isTimeUp = false;

// 重置显示
remainingTime = TimeSpan.Zero;
lblTimeDisplay.Text = "00:00:00";
lblTimeDisplay.ForeColor = Color.Black;
lblTimeDisplay.Font = new Font(lblTimeDisplay.Font, FontStyle.Regular);

// 重置输入控件
nudHours.Value = 0;
nudMinutes.Value = 0;
nudSeconds.Value = 0;

// 更新按钮状态
btnStart.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = false;
}

private void nudHours_ValueChanged(object sender, EventArgs e)
{
if (!isRunning && !isTimeUp)
{
UpdateTimeFromInput();
}
}

private void nudMinutes_ValueChanged(object sender, EventArgs e)
{
if (!isRunning && !isTimeUp)
{
UpdateTimeFromInput();
}
}

private void nudSeconds_ValueChanged(object sender, EventArgs e)
{
if (!isRunning && !isTimeUp)
{
UpdateTimeFromInput();
}
}

private void UpdateTimeFromInput()
{
int hours = (int)nudHours.Value;
int minutes = (int)nudMinutes.Value;
int seconds = (int)nudSeconds.Value;

remainingTime = new TimeSpan(hours, minutes, seconds);
UpdateDisplay();
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
// 清理资源
if (alarmPlayer != null)
{
alarmPlayer.Stop();
alarmPlayer.Dispose();
}

if (countdownTimer != null)
{
countdownTimer.Stop();
countdownTimer.Dispose();
}

if (flashTimer != null)
{
flashTimer.Stop();
flashTimer.Dispose();
}

base.OnFormClosing(e);
}
}
}

将其复制后再次覆盖到Form1.cs,检查下方的检查器,这下没有报错了,于是我们点击上方的“启动”,开始调试程序:

启动后,我们可以看到已经做出了一个差不多的倒计时程序,这里可以试一下它的功能是否正常,如我们先设置10秒,然后启动倒计时,再去打开一个视频什么的开始假装玩,等到10秒后,该程序发出了控制台的蜂鸣器音效,再玩玩这个暂停和停止按钮,也没有什么问题,可以说基本功能已经实现。

不过,仍然发现了一些需要改进的问题,如这个蜂鸣器的音效不够吵闹,应当让其一直无间断地播放;再就是这个蜂鸣器循环音效在用户按下停止后仍然在继续播放,这是需要修正的。于是再次发问:

蜂鸣器闹铃有问题,在用户按下停止按钮后程序仍然在播放闹铃音效,用户按下停止按钮后,闹铃音效必须停止。另外,一旦触发闹铃,蜂鸣器的音效不能是脉冲式的,必须是长音一直响。请修改代码,并再次给出完整的Form1.cs代码。

将其复制后再次覆盖到Form1.cs。这次,这个蜂鸣器闹铃足够响亮,甚至可以说十分惊悚,足以打断你对任何事物的沉迷了。而且停止按钮可以正常解除闹铃音效。再测试一下分钟级别的时间,也是可以正常运作。到这一步,程序基本就可以正常运行了。

本次对话后的Form1.cs代码如下:

点此展开Form1.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
using System;
using System.Drawing;
using System.Media;
using System.Windows.Forms;

namespace CountdownTimer
{
public partial class Form1 : Form
{
private Timer countdownTimer;
private TimeSpan remainingTime;
private bool isRunning = false;
private bool isTimeUp = false;
private SoundPlayer alarmPlayer;
private bool isFlashing = false;
private Timer flashTimer;
private Icon originalIcon;
private Icon flashingIcon;
private Timer beepTimer; // 用于系统蜂鸣声的计时器

public Form1()
{
InitializeComponent();
InitializeCustomComponents();
}

private void InitializeCustomComponents()
{
countdownTimer = new Timer();
countdownTimer.Interval = 1000; // 1秒间隔
countdownTimer.Tick += CountdownTimer_Tick;

flashTimer = new Timer();
flashTimer.Interval = 500; // 0.5秒闪烁一次
flashTimer.Tick += FlashTimer_Tick;

// 设置闪烁图标
originalIcon = this.Icon;

// 创建一个简单的警告图标用于闪烁
flashingIcon = SystemIcons.Warning;
}

private void CountdownTimer_Tick(object sender, EventArgs e)
{
if (isRunning && remainingTime.TotalSeconds > 0)
{
remainingTime = remainingTime.Subtract(TimeSpan.FromSeconds(1));
UpdateDisplay();

if (remainingTime.TotalSeconds <= 0)
{
StopCountdown();
OnTimeUp();
}
}
}

private void StopCountdown()
{
isRunning = false;
countdownTimer.Stop();
}

private void UpdateDisplay()
{
lblTimeDisplay.Text = string.Format("{0:D2}:{1:D2}:{2:D2}",
remainingTime.Hours,
remainingTime.Minutes,
remainingTime.Seconds);

// 当剩余时间少于10秒时,改变颜色提醒
if (remainingTime.TotalSeconds <= 10 && remainingTime.TotalSeconds > 0)
{
lblTimeDisplay.ForeColor = Color.Red;
lblTimeDisplay.Font = new Font(lblTimeDisplay.Font, FontStyle.Bold);
}
else
{
lblTimeDisplay.ForeColor = Color.Black;
lblTimeDisplay.Font = new Font(lblTimeDisplay.Font, FontStyle.Regular);
}
}

private void OnTimeUp()
{
isTimeUp = true;
isRunning = false;

// 播放闹铃(长音)
PlayAlarm();

// 开始闪烁任务栏图标
StartFlashing();

// 显示消息框
MessageBox.Show("倒计时结束!", "时间到", MessageBoxButtons.OK, MessageBoxIcon.Information);

// 更新按钮状态
btnStart.Enabled = false;
btnPause.Enabled = false;
btnStop.Enabled = true;
}

private void PlayAlarm()
{
try
{
// 停止任何现有的闹铃
StopAlarm();

// 使用系统内置的蜂鸣声,持续长鸣
// 方法1: 使用 Console.Beep 在后台线程中持续播放
beepTimer = new Timer();
beepTimer.Interval = 100; // 每100毫秒检查一次
beepTimer.Tick += (s, e) =>
{
if (isTimeUp && !isRunning)
{
// 持续发出蜂鸣声(长音效果)
Console.Beep(1000, 500);
}
};
beepTimer.Start();

// 方法2: 尝试使用 SoundPlayer 播放自定义的持续声音
try
{
alarmPlayer = new SoundPlayer();
string alarmSoundPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "alarm.wav");
GenerateContinuousBeepSound(alarmSoundPath);
if (System.IO.File.Exists(alarmSoundPath))
{
alarmPlayer.SoundLocation = alarmSoundPath;
alarmPlayer.Load();
alarmPlayer.PlayLooping();
// 如果成功播放WAV文件,就停止蜂鸣器计时器
if (beepTimer != null)
{
beepTimer.Stop();
beepTimer.Dispose();
beepTimer = null;
}
}
}
catch
{
// 如果WAV播放失败,继续使用蜂鸣器
}
}
catch (Exception ex)
{
Console.WriteLine($"播放闹铃失败: {ex.Message}");
}
}

private void GenerateContinuousBeepSound(string filePath)
{
try
{
// 创建一个持续5秒的蜂鸣声WAV文件
int sampleRate = 44100;
int frequency = 1000;
int duration = 5000; // 5秒持续时间
int amplitude = 32760;

double samples = (double)sampleRate * duration / 1000.0;
byte[] buffer = new byte[(int)samples * 2]; // 16位音频

for (int i = 0; i < samples; i++)
{
double time = (double)i / sampleRate;
short sample = (short)(amplitude * Math.Sin(2 * Math.PI * frequency * time));
byte[] bytes = BitConverter.GetBytes(sample);
buffer[i * 2] = bytes[0];
buffer[i * 2 + 1] = bytes[1];
}

// 写入WAV文件头
using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
{
// RIFF头
fs.Write(System.Text.Encoding.ASCII.GetBytes("RIFF"), 0, 4);
fs.Write(BitConverter.GetBytes(36 + buffer.Length), 0, 4);
fs.Write(System.Text.Encoding.ASCII.GetBytes("WAVE"), 0, 4);

// fmt块
fs.Write(System.Text.Encoding.ASCII.GetBytes("fmt "), 0, 4);
fs.Write(BitConverter.GetBytes(16), 0, 4); // 块大小
fs.Write(BitConverter.GetBytes((short)1), 0, 2); // 音频格式(PCM)
fs.Write(BitConverter.GetBytes((short)1), 0, 2); // 声道数
fs.Write(BitConverter.GetBytes(sampleRate), 0, 4); // 采样率
fs.Write(BitConverter.GetBytes(sampleRate * 2), 0, 4); // 字节率
fs.Write(BitConverter.GetBytes((short)2), 0, 2); // 块对齐
fs.Write(BitConverter.GetBytes((short)16), 0, 2); // 位深度

// data块
fs.Write(System.Text.Encoding.ASCII.GetBytes("data"), 0, 4);
fs.Write(BitConverter.GetBytes(buffer.Length), 0, 4);
fs.Write(buffer, 0, buffer.Length);
}
}
catch
{
// 如果生成失败,忽略
}
}

private void StopAlarm()
{
// 停止WAV播放器
if (alarmPlayer != null)
{
alarmPlayer.Stop();
alarmPlayer.Dispose();
alarmPlayer = null;
}

// 停止蜂鸣器计时器
if (beepTimer != null)
{
beepTimer.Stop();
beepTimer.Dispose();
beepTimer = null;
}
}

private void StartFlashing()
{
isFlashing = true;
flashTimer.Start();
}

private void StopFlashing()
{
isFlashing = false;
flashTimer.Stop();
this.Icon = originalIcon;
}

private void FlashTimer_Tick(object sender, EventArgs e)
{
if (isFlashing)
{
// 切换任务栏图标来产生闪烁效果
this.Icon = this.Icon == originalIcon ? flashingIcon : originalIcon;
}
}

private void btnStart_Click(object sender, EventArgs e)
{
if (isTimeUp)
{
// 如果倒计时已结束,重置后再启动
ResetToInitialState();
}

if (!isRunning)
{
// 如果是暂停状态恢复运行,或者是新的倒计时
if (remainingTime.TotalSeconds <= 0)
{
// 获取用户输入的时间
int hours = (int)nudHours.Value;
int minutes = (int)nudMinutes.Value;
int seconds = (int)nudSeconds.Value;

remainingTime = new TimeSpan(hours, minutes, seconds);

if (remainingTime.TotalSeconds <= 0)
{
MessageBox.Show("请设置一个有效的时间!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

UpdateDisplay();
}

isRunning = true;
countdownTimer.Start();

// 更新按钮状态
btnStart.Enabled = false;
btnPause.Enabled = true;
btnStop.Enabled = true;
}
}

private void btnPause_Click(object sender, EventArgs e)
{
if (isRunning)
{
isRunning = false;
countdownTimer.Stop();

// 更新按钮状态
btnStart.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = true;
}
}

private void btnStop_Click(object sender, EventArgs e)
{
ResetToInitialState();
}

private void ResetToInitialState()
{
// 停止计时器
isRunning = false;
countdownTimer.Stop();

// 停止所有闹铃
StopAlarm();

// 停止闪烁
StopFlashing();

// 重置标志
isTimeUp = false;

// 重置显示
remainingTime = TimeSpan.Zero;
lblTimeDisplay.Text = "00:00:00";
lblTimeDisplay.ForeColor = Color.Black;
lblTimeDisplay.Font = new Font(lblTimeDisplay.Font, FontStyle.Regular);

// 重置输入控件
nudHours.Value = 0;
nudMinutes.Value = 0;
nudSeconds.Value = 0;

// 更新按钮状态
btnStart.Enabled = true;
btnPause.Enabled = false;
btnStop.Enabled = false;
}

private void nudHours_ValueChanged(object sender, EventArgs e)
{
if (!isRunning && !isTimeUp)
{
UpdateTimeFromInput();
}
}

private void nudMinutes_ValueChanged(object sender, EventArgs e)
{
if (!isRunning && !isTimeUp)
{
UpdateTimeFromInput();
}
}

private void nudSeconds_ValueChanged(object sender, EventArgs e)
{
if (!isRunning && !isTimeUp)
{
UpdateTimeFromInput();
}
}

private void UpdateTimeFromInput()
{
int hours = (int)nudHours.Value;
int minutes = (int)nudMinutes.Value;
int seconds = (int)nudSeconds.Value;

remainingTime = new TimeSpan(hours, minutes, seconds);
UpdateDisplay();
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
// 清理资源
StopAlarm();

if (countdownTimer != null)
{
countdownTimer.Stop();
countdownTimer.Dispose();
}

if (flashTimer != null)
{
flashTimer.Stop();
flashTimer.Dispose();
}

base.OnFormClosing(e);
}
}
}

只要我们在VS中用“启动”按钮编译过这个程序后,程序就将在解决方案的目录下生成这个最后一次正确编译的可执行程序文件。这个目录现在应当是D:\NewForm\CountDownAlarm\bin\Debug,在这个目录中我们能看到一个叫CountDownAlarm.exe的可执行程序,这就是造出来的程序了,我们可以将它复制到任何地方,理论上都是可以直接运行的。

如果我们这时再在VS中点击“Form1.cs[设计]”这个标签页(如果没有这个标签页,就在解决方案资源管理器中-Form1.cs-右键点击-查看设计器),可以看到原本啥都没有的Form1窗体其实已经变成了程序的样子。这其实就是Winform的图形化设计界面,左边那些茫茫多的属性栏就能控制每个元素的各种属性了,有布局、颜色、字体等等,当然这就是Winform相关的内容了,感兴趣的话可以自行研究。

总的来看,我们并不需要真正明白任何一行代码,就能造出这样的程序了。可以说成本几乎没有,只要有电脑有手都能做,但正因为门槛太低,我还是得提醒各位不要让你们的老板或领导知道了这套流程,否则哪天真让他们玩明白了,肯定有人就要被降本增效了。正确的做法是应当利用这些工具增加自己的空闲时间,原本两天才能干完的事情,现在一个小时就解决了,但对老板那可是一定要说需要至少一天半才能干完。剩下的时间嘛,多休息多玩,多陪陪家人,这些才是最重要的。

  • 版权声明: 本博客所有文章著作权归作者所有,禁止任何形式的转载。
  • Copyrights © 2019-2026 Caelica

请我喝杯咖啡吧~