-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clock.cs
234 lines (197 loc) · 7.11 KB
/
Clock.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace FloatingClock
{
/// <summary>
/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
///
/// Step 1a) Using this custom control in a XAML file that exists in the current project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:FloatingClock"
///
///
/// Step 1b) Using this custom control in a XAML file that exists in a different project.
/// Add this XmlNamespace attribute to the root element of the markup file where it is
/// to be used:
///
/// xmlns:MyNamespace="clr-namespace:FloatingClock;assembly=FloatingClock"
///
/// You will also need to add a project reference from the project where the XAML file lives
/// to this project and Rebuild to avoid compilation errors:
///
/// Right click on the target project in the Solution Explorer and
/// "Add Reference"->"Projects"->[Browse to and select this project]
///
///
/// Step 2)
/// Go ahead and use your control in the XAML file.
///
/// <MyNamespace:Clock/>
///
/// </summary>
public class Clock : Canvas
{
static Clock()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(Clock), new FrameworkPropertyMetadata(typeof(Clock)));
}
public Clock()
{
timer.Elapsed += new ElapsedEventHandler(Handle_Timer);
}
public void Start()
{
Handle_Timer(null, null);
timer.Start();
}
public void Stop()
{
timer.Stop();
}
public Brush FaceColor = Brushes.White;
public Brush TickColor = Brushes.DarkGray;
public Brush HandsColor = Brushes.Black;
private double size = 0;
private const double hourWidth = 2;
private const double minuteWidth = 2;
private const double secondWidth = 0.5;
private const double hourScale = 0.3;
private const double minuteScale = 0.45;
private const double secondScale = 0.45;
private const double tailScale = 0.075;
private UIElement hourHand;
private UIElement minuteHand;
private UIElement secondHand;
private Label date;
private Label day;
private readonly Timer timer = new Timer();
private void Handle_Timer(object sender, System.Timers.ElapsedEventArgs e)
{
Dispatcher.Invoke(() =>
{
UpdateTime();
});
timer.Interval = 1000 - DateTime.Now.Millisecond;
}
private void UpdateTime() {
if (hourHand == null || minuteHand == null || secondHand == null)
{
return;
}
var time = DateTime.Now;
var hours = Math.Floor(time.TimeOfDay.TotalSeconds) % (12 * 60 * 60) / (60 * 60);
var minutes = Math.Floor(time.TimeOfDay.TotalSeconds) % (60 * 60) / 60;
var seconds = Math.Floor(time.TimeOfDay.TotalSeconds) % 60;
hourHand.RenderTransform = new RotateTransform(hours * 30, hourWidth / 2, size * hourScale);
minuteHand.RenderTransform = new RotateTransform(minutes * 6, minuteWidth / 2, size * minuteScale);
secondHand.RenderTransform = new RotateTransform(seconds * 6, secondWidth / 2, size * secondScale);
date.Content = time;
day.Content = time;
}
private void DrawFace()
{
Children.Add(
new Ellipse
{
Height = size,
Width = size,
Fill = FaceColor,
Stroke = TickColor,
StrokeThickness = 1,
}
);
for (double a = 0; a < 360; a += 6)
{
var height = 5;
var width = 1;
if (a % 30 == 0)
{
height = 10;
width = 3;
}
var tick = new Rectangle
{
Width = width,
Height = height,
Fill = TickColor,
Stroke = TickColor,
RenderTransform = new RotateTransform(a, width / 2, size / 2),
};
Children.Add(tick);
SetTop(tick, 0);
SetLeft(tick, size / 2 - width / 2);
}
}
private void DrawDate()
{
date = new Label
{
Content = DateTime.Now,
ContentStringFormat = "MMM d",
};
Children.Add(date);
date.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
SetLeft(date, (size - date.DesiredSize.Width) / 2);
SetTop(date, size / 3 - date.DesiredSize.Height / 2);
day = new Label
{
Content = DateTime.Now,
ContentStringFormat = "dddd",
};
Children.Add(day);
day.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
SetLeft(day, (size - day.DesiredSize.Width) / 2);
SetTop(day, size / 3 * 2 - day.DesiredSize.Height / 2);
}
private void DrawHands()
{
hourHand = new Rectangle
{
Width = hourWidth,
Height = size * hourScale + size * tailScale,
Fill = HandsColor,
};
Canvas.SetTop(hourHand, size / 2 - (size * hourScale));
Canvas.SetLeft(hourHand, (size - hourWidth) / 2);
minuteHand = new Rectangle
{
Width = minuteWidth,
Height = size * minuteScale + size * tailScale,
Fill = HandsColor,
};
Canvas.SetTop(minuteHand, size / 2 - (size * minuteScale));
Canvas.SetLeft(minuteHand, (size - minuteWidth) / 2);
secondHand = new Rectangle
{
Width = secondWidth,
Height = size * secondScale + size * tailScale,
Fill = HandsColor,
};
Canvas.SetTop(secondHand, size / 2 - (size * secondScale));
Canvas.SetLeft(secondHand, (size - secondWidth) / 2);
Children.Add(hourHand);
Children.Add(minuteHand);
Children.Add(secondHand);
}
protected override Size ArrangeOverride(Size arrangeBounds)
{
var newSize = Math.Min(arrangeBounds.Width, arrangeBounds.Height);
if (size != newSize)
{
size = newSize;
Children.Clear();
DrawFace();
DrawHands();
DrawDate();
UpdateTime();
}
return base.ArrangeOverride(arrangeBounds);
}
}
}