-- 作者:卷积内核
-- 发布时间:9/5/2009 1:47:00 PM
--
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Threading; using System.Timers; using System.IO; using System.Reflection; namespace timerAlarm { public class TimerForm : System.Windows.Forms.Form { //Controls and Components private System.Windows.Forms.TextBox timerInput; private System.Windows.Forms.Button StartButton; private System.Windows.Forms.Button ResetButton; private System.ComponentModel.IContainer components; //Timer and associated variables private System.Timers.Timer timerClock = new System.Timers.Timer(); private int clockTime = 0; private int alarmTime = 0; public TimerForm() { InitializeComponent(); InitializeTimer(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code #endregion public void InitializeTimer() { this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer); this.timerClock.Interval = 1000; this.timerClock.Enabled = true; } [STAThread] static void Main() { Application.Run(new TimerForm()); } private void TimerForm_Resized(object sender, System.EventArgs e) { if( this.WindowState == FormWindowState.Minimized ) { this.Hide(); } } private void StartButton_Click(object sender, System.EventArgs e) { this.clockTime = 0; inputToSeconds( this.timerInput.Text ); } private void ResetButton_Click(object sender, System.EventArgs e) { try { this.clockTime = 0; this.alarmTime = 0; this.timerInput.Text = "00:00:00"; } catch( Exception ex ) { MessageBox.Show("ResetButton_Click(): " + ex.Message ); } } public void OnTimer(Object source, ElapsedEventArgs e) { try { this.clockTime++; int countdown = this.alarmTime - this.clockTime ; if( this.alarmTime != 0 ) { this.timerInput.Text = secondsToTime(countdown); } //Sound Alarm if( this.clockTime == this.alarmTime ) { MessageBox.Show("Play Sound"); } } catch( Exception ex ) { MessageBox.Show("OnTimer(): " + ex.Message ); } } private void inputToSeconds( string timerInput ) { try { string[] timeArray = new string[3]; int minutes = 0; int hours = 0; int seconds = 0; int occurence = 0; int length = 0; occurence = timerInput.LastIndexOf(":"); length = timerInput.Length; //Check for invalid input if( occurence == -1 || length != 8 ) { MessageBox.Show("Invalid Time Format."); 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( Exception e ) { MessageBox.Show("inputToSeconds(): " + e.Message ); } } public string secondsToTime( int seconds ) { int minutes = 0; int hours = 0; while( seconds >= 60 ) { minutes += 1; seconds -= 60; } while( minutes >= 60 ) { hours += 1; minutes -= 60; } string strHours = hours.ToString(); string strMinutes = minutes.ToString(); string strSeconds = seconds.ToString(); if( strHours.Length < 2 ) strHours = "0" + strHours; if( strMinutes.Length < 2 ) strMinutes = "0" + strMinutes; if( strSeconds.Length < 2 ) strSeconds = "0" + strSeconds; return strHours + ":" + strMinutes + ":" + strSeconds; } } } 实际的执行代码比上面的要多,本范例演示了Timer在实际环境中的一个简单应用,仅仅使用了一些简单的基础知识来创建一个简单的应用。
|