ソフトの起動で計測を開始、終了で停止するだけの単純なタイマー。自分のモチベーションアップ用。
時間のリセットはdat.xmlを削除。Cドライブなどで実行すると落ちる。
以前も似たようなソフトを作ろうとしていたが、面倒になったのでシンプルにした。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace study_timer_simple { public partial class Form1 : Form { public Form1() { InitializeComponent(); string path = Application.StartupPath + @"\dat.xml"; TimeSpan load_time_span = new TimeSpan(); if (System.IO.File.Exists(path)) { System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(int[])); System.IO.StreamReader sr = new System.IO.StreamReader(path, new UTF8Encoding(false)); int[] ts = (int[])x.Deserialize(sr); load_time_span = new TimeSpan(ts[0], ts[1], ts[2], ts[3]); sr.Close(); } TimeSpan current_time_span = new TimeSpan(); DateTime original_dt = DateTime.Now; Timer t = new Timer(); t.Tick += new EventHandler((object t_sender, EventArgs t_e) => { current_time_span = (DateTime.Now - original_dt); int h = (load_time_span + current_time_span).Days * 24 + (load_time_span + current_time_span).Hours; label1.Text = h.ToString() + "時間" + (load_time_span + current_time_span).ToString(@"mm\分ss\秒"); }); t.Start(); this.FormClosed += new FormClosedEventHandler((object sender, FormClosedEventArgs e) => { System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(int[])); System.IO.StreamWriter sw = new System.IO.StreamWriter(path, false, new UTF8Encoding(false)); x.Serialize(sw, new int[] { (int)(load_time_span + current_time_span).Days, (int)(load_time_span + current_time_span).Hours, (int)(load_time_span + current_time_span).Minutes, (int)(load_time_span + current_time_span).Seconds }); sw.Close(); }); } } } |