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

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

程序员文章站 2023-12-13 11:03:16
恩  主要大家可以看下思路吧  图形界面里 除了图标和音乐两个资源 别的都是代码。 时间没有用timer组件 是自创的time类在一个线程中进行的倒计时...

恩  主要大家可以看下思路吧  图形界面里 除了图标和音乐两个资源 别的都是代码。 时间没有用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();
        }
    }
}

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

上一篇:

下一篇: