C#使用timer实现的简单闹钟程序
本文实例讲述了C#使用timer实现的简单闹钟程序。分享给大家供大家参考。具体如下:
当我在电脑上工作,我经常会被一些东西吸引,比如某宝,结果三个小时过去了我都完全没有注意到。所以我通过C#做了一个简单闹钟程序,这个小程序主要使用C#Timer对象,让用户设定一个倒计时的时长,如果时间到了,就播放一个wav音频文件(也就是闹铃)。
我一直试图保持这个timer的简单性,但我还是添加了一些额外的功能,在状态栏中显示一个通知图标。
通过这个小应用你也可以了解到C#中timer定时器的一些简单用法。
usingSystem; usingSystem.Drawing; usingSystem.Collections; usingSystem.ComponentModel; usingSystem.Windows.Forms; usingSystem.Data; usingSystem.Threading; usingSystem.Timers; usingSystem.IO; usingSystem.Reflection; namespacetimerAlarm { publicclassTimerForm:System.Windows.Forms.Form { //ControlsandComponents privateSystem.Windows.Forms.TextBoxtimerInput; privateSystem.Windows.Forms.ButtonStartButton; privateSystem.Windows.Forms.ButtonResetButton; privateSystem.ComponentModel.IContainercomponents; //Timerandassociatedvariables privateSystem.Timers.TimertimerClock=newSystem.Timers.Timer(); privateintclockTime=0; privateintalarmTime=0; publicTimerForm() { InitializeComponent(); InitializeTimer(); } protectedoverridevoidDispose(booldisposing) { if(disposing) { if(components!=null) { components.Dispose(); } } base.Dispose(disposing); } #regionWindowsFormDesignergeneratedcode ///<SUMMARY> ///RequiredmethodforDesignersupport-donotmodify ///thecontentsofthismethodwiththecodeeditor. ///</SUMMARY> privatevoidInitializeComponent() { this.components=newSystem.ComponentModel.Container(); System.Resources.ResourceManagerresources=newSystem. Resources.ResourceManager(typeof(TimerForm)); this.timerInput=newSystem.Windows.Forms.TextBox(); this.StartButton=newSystem.Windows.Forms.Button(); this.ResetButton=newSystem.Windows.Forms.Button(); this.SuspendLayout(); // //timerInput // this.timerInput.Location=newSystem.Drawing.Point(12,13); this.timerInput.Name="timerInput"; this.timerInput.Size=newSystem.Drawing.Size(50,20); this.timerInput.TabIndex=0; this.timerInput.Text="00:00:00"; // //StartButton // this.StartButton.FlatStyle=System.Windows.Forms. FlatStyle.System; this.StartButton.Location=newSystem.Drawing.Point(75,11); this.StartButton.Name="StartButton"; this.StartButton.TabIndex=1; this.StartButton.Text="Start"; this.StartButton.Click+=newSystem.EventHandler (this.StartButton_Click); // //ResetButton // this.ResetButton.FlatStyle=System.Windows.Forms. FlatStyle.System; this.ResetButton.Location=newSystem.Drawing.Point(161,11); this.ResetButton.Name="ResetButton"; this.ResetButton.TabIndex=2; this.ResetButton.Text="Reset"; this.ResetButton.Click+=new System.EventHandler(this.ResetButton_Click); // //TimerForm // this.AutoScaleBaseSize=newSystem.Drawing.Size(5,13); this.ClientSize=newSystem.Drawing.Size(247,46); this.Controls.AddRange(newSystem.Windows.Forms.Control[]{ this.ResetButton, this.StartButton, this.timerInput}); this.FormBorderStyle=System.Windows.Forms. FormBorderStyle.FixedSingle; this.Icon=((System.Drawing.Icon)(resources. GetObject("$this.Icon"))); this.MaximizeBox=false; this.Name="TimerForm"; this.StartPosition=System.Windows.Forms. FormStartPosition.CenterScreen; this.Text="AlarmTimer"; this.Resize+=newSystem.EventHandler(this. TimerForm_Resized); this.ResumeLayout(false); } #endregion publicvoidInitializeTimer() { this.timerClock.Elapsed+=newElapsedEventHandler(OnTimer); this.timerClock.Interval=1000; this.timerClock.Enabled=true; } [STAThread] staticvoidMain() { Application.Run(newTimerForm()); } privatevoidTimerForm_Resized(objectsender,System.EventArgse) { if(this.WindowState==FormWindowState.Minimized) { this.Hide(); } } privatevoidStartButton_Click(objectsender,System.EventArgse) { this.clockTime=0; inputToSeconds(this.timerInput.Text); } privatevoidResetButton_Click(objectsender,System.EventArgse) { try { this.clockTime=0; this.alarmTime=0; this.timerInput.Text="00:00:00"; } catch(Exceptionex) { MessageBox.Show("ResetButton_Click():"+ex.Message); } } publicvoidOnTimer(Objectsource,ElapsedEventArgse) { try { this.clockTime++; intcountdown=this.alarmTime-this.clockTime; if(this.alarmTime!=0) { this.timerInput.Text=secondsToTime(countdown); } //SoundAlarm if(this.clockTime==this.alarmTime) { MessageBox.Show("PlaySound"); } } catch(Exceptionex) { MessageBox.Show("OnTimer():"+ex.Message); } } privatevoidinputToSeconds(stringtimerInput) { try { string[]timeArray=newstring[3]; intminutes=0; inthours=0; intseconds=0; intoccurence=0; intlength=0; occurence=timerInput.LastIndexOf(":"); length=timerInput.Length; //Checkforinvalidinput if(occurence==-1||length!=8) { MessageBox.Show("InvalidTimeFormat."); ResetButton_Click(null,null); } else { timeArray=timerInput.Split(':'); seconds=Convert.ToInt32(timeArray[2]); minutes=Convert.ToInt32(timeArray[1]); hours=Convert.ToInt32(timeArray[0]); this.alarmTime+=seconds; this.alarmTime+=minutes*60; this.alarmTime+=(hours*60)*60; } } catch(Exceptione) { MessageBox.Show("inputToSeconds():"+e.Message); } } publicstringsecondsToTime(intseconds) { intminutes=0; inthours=0; while(seconds>=60) { minutes+=1; seconds-=60; } while(minutes>=60) { hours+=1; minutes-=60; } stringstrHours=hours.ToString(); stringstrMinutes=minutes.ToString(); stringstrSeconds=seconds.ToString(); if(strHours.Length<2) strHours="0"+strHours; if(strMinutes.Length<2) strMinutes="0"+strMinutes; if(strSeconds.Length<2) strSeconds="0"+strSeconds; returnstrHours+":"+strMinutes+":"+strSeconds; } } }
完整实例代码点击此处本站下载。
希望本文所述对大家的C#程序设计有所帮助。