1 //Program.cs
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Threading.Tasks;
6 using System.Windows.Forms;
7
8 namespace TimerExc01
9 {
10 static class Program
11 {
12 /// <summary>
13 /// 应用程序的主入口点。
14 /// </summary>
15 [STAThread]
16 static void Main()
17 {
18 Application.EnableVisualStyles();
19 Application.SetCompatibleTextRenderingDefault(false);
20 Application.Run(new Form1());
21 }
22 }
23 }
1 //Form1.cs
2 using System;
3 using System.Collections.Generic;
4 using System.ComponentModel;
5 using System.Data;
6 using System.Drawing;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11
12 namespace TimerExc01
13 {
14 public partial class Form1 : Form
15 {
16 private int time = 0;
17 private int FirstSecond = 0;//启动程序时的时间
18 private int FirstHour = 0;
19 private int FirstMinute = 0;
20
21 public Form1()
22 {
23 InitializeComponent();
24 }
25
26 private void tmrCountTime_Tick(object sender, EventArgs e)
27 {
28 time++;
29 int currentHour = time / 3600;
30 int currentMinu = (time % 3600) / 60;
31 int currentSecond = (time % 3600) % 60;
32 this.tbxTime.Text = currentHour + "时" + currentMinu + "分" + currentSecond + "秒";
33 }
34
35 private void btnStart_Click(object sender, EventArgs e)
36 {
37 this.tmrCountTime.Enabled = true;
38 }
39
40 private void btnStop_Click(object sender, EventArgs e)
41 {
42 time = 0;
43 this.tmrCountTime.Enabled = false;
44 }
45
46 #region 界面加载
47 private void Form1_Load(object sender, EventArgs e)
48 {
49 DateTime datetime = System.DateTime.Now;
50 this.numHour.Value = datetime.Hour;
51 this.numMinu.Value = datetime.Minute;
52 this.numSecond.Value = datetime.Second;
53 FirstSecond = datetime.Second;//记录下最初的秒数
54 FirstMinute = datetime.Minute;
55 FirstHour = datetime.Hour;
56 tmrCurrentTime.Enabled = true;
57 }
58 #endregion
59
60 #region 定时器更新当前系统时间
61 int countSecond = 0;
62 private void tmrCurrentTime_Tick(object sender, EventArgs e)
63 {
64 int currentMinu = 0; int currentHour = 0;
65 countSecond += 1;
66 //最初的秒加上定时器走过的秒
67 int PastSumAllSecond = FirstSecond + countSecond;
68
69 //求小时数
70 int hour = PastSumAllSecond / 3600;
71
72 int leftSecond = PastSumAllSecond % 3600;
73
74 //求分钟数
75 int minute = leftSecond / 60;
76
77 //求秒数
78 int Second = leftSecond % 60;
79
80
81 int resulSecond = FirstSecond + Second;
82
83 //if (resulSecond >= 60)
84 //{
85 // this.numSecond.Value = resulSecond % 60;
86 //}
87 //else
88 //{
89 this.numSecond.Value = Second;
90 //}
91 currentMinu = FirstMinute + minute;
92
93 if (currentMinu > 60)
94 {
95 this.numMinu.Value = currentMinu % 60;
96
97 }else if(currentMinu==60)
98 {
99 FirstHour++;
100 //currentHour = FirstHour + hour + 1;
101 this.numMinu.Value = 0;
102 }
103 else
104 {
105 currentMinu = FirstMinute + minute;
106 this.numMinu.Value = currentMinu;
107 }
108
109
110 //更新界面小时,分钟,秒
111 if (currentHour > 24)
112 {
113 this.numHour.Value = currentHour % 24;
114 }else if(currentHour==24)
115 {
116 this.numHour.Value = 0;
117 }
118 else
119 {
120 currentHour = FirstHour + hour;
121 this.numHour.Value = currentHour;
122 }
123 }
124 #endregion
125
126 #region 定时关机按钮
127 private void btnClosePC_Click(object sender, EventArgs e)
128 {
129 string cmd = "shutdown -r-t";
130 }
131 #endregion
132
133 private void NumHour_ValueChanged(object sender, EventArgs e)
134 {
135
136 }
137 }
138 }