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; using System.IO; namespace Momo { public partial class Momo : Form { private static readonly TimeSpan TIME_TOTAL = TimeSpan.FromHours(7.7 + 0.5); private static readonly String DUMP_FILE = @"kassio"; private DateTime startTime; private Boolean isMouseDown; private Point lastMouseDownPosition; public Momo() { this.startTime = DateTime.Now; InitializeComponent(); AddEvents(this); UpdateMomo(); UpdatePosition(); } private void Momo_Load(object sender, EventArgs e) { if (File.Exists(DUMP_FILE)) { String ticks = File.ReadAllText(DUMP_FILE); DateTime dumpTime = new DateTime(long.Parse(ticks), DateTimeKind.Utc).ToLocalTime(); if(dumpTime.Date.CompareTo(startTime.Date) == 0) { string text = "A current dump date was found.\n\n" + "Dump Date:\t"+dumpTime+"\n" + "Start Date:\t"+startTime+"\n\n" + "Do you want to use the dump date?"; string caption = "Dump Date Found"; DialogResult result = MessageBox.Show(text, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Console.WriteLine("Old start time " + startTime); this.startTime = dumpTime; Console.WriteLine("Using dump time as new start time " + startTime); } else { WriteDumpTime(); } } } else { WriteDumpTime(); } Timer clockTimer = new Timer { Interval = 200 }; clockTimer.Tick += ClockTimer_Tick; clockTimer.Start(); } private void WriteDumpTime() { Console.WriteLine("Writing new dump time " + startTime); if (File.Exists(DUMP_FILE)) { if (File.Exists(DUMP_FILE + ".bak")) File.Delete(DUMP_FILE + ".bak"); File.Move(DUMP_FILE, DUMP_FILE + ".bak"); } File.WriteAllText(@"kassio", startTime.ToUniversalTime().Ticks.ToString()); } private void AddEvents(Control control) { control.MouseDown += Momo_MouseDown_Drag; control.MouseUp += Momo_MouseUp_Drag; control.MouseMove += Momo_MouseMove_Drag; control.MouseClick += Momo_MouseClick_Drag; control.MouseDoubleClick += Momo_DoubleClick_Close; if (control.Controls.Count == 0) return; foreach (Control childControl in control.Controls) { AddEvents(childControl); } } private void ClockTimer_Tick(object sender, EventArgs e) { UpdateMomo(); } private void UpdateMomo() { UpdateDate(); UpdateClock(); } private void UpdateDate() { this.lblDate.Text = DateTime.Now.ToString("dd.MM.yyyy"); } private void UpdateClock() { TimeSpan timeSpent = DateTime.Now.Subtract(startTime); TimeSpan timeLeft = TIME_TOTAL.Subtract(timeSpent); DateTime endTime = startTime.Add(TIME_TOTAL); this.lblClock.Text = DateTime.Now.ToString("HH:mm") + " " + (timeLeft.CompareTo(TimeSpan.Zero) < 0 ? "-" : "") + timeLeft.ToString("hh\\:mm") + " " + endTime.ToString("HH:mm"); } private void UpdatePosition() { this.Location = new Point(Screen.PrimaryScreen.Bounds.Right - this.Width, Screen.PrimaryScreen.WorkingArea.Bottom - this.Height); } private void Momo_MouseDown_Drag(object sender, MouseEventArgs e) { this.isMouseDown = true; this.lastMouseDownPosition = e.Location; } private void Momo_MouseMove_Drag(object sender, MouseEventArgs e) { if (isMouseDown) { this.Location = new Point( (this.Location.X - lastMouseDownPosition.X) + e.X, (this.Location.Y - lastMouseDownPosition.Y) + e.Y); this.Update(); } } private void Momo_MouseUp_Drag(object sender, MouseEventArgs e) { this.isMouseDown = false; } private void Momo_MouseClick_Drag(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Right) { this.UpdatePosition(); } } private void Momo_DoubleClick_Close(object sender, EventArgs e) { this.Close(); } } }