-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalogClock.cs
190 lines (186 loc) · 7.51 KB
/
AnalogClock.cs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Threading;
namespace AnalogClocks
{
public partial class AnalogClock : UserControl
{
private PointF center;
private float diam;
private Color minColor = Color.Red, secColor = Color.Blue, hourColor = Color.Green;
private Color colorDigFrame = Color.Navy, colorDigDial = Color.Purple, colorDigNum = Color.Silver;
private Color colorDial = Color.Gray, colorFrame = Color.Black;
private const double degreeToRad = Math.PI / 180;
private float lenHour, lenMin, lenSec;
private bool hourAvail = true, minAvail = true, secAvail = true;
private System.Windows.Forms.Timer timer;
private DateTime date;
private Pen penHour,penMin, penSec;
private bool manual = false;
public AnalogClock()
{
InitializeComponent();
timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler((object s, EventArgs e) => { if (!manual) Refresh();});
timer.Start();
penHour = new Pen(hourColor);
penMin = new Pen(minColor);
penSec = new Pen(secColor);
penHour.StartCap = LineCap.Round;
penHour.EndCap = LineCap.Triangle;
penMin.StartCap = LineCap.Round;
penMin.EndCap = LineCap.Triangle;
penSec.StartCap = LineCap.RoundAnchor;
penSec.EndCap = LineCap.ArrowAnchor;
}
private void AnalogClock_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
DrawFrame(10, e);
DrawDial(e);
DateTime time = manual ? date : DateTime.Now;
DrawDigit(time, e);
float degrSec = (time.Second % 60) * 6;
float degrMin = (time.Minute + time.Second/60)* 6;
float degrHour = (time.Hour % 12 + time.Minute / 60) * 30;
if(hourAvail)
DrawHour(degrHour, e);
if(minAvail)
DrawMinute(degrMin, e);
if(secAvail)
DrawSecond(degrSec, e);
}
private bool showTime = true;
private void AnalogClock_MouseHover(object sender, EventArgs e)
{
if (!showTime)
return;
showTime = false;
MessageBox.Show(date.ToLongTimeString());
new Thread(() => { Thread.Sleep(3000); showTime = true; }).Start();
}
private void AnalogClock_Load(object sender, EventArgs e)
{
AnalogClock_Resize(sender, e);
}
private void AnalogClock_Resize(object sender, EventArgs e)
{
center = new PointF(Width / 2, Height / 2);
diam = Width < Height ? Width*0.9F : Height*0.9F;
lenSec = diam / 2 * 0.9F;
lenMin = diam / 2 * 0.7F;
lenHour = diam / 2 * 0.6F;
penHour.Width = Height / 10F;
penMin.Width = Height / 14F;
penSec.Width = Height / 20F;
}
private void DrawFrame(float width, PaintEventArgs e)
{
PointF leftCorner = new PointF(center.X - diam/2,center.Y - diam/2);
e.Graphics.DrawEllipse(new Pen(colorFrame, width), leftCorner.X,leftCorner.Y,diam,diam);
}
private void DrawDial(PaintEventArgs e)
{
e.Graphics.FillEllipse(new SolidBrush(colorDial), center.X - diam / 2, center.Y - diam / 2, diam, diam);
}
private void DrawMinute(float degree, PaintEventArgs e)
{
e.Graphics.DrawLine(penMin, center.X, center.Y, center.X + (float)(lenMin * Math.Sin(degree * degreeToRad)), center.Y - (float)(lenMin * Math.Cos(degree * Math.PI / 180)));
}
private void DrawHour (float degree, PaintEventArgs e)
{
e.Graphics.DrawLine(penHour, center.X, center.Y, center.X + (float)(lenHour * Math.Sin(degree * degreeToRad)), center.Y - (float)(lenHour * Math.Cos(degree * degreeToRad)));
}
private void DrawSecond(float degree, PaintEventArgs e)
{
e.Graphics.DrawLine(penSec, center.X, center.Y, center.X + (float)(lenSec * Math.Sin(degree * degreeToRad)), center.Y - (float)(lenSec * Math.Cos(degree * degreeToRad)));
}
private void DrawDigit(DateTime time,PaintEventArgs e)
{
string displTime = "";
displTime += time.Hour >= 10 ? time.Hour.ToString() : "0" + time.Hour.ToString();
displTime += ":";
displTime += time.Minute >= 10 ? time.Minute.ToString() : "0" + time.Minute.ToString();
displTime += ":";
displTime += time.Second >= 10 ? time.Second.ToString() : "0" + time.Second.ToString();
int strLen = displTime.Length;
const float k = 1.3F;
float fontSize = diam * (0.6F / 8) * k;
PointF lCorner = new PointF(center.X - (diam*0.3F), center.Y + (diam*0.2F));
float width = diam*0.61F;
float height = diam/2 * 0.3F;
e.Graphics.FillRectangle(new SolidBrush(colorDigDial), lCorner.X, lCorner.Y, width, height);
e.Graphics.DrawRectangle(new Pen(colorDigFrame,3), lCorner.X, lCorner.Y, width, height);
Font font = new Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Italic);
e.Graphics.DrawString(displTime, font, new SolidBrush(colorDigNum), lCorner);
}
public Color HourColor
{
get { return hourColor; }
set { hourColor = value; penHour.Color = hourColor; Refresh(); }
}
public Color MinuteHour
{
get { return minColor; }
set { minColor = value; penMin.Color = minColor;Refresh(); }
}
public Color SecColor
{
get { return secColor; }
set { secColor = value; penSec.Color = secColor; Refresh(); }
}
public Color DialColor
{
get { return colorDial; }
set { colorDial = value;Refresh(); }
}
public Color FrameColor
{
get { return colorFrame; }
set { colorFrame = value; Refresh(); }
}
public Color DigitalFrame
{
get { return colorDigFrame; }
set { colorDigFrame = value;Refresh(); }
}
public Color DigitalDial
{
get { return colorDigDial; }
set { colorDigDial = value; Refresh(); }
}
public Color DigitalTime
{
get { return colorDigNum; }
set { colorDigNum = value; Refresh(); }
}
public bool EnableHour
{
get { return hourAvail; }
set { hourAvail = value; Refresh(); }
}
public bool EnableMinute
{
get { return minAvail; }
set { minAvail = value; Refresh(); }
}
public bool EnableSec
{
get { return secAvail; }
set { secAvail = value; Refresh(); }
}
public bool Manual
{
get { return manual; }
set { manual = value; this.Refresh(); }
}
public DateTime Date
{
get { return date; }
set { date = value;if (manual) Refresh(); }
}
}
}