c#编写的番茄钟倒计时器代码

恩  主要大家可以看下思路吧  图形界面里 除了图标和音乐两个资源 别的都是代码。 时间没有用timer组件 是自创的Time类在一个线程中进行的倒计时。  对于导出记录 创建了一个Record类  别的就没什么了  。。。。

Program.cs

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace 番茄钟
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.Designer.cs

代码如下:

namespace 番茄钟
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        #region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.label4 = new System.Windows.Forms.Label();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.button6 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.BackColor = System.Drawing.Color.Black;
            this.label1.Font = new System.Drawing.Font("黑体", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.ForeColor = System.Drawing.Color.Red;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(283, 70);
            this.label1.TabIndex = 0;
            this.label1.Text = "倒计时  00:00";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 104);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(65, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "番茄时间:";
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(83, 101);
            this.textBox1.MaxLength = 5;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(54, 21);
            this.textBox1.TabIndex = 2;
            this.textBox1.Text = "25";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(12, 136);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(65, 12);
            this.label3.TabIndex = 5;
            this.label3.Text = "工作安排:";
            //
            // textBox2
            //
            this.textBox2.Location = new System.Drawing.Point(85, 133);
            this.textBox2.MaxLength = 300;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(198, 21);
            this.textBox2.TabIndex = 6;
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(13, 163);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 7;
            this.button1.Text = "开始";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(108, 163);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 8;
            this.button2.Text = "停止";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // button3
            //
            this.button3.Location = new System.Drawing.Point(208, 162);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 9;
            this.button3.Text = "暂停";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            //
            // richTextBox1
            //
            this.richTextBox1.BackColor = System.Drawing.Color.White;
            this.richTextBox1.ForeColor = System.Drawing.Color.Black;
            this.richTextBox1.Location = new System.Drawing.Point(13, 193);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.ReadOnly = true;
            this.richTextBox1.Size = new System.Drawing.Size(270, 279);
            this.richTextBox1.TabIndex = 10;
            this.richTextBox1.Text = "";
            //
            // label4
            //
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(144, 104);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(29, 12);
            this.label4.TabIndex = 11;
            this.label4.Text = "分钟";
            //
            // button4
            //
            this.button4.Location = new System.Drawing.Point(13, 479);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 12;
            this.button4.Text = "清空";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            //
            // button5
            //
            this.button5.Location = new System.Drawing.Point(208, 478);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(75, 23);
            this.button5.TabIndex = 13;
            this.button5.Text = "导出";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            //
            // button6
            //
            this.button6.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.button6.Location = new System.Drawing.Point(191, 93);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(104, 34);
            this.button6.TabIndex = 14;
            this.button6.Text = "番茄钟 V1.1";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click_1);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(309, 508);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "番茄钟";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
        }
        #endregion
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.Button button6;
    }
}

Form1.cs

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;
using System.IO;
using System.Media;
using System.Reflection;
namespace 番茄钟
{
    public partial class Form1 : Form
    {
        private Time time;
        private Thread cutDownThread;
        private bool pause=false;
        private int num = 0;
        private ArrayList records;
        string title = "倒计时  ";
        public void cutDownTime()
        {
            while (!time.Stop)
            {
                Thread.Sleep(1000);
                time.subOneSecond();
                this.Invoke((EventHandler)delegate
                {
                    label1.Text = title + time.showTime();
                });
            }
            this.Invoke((EventHandler)delegate
            {
                richTextBox1.AppendText("完成\n");
                Record r = (Record)records[records.Count - 1];
                r.getEnd("完成状态:完成");
            });
            Assembly asm = Assembly.GetExecutingAssembly();
            string name = asm.GetName().Name;
            SoundPlayer sp = new SoundPlayer(番茄钟.Properties.Resources._3462_clip);
            sp.Play();
            MessageBox.Show("时间到了!!!");
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.AppendText("已完成的番茄钟记录:\n");
            records = new ArrayList();
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            if (cutDownThread != null)
                if (cutDownThread.IsAlive)
                    cutDownThread.Abort();
            base.OnClosing(e);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (!pause)
            {
                double min;
                if (!double.TryParse(textBox1.Text, out min))
                {
                    MessageBox.Show("请正确输入数字");
                    return;
                }
                if (min < 0&&min>99)
                {
                    MessageBox.Show("请输入一个不超过99的正实数");
                    return;
                }
                time = new Time(min);
                label1.Text = title + time.showTime();
                cutDownThread = new Thread(new ThreadStart(cutDownTime));
                cutDownThread.Start();
                StringBuilder temp = new StringBuilder();
                temp.Append("番茄钟").Append(++num).Append(":");
                if (textBox2.Text != "")
                    temp.Append(textBox2.Text);
                else
                    temp.Append("无具体安排");
                records.Add(new Record(temp.ToString(), min));
                temp.Append("\n完成状态:");
                richTextBox1.AppendText(temp.ToString());
            }
            else
            {
                pause = false;
                label1.Text = title + time.showTime();
                cutDownThread = new Thread(new ThreadStart(cutDownTime));
                cutDownThread.Start();
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (cutDownThread != null)
                if (cutDownThread.IsAlive)
                    cutDownThread.Abort();
            label1.Text = title + time.showTime();
            pause = true;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (cutDownThread != null)
                if (cutDownThread.IsAlive)
                    cutDownThread.Abort();
            time = new Time(0);
            label1.Text = title + time.showTime();
            richTextBox1.AppendText("未完成\n");
            Record r = (Record)records[records.Count - 1];
            r.getEnd("完成状态:未完成");
        }
        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("你真的要清空吗?\n(清空记录会把之前所有\n  完成的番茄钟记录消\n  除,建议先将记录导出)",
                "确认",MessageBoxButtons.YesNo);
            if (dr == DialogResult.No)
            {
                MessageBox.Show("清空操作已撤销");
            }
            else if (dr == DialogResult.Yes)
            {
                num = 0;
                richTextBox1.Text = "";
                richTextBox1.AppendText("已完成的番茄钟记录:\n");
                if (cutDownThread != null)
                    if (cutDownThread.IsAlive)
                        cutDownThread.Abort();
                time = new Time(0);
                label1.Text = title + time.showTime();
                MessageBox.Show("番茄钟记录已清空并重置!");
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            string fn="";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "文本文件 (*.txt) |*.txt";
            ofd.FilterIndex = 1;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                fn = ofd.FileName;
                FileStream fs = new FileStream(fn, FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine("************************************************");
                sw.WriteLine("         " + DateTime.Now + "的番茄钟记录  ");
                sw.WriteLine("************************************************");
                foreach (Record r in records)
                {
                    sw.Write(r.showRecord());
                }
                sw.Close();
                fs.Close();
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            double min = double.Parse(textBox2.Text);
            int a = (int)min;
            int b = (int)((min - a) * 60);
            textBox2.Text = a.ToString()+":"+b.ToString();
        }
        private void button6_Click_1(object sender, EventArgs e)
        {
            MessageBox.Show("          番茄钟V1.1\n 在使用中如遇到问题或发现漏洞,\n  请联系QQ:583490567,谢谢!\n  本产品由晓奇工作室荣誉出品\n 版权:Copyright © 蒋晓奇 2015",
                "关于番茄钟", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
    public class Time
    {
        private int sec;
        private int min;
        private bool stop;
        public Time(double min)
        {
            this.min = (int)min;
            sec = (int)((min - this.min) * 60);
            stop = false;
        }
        public void subOneSecond()
        {
            if (sec > 0)
                sec--;
            else if (min > 0)
            {
                min--;
                sec = 59;
            }
            else
                stop = true;
        }
        public bool Stop
        {
            get { return stop; }
        }
        public string showTime()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(min<10?"0"+min:min.ToString()).Append(":").Append(sec<10?"0"+sec:sec.ToString());
            return sb.ToString();
        }
    }
    public class Record
    {
        private string text;
        private DateTime first;
        private DateTime last;
        private double length;
        private string state;
        public Record(string text,double length)
        {
            this.text = text;
            first = DateTime.Now;
            this.length = length;
        }
        public void getEnd(string state)
        {
            last = DateTime.Now;
            this.state = state;
        }
        public string showRecord()
        {
            if (last == null)
                last = DateTime.Now;
            if (state == null)
                state = "未知";
            StringBuilder s = new StringBuilder();
            s.Append("起止时间:").Append(first.ToString()).Append("-").AppendLine(last.ToString());
            s.AppendLine(text).Append(state).Append("\t").
                Append("番茄钟时长:").Append(length.ToString()).AppendLine("分钟");
            return s.ToString();
        }
    }
}

以上就是本文分享的全部代码了,希望大家能够喜欢。

(0)

相关推荐

  • C# string格式的日期时间字符串转为DateTime类型的方法

    方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss 方法二:Convert.ToDateTime(string, IFormatProvider) DateTime dt; DateTimeFormatInfo dtFormat = new System.GlobalizationDateTimeFormatInfo(); dtFormat.ShortDatePattern = "yyyy/MM/dd";

  • C#使用Shader实现夜幕降临倒计时的效果

    最近火爆全球的PC游戏Battlerite(战争仪式)在倒计时的会生成一种类似夜幕降临的效果,会以战场中心为圆心,某个长度为半径的范围外是暗的,而这个半径会逐渐缩小,而圆之外的阴暗部分是附着地形的,本文就尝试使用屏幕后处理的手段来实现这种效果. (暂时缺少Battlerite的截图,稍后会补上) 首先看效果图: 注:本文参考了Tasharen Fog of War插件 创建一个C#脚本,命名为NightFall.cs,为NightFall类创建一些公共变量(nightColor,center和r

  • asp.net(C#)实现功能强大的时间日期处理类完整实例

    本文实例讲述了asp.net(C#)实现功能强大的时间日期处理类.分享给大家供大家参考,具体如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts

  • C#中各种计时器用法小结

    本文实例总结了C#中各种计时器用法.分享给大家供大家参考,具体如下: 1.使用 Stopwatch 类 (System.Diagnostics.Stopwatch) Stopwatch 实例可以测量一个时间间隔的运行时间,也可以测量多个时间间隔的总运行时间.在典型的 Stopwatch 方案中,先调用 Start 方法,然后调用 Stop 方法,最后使用 Elapsed 属性检查运行时间. Stopwatch 实例或者在运行,或者已停止:使用 IsRunning 可以确定 Stopwatch 的

  • c#的时间日期操作示例分享(c#获取当前日期)

    1.给定时间戳返回指定的时间格式 复制代码 代码如下: private string StampToDate(string timeStamp,string format){ DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = long.Parse(timeStamp + "0000000"); TimeSpan toNow = new TimeS

  • C#基于TimeSpan实现倒计时效果的方法

    本文实例展示了C#基于TimeSpan实现倒计时效果的方法,比较实用的功能,对于初学者来说有一定的学习参考价值.具体实现方法如下: 示例代码如下: using System; using System.Threading; namespace ConsoleApplication29 { class Program { static void Main(string[] args) { try { DateTime _timeEnd = DateTime.Now.AddSeconds(62);

  • C#实现windows form倒计时的方法

    本文实例讲述了C#实现windows form倒计时的方法.分享给大家供大家参考.具体实现方法如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace date { public partial cl

  • C# 常用日期时间函数(老用不熟)

    --DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.DateTime.Now; 1.2 取当前年 int 年=currentTime.Year; 1.3 取当前月 int 月=currentTime.Month; 1.4 取当前日 int 日=currentTime.Day; 1.5 取当前时 int 时=currentTime.Hour; 1.6 取

  • C#中计时器的简单实现方法示例

    本文实例讲述了C#中计时器的简单实现方法.分享给大家供大家参考,具体如下: startTime = DateTime.Now; DispatcherTimer dt = new DispatcherTimer(); dt.Interval = new TimeSpan(0, 0, 1); dt.Tick += new EventHandler(dt_Tick);//调用函数 dt.Start(); void dt_Tick(object sender, EventArgs e) { timeSp

  • C#实现的Win32控制台线程计时器功能示例

    本文实例讲述了C#实现的Win32控制台线程计时器功能.分享给大家供大家参考,具体如下: 在C#中提供了三种类型的计时器: 1.基于 Windows 的标准计时器(System.Windows.Forms.Timer) 2.基于服务器的计时器(System.Timers.Timer) 3.线程计时器(System.Threading.Timer) 一.基于 Windows 的标准计时器(System.Windows.Forms.Timer) 首先注意一点就是:Windows 计时器是为单线程环境

  • C#结合JavaScript实现秒杀倒计时的方法

    本文实例讲述了C#结合JavaScript实现秒杀倒计时的方法.分享给大家供大家参考.具体如下: 最近做个秒杀活动,要用到倒计时.要求每周三上午10:00开始倒计时 private string Dtime() { byte tempB = (byte)DateTime.Now.DayOfWeek; byte dayByte = (byte)DayOfWeek.Wednesday; DateTime wednesdayNow = DateTime.Now.AddDays(dayByte - te

随机推荐