c#编写的番茄钟倒计时器代码
恩 主要大家可以看下思路吧 图形界面里除了图标和音乐两个资源别的都是代码。时间没有用timer组件是自创的Time类在一个线程中进行的倒计时。 对于导出记录创建了一个Record类 别的就没什么了 。。。。
Program.cs
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Windows.Forms; namespace番茄钟 { staticclassProgram { ///<summary> ///应用程序的主入口点。 ///</summary> [STAThread] staticvoidMain() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(newForm1()); } } }
Form1.Designer.cs
namespace番茄钟 { partialclassForm1 { ///<summary> ///必需的设计器变量。 ///</summary> privateSystem.ComponentModel.IContainercomponents=null; ///<summary> ///清理所有正在使用的资源。 ///</summary> ///<paramname="disposing">如果应释放托管资源,为true;否则为false。</param> protectedoverridevoidDispose(booldisposing) { if(disposing&&(components!=null)) { components.Dispose(); } base.Dispose(disposing); } #regionWindows窗体设计器生成的代码 ///<summary> ///设计器支持所需的方法-不要 ///使用代码编辑器修改此方法的内容。 ///</summary> privatevoidInitializeComponent() { System.ComponentModel.ComponentResourceManagerresources=newSystem.ComponentModel.ComponentResourceManager(typeof(Form1)); this.label1=newSystem.Windows.Forms.Label(); this.label2=newSystem.Windows.Forms.Label(); this.textBox1=newSystem.Windows.Forms.TextBox(); this.label3=newSystem.Windows.Forms.Label(); this.textBox2=newSystem.Windows.Forms.TextBox(); this.button1=newSystem.Windows.Forms.Button(); this.button2=newSystem.Windows.Forms.Button(); this.button3=newSystem.Windows.Forms.Button(); this.richTextBox1=newSystem.Windows.Forms.RichTextBox(); this.label4=newSystem.Windows.Forms.Label(); this.button4=newSystem.Windows.Forms.Button(); this.button5=newSystem.Windows.Forms.Button(); this.button6=newSystem.Windows.Forms.Button(); this.SuspendLayout(); // //label1 // this.label1.BackColor=System.Drawing.Color.Black; this.label1.Font=newSystem.Drawing.Font("黑体",26.25F,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,((byte)(134))); this.label1.ForeColor=System.Drawing.Color.Red; this.label1.Location=newSystem.Drawing.Point(12,9); this.label1.Name="label1"; this.label1.Size=newSystem.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=newSystem.Drawing.Point(12,104); this.label2.Name="label2"; this.label2.Size=newSystem.Drawing.Size(65,12); this.label2.TabIndex=1; this.label2.Text="番茄时间:"; // //textBox1 // this.textBox1.Location=newSystem.Drawing.Point(83,101); this.textBox1.MaxLength=5; this.textBox1.Name="textBox1"; this.textBox1.Size=newSystem.Drawing.Size(54,21); this.textBox1.TabIndex=2; this.textBox1.Text="25"; // //label3 // this.label3.AutoSize=true; this.label3.Location=newSystem.Drawing.Point(12,136); this.label3.Name="label3"; this.label3.Size=newSystem.Drawing.Size(65,12); this.label3.TabIndex=5; this.label3.Text="工作安排:"; // //textBox2 // this.textBox2.Location=newSystem.Drawing.Point(85,133); this.textBox2.MaxLength=300; this.textBox2.Name="textBox2"; this.textBox2.Size=newSystem.Drawing.Size(198,21); this.textBox2.TabIndex=6; // //button1 // this.button1.Location=newSystem.Drawing.Point(13,163); this.button1.Name="button1"; this.button1.Size=newSystem.Drawing.Size(75,23); this.button1.TabIndex=7; this.button1.Text="开始"; this.button1.UseVisualStyleBackColor=true; this.button1.Click+=newSystem.EventHandler(this.button1_Click); // //button2 // this.button2.Location=newSystem.Drawing.Point(108,163); this.button2.Name="button2"; this.button2.Size=newSystem.Drawing.Size(75,23); this.button2.TabIndex=8; this.button2.Text="停止"; this.button2.UseVisualStyleBackColor=true; this.button2.Click+=newSystem.EventHandler(this.button2_Click); // //button3 // this.button3.Location=newSystem.Drawing.Point(208,162); this.button3.Name="button3"; this.button3.Size=newSystem.Drawing.Size(75,23); this.button3.TabIndex=9; this.button3.Text="暂停"; this.button3.UseVisualStyleBackColor=true; this.button3.Click+=newSystem.EventHandler(this.button3_Click); // //richTextBox1 // this.richTextBox1.BackColor=System.Drawing.Color.White; this.richTextBox1.ForeColor=System.Drawing.Color.Black; this.richTextBox1.Location=newSystem.Drawing.Point(13,193); this.richTextBox1.Name="richTextBox1"; this.richTextBox1.ReadOnly=true; this.richTextBox1.Size=newSystem.Drawing.Size(270,279); this.richTextBox1.TabIndex=10; this.richTextBox1.Text=""; // //label4 // this.label4.AutoSize=true; this.label4.Location=newSystem.Drawing.Point(144,104); this.label4.Name="label4"; this.label4.Size=newSystem.Drawing.Size(29,12); this.label4.TabIndex=11; this.label4.Text="分钟"; // //button4 // this.button4.Location=newSystem.Drawing.Point(13,479); this.button4.Name="button4"; this.button4.Size=newSystem.Drawing.Size(75,23); this.button4.TabIndex=12; this.button4.Text="清空"; this.button4.UseVisualStyleBackColor=true; this.button4.Click+=newSystem.EventHandler(this.button4_Click); // //button5 // this.button5.Location=newSystem.Drawing.Point(208,478); this.button5.Name="button5"; this.button5.Size=newSystem.Drawing.Size(75,23); this.button5.TabIndex=13; this.button5.Text="导出"; this.button5.UseVisualStyleBackColor=true; this.button5.Click+=newSystem.EventHandler(this.button5_Click); // //button6 // this.button6.Font=newSystem.Drawing.Font("微软雅黑",12F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(134))); this.button6.Location=newSystem.Drawing.Point(191,93); this.button6.Name="button6"; this.button6.Size=newSystem.Drawing.Size(104,34); this.button6.TabIndex=14; this.button6.Text="番茄钟V1.1"; this.button6.UseVisualStyleBackColor=true; this.button6.Click+=newSystem.EventHandler(this.button6_Click_1); // //Form1 // this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F); this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font; this.ClientSize=newSystem.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+=newSystem.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion privateSystem.Windows.Forms.Labellabel1; privateSystem.Windows.Forms.Labellabel2; privateSystem.Windows.Forms.TextBoxtextBox1; privateSystem.Windows.Forms.Labellabel3; privateSystem.Windows.Forms.TextBoxtextBox2; privateSystem.Windows.Forms.Buttonbutton1; privateSystem.Windows.Forms.Buttonbutton2; privateSystem.Windows.Forms.Buttonbutton3; privateSystem.Windows.Forms.RichTextBoxrichTextBox1; privateSystem.Windows.Forms.Labellabel4; privateSystem.Windows.Forms.Buttonbutton4; privateSystem.Windows.Forms.Buttonbutton5; privateSystem.Windows.Forms.Buttonbutton6; } }
Form1.cs
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Linq; usingSystem.Text; usingSystem.Windows.Forms; usingSystem.Threading; usingSystem.Collections; usingSystem.IO; usingSystem.Media; usingSystem.Reflection; namespace番茄钟 { publicpartialclassForm1:Form { privateTimetime; privateThreadcutDownThread; privateboolpause=false; privateintnum=0; privateArrayListrecords; stringtitle="倒计时 "; publicvoidcutDownTime() { while(!time.Stop) { Thread.Sleep(1000); time.subOneSecond(); this.Invoke((EventHandler)delegate { label1.Text=title+time.showTime(); }); } this.Invoke((EventHandler)delegate { richTextBox1.AppendText("完成\n"); Recordr=(Record)records[records.Count-1]; r.getEnd("完成状态:完成"); }); Assemblyasm=Assembly.GetExecutingAssembly(); stringname=asm.GetName().Name; SoundPlayersp=newSoundPlayer(番茄钟.Properties.Resources._3462_clip); sp.Play(); MessageBox.Show("时间到了!!!"); } publicForm1() { InitializeComponent(); } privatevoidForm1_Load(objectsender,EventArgse) { richTextBox1.AppendText("已完成的番茄钟记录:\n"); records=newArrayList(); } protectedoverridevoidOnClosing(CancelEventArgse) { if(cutDownThread!=null) if(cutDownThread.IsAlive) cutDownThread.Abort(); base.OnClosing(e); } privatevoidbutton1_Click(objectsender,EventArgse) { if(!pause) { doublemin; if(!double.TryParse(textBox1.Text,outmin)) { MessageBox.Show("请正确输入数字"); return; } if(min<0&&min>99) { MessageBox.Show("请输入一个不超过99的正实数"); return; } time=newTime(min); label1.Text=title+time.showTime(); cutDownThread=newThread(newThreadStart(cutDownTime)); cutDownThread.Start(); StringBuildertemp=newStringBuilder(); temp.Append("番茄钟").Append(++num).Append(":"); if(textBox2.Text!="") temp.Append(textBox2.Text); else temp.Append("无具体安排"); records.Add(newRecord(temp.ToString(),min)); temp.Append("\n完成状态:"); richTextBox1.AppendText(temp.ToString()); } else { pause=false; label1.Text=title+time.showTime(); cutDownThread=newThread(newThreadStart(cutDownTime)); cutDownThread.Start(); } } privatevoidbutton3_Click(objectsender,EventArgse) { if(cutDownThread!=null) if(cutDownThread.IsAlive) cutDownThread.Abort(); label1.Text=title+time.showTime(); pause=true; } privatevoidbutton2_Click(objectsender,EventArgse) { if(cutDownThread!=null) if(cutDownThread.IsAlive) cutDownThread.Abort(); time=newTime(0); label1.Text=title+time.showTime(); richTextBox1.AppendText("未完成\n"); Recordr=(Record)records[records.Count-1]; r.getEnd("完成状态:未完成"); } privatevoidbutton4_Click(objectsender,EventArgse) { DialogResultdr=MessageBox.Show("你真的要清空吗?\n(清空记录会把之前所有\n 完成的番茄钟记录消\n 除,建议先将记录导出)", "确认",MessageBoxButtons.YesNo); if(dr==DialogResult.No) { MessageBox.Show("清空操作已撤销"); } elseif(dr==DialogResult.Yes) { num=0; richTextBox1.Text=""; richTextBox1.AppendText("已完成的番茄钟记录:\n"); if(cutDownThread!=null) if(cutDownThread.IsAlive) cutDownThread.Abort(); time=newTime(0); label1.Text=title+time.showTime(); MessageBox.Show("番茄钟记录已清空并重置!"); } } privatevoidbutton5_Click(objectsender,EventArgse) { stringfn=""; OpenFileDialogofd=newOpenFileDialog(); ofd.Filter="文本文件(*.txt)|*.txt"; ofd.FilterIndex=1; if(ofd.ShowDialog()==DialogResult.OK) { fn=ofd.FileName; FileStreamfs=newFileStream(fn,FileMode.Append,FileAccess.Write); StreamWritersw=newStreamWriter(fs,Encoding.Default); sw.WriteLine("************************************************"); sw.WriteLine(" "+DateTime.Now+"的番茄钟记录 "); sw.WriteLine("************************************************"); foreach(Recordrinrecords) { sw.Write(r.showRecord()); } sw.Close(); fs.Close(); } } privatevoidbutton6_Click(objectsender,EventArgse) { doublemin=double.Parse(textBox2.Text); inta=(int)min; intb=(int)((min-a)*60); textBox2.Text=a.ToString()+":"+b.ToString(); } privatevoidbutton6_Click_1(objectsender,EventArgse) { MessageBox.Show(" 番茄钟V1.1\n在使用中如遇到问题或发现漏洞,\n 请联系QQ:583490567,谢谢!\n 本产品由晓奇工作室荣誉出品\n版权:Copyright©蒋晓奇2015", "关于番茄钟",MessageBoxButtons.OK,MessageBoxIcon.Information); } } publicclassTime { privateintsec; privateintmin; privateboolstop; publicTime(doublemin) { this.min=(int)min; sec=(int)((min-this.min)*60); stop=false; } publicvoidsubOneSecond() { if(sec>0) sec--; elseif(min>0) { min--; sec=59; } else stop=true; } publicboolStop { get{returnstop;} } publicstringshowTime() { StringBuildersb=newStringBuilder(); sb.Append(min<10?"0"+min:min.ToString()).Append(":").Append(sec<10?"0"+sec:sec.ToString()); returnsb.ToString(); } } publicclassRecord { privatestringtext; privateDateTimefirst; privateDateTimelast; privatedoublelength; privatestringstate; publicRecord(stringtext,doublelength) { this.text=text; first=DateTime.Now; this.length=length; } publicvoidgetEnd(stringstate) { last=DateTime.Now; this.state=state; } publicstringshowRecord() { if(last==null) last=DateTime.Now; if(state==null) state="未知"; StringBuilders=newStringBuilder(); s.Append("起止时间:").Append(first.ToString()).Append("-").AppendLine(last.ToString()); s.AppendLine(text).Append(state).Append("\t"). Append("番茄钟时长:").Append(length.ToString()).AppendLine("分钟"); returns.ToString(); } } }
以上就是本文分享的全部代码了,希望大家能够喜欢。