使用Timer實現WinForm柔城鬧鍾,以下是原理代碼。這只是簡單的原理,最終實現包括時間設置的驗證,響鈴等提醒方式,請下載源代碼。
using System; using System.Windows.Forms; namespace Sosoft.Cnblogs.Com { //柔城鬧鍾 public partial class MainForm : Form { Timer SoSoftTimer = new Timer();//定義計時器 DateTime RingTime;//提醒時間 public MainForm() { InitializeComponent(); SoSoftTimer.Interval = 400;//設置檢測間隔時間 SoSoftTimer.Tick += new EventHandler(SoSoftTimer_Tick); textBox_time.Text = DateTime.Now.AddHours(1).ToString();//設置初始鬧鍾默認值 } void SoSoftTimer_Tick(object sender, EventArgs e) { if (DateTime.Now.CompareTo(RingTime) > 0)//提醒的時間條件 { Ringing(); } } /// <summary> /// 提醒 /// </summary> private void Ringing() { SoSoftTimer.Stop();//停止計時器 //加入提醒代碼,包括響鈴。 } /// <summary> /// 打開鬧鍾 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_TurnOn_Click(object sender, EventArgs e) { RingTime = Convert.ToDateTime(textBox_time.Text); SoSoftTimer.Start();//開始計時器 } } }
點擊這里進入完整的源代碼下載頁面:http://sosoft.codeplex.com/releases/view/95183
點擊sosoft.rar下載
另外源代碼的理解請參看:
http://www.cnblogs.com/sosoft/archive/2012/09/21/sound.html
http://www.cnblogs.com/sosoft/archive/2012/09/20/tuopan.html
http://www.cnblogs.com/sosoft/archive/2012/09/18/2691140.html