欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

倒计时功能实现

程序员文章站 2022-04-09 09:13:06
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; ... ......
 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 }