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

C#使用timer实现的简单闹钟程序

程序员文章站 2023-12-12 14:47:40
本文实例讲述了c#使用timer实现的简单闹钟程序。分享给大家供大家参考。具体如下: 当我在电脑上工作,我经常会被一些东西吸引,比如某宝,结果三个小时过去了我都完全没有注...

本文实例讲述了c#使用timer实现的简单闹钟程序。分享给大家供大家参考。具体如下:

当我在电脑上工作,我经常会被一些东西吸引,比如某宝,结果三个小时过去了我都完全没有注意到。所以我通过c#做了一个简单闹钟程序,这个小程序主要使用c# timer对象,让用户设定一个倒计时的时长,如果时间到了,就播放一个wav音频文件(也就是闹铃)。

我一直试图保持这个timer的简单性,但我还是添加了一些额外的功能,在状态栏中显示一个通知图标。
通过这个小应用你也可以了解到c#中timer定时器的一些简单用法。

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
    /// <summary>
    /// required method for designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void initializecomponent()
    {
      this.components = new system.componentmodel.container();
      system.resources.resourcemanager resources = new system.
        resources.resourcemanager(typeof(timerform));
      this.timerinput = new system.windows.forms.textbox();
      this.startbutton = new system.windows.forms.button();
      this.resetbutton = new system.windows.forms.button();
      this.suspendlayout();
      //
      // timerinput
      //
      this.timerinput.location = new system.drawing.point(12, 13);
      this.timerinput.name = "timerinput";
      this.timerinput.size = new system.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 = new system.drawing.point(75, 11);
      this.startbutton.name = "startbutton";
      this.startbutton.tabindex = 1;
      this.startbutton.text = "start";
      this.startbutton.click += new system.eventhandler
        (this.startbutton_click);
      //
      // resetbutton
      //
      this.resetbutton.flatstyle = system.windows.forms.
        flatstyle.system;
      this.resetbutton.location = new system.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 = new system.drawing.size(5, 13);
      this.clientsize = new system.drawing.size(247, 46);
      this.controls.addrange(new system.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 = "alarm timer";
      this.resize += new system.eventhandler(this.
        timerform_resized);
      this.resumelayout(false);
    }
    #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;
    }
  }
}

完整实例代码点击此处本站下载

希望本文所述对大家的c#程序设计有所帮助。

上一篇:

下一篇: