The Analog Clock Control is created with VB.Net Control Library. It is a clock control which has almost all the functionality that this type of control can have and it is fully customizable. Since this is a control library you can use it in C++, C#, J# and VB.Net projects in .Net environment.
I created this control to help someone in VBForums. At the beginning it was very simple clock but then it became somewhat advanced after I added many properties, events and a functionality that made the clock very flexible.
The
Analog Clock control is a Windows
UserControl. Almost all the elements of the clock (except the
Symbols) have been constructed (the core of the element) with
GraphicsPath object. They contain a member variable Base-Path which is a
GraphicsPath of the element. These Base-Paths are used differently for each type of element. For example, each marker Base-Path represents a
GraphicsPath object constructed at 12 hour position and than rotated using a
Matrix object. Since it is rotated only once, there is no need for any other helper objects. The clock's hands have two member variables of type
GraphicsPath; Base-Path and Shift-Path. The Base-Path of the hands are always positioned at 12 o' clock, and only reshaped if the element's shape (width, length, or style) is modified. On the other hand, the Shift-Path is the actual
GrapicsPath of the hand at any given time. Shift-Path is the copy of the rotated Base-Path.
The clock control's elements are painted layer by layer. Here is the order of the elements' paint that also raises the events.
- Background Paint
- Small-Markers Paint
- Big-Markers Paint
- Symbols Paint
- Hour-Hand Paint
- Minute-Hand Paint
- Second-Hand Paint
- Center-Point Paint
- Frame Paint
- Paint
My objective was to create an analog clock control whose elements could be customizable at design and run time. Although every element supports at leas two styles, a custom style (a
GraphicsPath object) can be provided by the developer in the elements'
CustomElementRequest event handler. Moreover, the elements can be filled with different brushes at run time in the elements'
ElementPainting event handler. All is needed a brush to be assigned to the
AnalogClock.PaintEventArgs argument in the event handler. I have tried to make the Analog Clock control as a true control and I think I have succeeded.
vb Code:
Private Sub Clock1_HandsPainting(ByVal sender As Object, ByVal e As AnalogClock.PaintEventArgs) _
Handles Clock1.SecondHandPainting, Clock1.MinuteHandPainting, Clock1.HourHandPainting
'Get reference to the hand object.
Dim h As Hand = DirectCast(sender, Hand)
'Make sure the hand's graphics path contains more than 2 points.
If h.Path.PointCount > 2 Then
'Make the hand gradient.
Dim br As New Drawing2D.PathGradientBrush(h.Path)
br.CenterColor = Color.White
br.SurroundColors = New Color() {h.Color}
e.Brush = br
br.Dispose()
End If
End Sub
In some situations you may need to set the clock time to a start time. For this we need to do a little bit calculation and set the UtcOffset property accordingly. Here how we can do that:
vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cdt As DateTime = CDate("#17:20:35#") 'Some custom start time for the clock.
Dim utcDt As DateTime = DateTime.UtcNow 'The current UTC dateTime. This is needed beacuse the clock internal works with UTC dateTime.
Me.Clock1.UtcOffset = New TimeSpan(0, cdt.Hour - utcDt.Hour, _
cdt.Minute - utcDt.Minute, cdt.Second - utcDt.Second)
End Sub
Some time people ask me why the clock is one hour off after daylight savings or how can we be sure that clock shows the correct time always.
vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set UTC offset to the system utc offset when the application loads
Me.Clock1.UtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)
End Sub
Private Sub Clock1_TimeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clock1.TimeChanged
'Set UTC offset to the system utc offset every time clock time changes. If the property has the same value it will do nothing.
Me.Clock1.UtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)
End Sub