10,42 → 10,106 |
{ |
public partial class MainForm : Form |
{ |
private int time = 0; |
public const string REGISTRY_KEY = @"Software\Anatoli Klassen\akStopWatch"; |
|
private int count = 3; |
public int Count { |
get { return count; } |
set { |
count = value; |
|
if(count < 1) count = 1; |
if(count > 10) count = 10; |
} |
} |
|
private TimeState[] times; |
|
public MainForm() |
{ |
InitializeComponent(); |
} |
|
private void MainForm_Click(object sender, EventArgs e) |
{ |
Start(); |
} |
private void LoadSettings() |
{ |
Microsoft.Win32.RegistryKey subkey = null; |
try |
{ |
subkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(REGISTRY_KEY); |
if(subkey != null) |
{ |
Count = (int)subkey.GetValue("Count", Count); |
} |
} |
finally |
{ |
if(subkey != null) |
subkey.Close(); |
} |
} |
|
private void SaveSettings() |
{ |
Microsoft.Win32.RegistryKey subkey = null; |
try |
{ |
subkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(REGISTRY_KEY); |
|
subkey.SetValue("Count", Count); |
} |
finally |
{ |
if(subkey != null) |
subkey.Close(); |
} |
} |
|
private void MainForm_KeyPress(object sender, KeyPressEventArgs e) |
{ |
Start(); |
int n = int.Parse(e.KeyChar.ToString()); |
|
if(n >= 0 && n < times.Length) |
{ |
times[n].Reset(DateTime.Now); |
ShowTime(); |
} |
} |
|
private void MainForm_Load(object sender, EventArgs e) |
{ |
Start(); |
LoadSettings(); |
Start(Count); |
} |
|
private void MainForm_Resize(object sender, EventArgs e) |
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) |
{ |
SaveSettings(); |
} |
|
private void timer_Tick(object sender, EventArgs e) |
{ |
++time; |
ShowTime(); |
DateTime now = DateTime.Now; |
bool isChanged = false; |
foreach(TimeState t in times) |
{ |
isChanged |= t.IsChanged(now); |
} |
|
if(isChanged) { |
ShowTime(); |
} |
} |
|
private void Start() |
private void Start(int n) |
{ |
timer.Stop(); |
time = 0; |
times = new TimeState[n]; |
|
DateTime now = DateTime.Now; |
for(int i = 0; i < times.Length; ++i) |
{ |
times[i] = new TimeState(now); |
} |
|
ShowTime(); |
timer.Start(); |
} |
59,23 → 123,35 |
{ |
try |
{ |
Graphics g = e.Graphics; |
SolidBrush brush = new SolidBrush(Color.Black); |
Graphics g = e.Graphics; |
SolidBrush brushBig = new SolidBrush(Color.Black); |
SolidBrush brushSmall = new SolidBrush(Color.Red); |
|
g.FillRectangle(SystemBrushes.Control, g.Clip.GetBounds(g)); |
|
int hour = time / 60 / 60; |
int min = time / 60 % 60; |
int sec = time % 60 % 60; |
string str = string.Format("{0:00}:{1:00}:{2:00}", hour, min, sec); |
float bigToSmall = 2.0f; |
string sampleBig = string.Format("{0:00}:{1:00}:{2:00}", 0, 0, 0); |
string sampleSmall = string.Format("{0:0}", 0); |
SizeF sizeBig = g.MeasureString(sampleBig, timeLabel.Font); |
SizeF sizeSmall = g.MeasureString(sampleSmall, timeLabel.Font); |
float k = Math.Min(timeLabel.Width / (sizeBig.Width + sizeSmall.Width / bigToSmall), |
timeLabel.Height / (sizeBig.Height * times.Length)); |
float dx = sizeSmall.Width * k / bigToSmall; |
float x = (timeLabel.Width - (sizeBig.Width + sizeSmall.Width / bigToSmall) * k) / 2; |
float dy = sizeBig.Height * k; |
float y = (timeLabel.Height - sizeBig.Height * times.Length * k) / 2; |
Font fontBig = new Font(timeLabel.Font.FontFamily, timeLabel.Font.Size * k, timeLabel.Font.Style); |
Font fontSmall = new Font(timeLabel.Font.FontFamily, timeLabel.Font.Size * k / bigToSmall, timeLabel.Font.Style); |
|
SizeF strSize = g.MeasureString(str, timeLabel.Font); |
float k = Math.Min(timeLabel.Width / strSize.Width, timeLabel.Height / strSize.Height); |
float x = (timeLabel.Width - strSize.Width * k) / 2; |
float y = (timeLabel.Height - strSize.Height * k) / 2; |
Font font = new Font(timeLabel.Font.FontFamily, timeLabel.Font.Size * k, timeLabel.Font.Style); |
|
g.DrawString(str, font, brush, x, y); |
DateTime now = DateTime.Now; |
for(int i = 0; i < times.Length; ++i) |
{ |
TimeSpan time = times[i].GetDiff(now); |
string str = string.Format("{0:00}:{1:00}:{2:00}", time.Hours, time.Minutes, time.Seconds); |
|
g.DrawString(i.ToString(), fontSmall, brushSmall, x, y + dy * i); |
g.DrawString(str, fontBig, brushBig, x + dx, y + dy * i); |
} |
} |
catch(Exception ex) |
{ |
82,5 → 158,47 |
Console.WriteLine(ex.Message + "\n" + ex.StackTrace); |
} |
} |
|
private void settingsButton_Click(object sender, EventArgs e) |
{ |
SettingsForm f = new SettingsForm(); |
|
f.countBox.Value = times.Length; |
if(f.ShowDialog(this) == DialogResult.OK) |
{ |
Count = (int)f.countBox.Value; |
Start(Count); |
} |
} |
} |
|
class TimeState |
{ |
private DateTime begin; |
private DateTime last; |
|
public TimeState(DateTime now) |
{ |
Reset(now); |
} |
|
public void Reset(DateTime now) |
{ |
begin = now; |
last = DateTime.MinValue; |
} |
|
public bool IsChanged(DateTime now) |
{ |
TimeSpan diff = now.Subtract(last); |
|
return (diff.TotalSeconds > 0); |
} |
|
public TimeSpan GetDiff(DateTime now) |
{ |
last = now; |
return now.Subtract(begin); |
} |
} |
} |