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.
You can do more exciting stuff with this clock control. My Desktop Alarm Clock that uses the control. All these styles are easy to create. You simply set the clock's background image and set its elements' properties. And yes, you need to fill the elements with gradient brushes.
Background:
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.
Update History:
Assembly Version: 1.5.0.0
File Version: 1.0.0.2
Note this version of the control is not compatible to the previous versions. There are many internal changes including some object names.
There was a design time issue that is fixed in this version. After making some changes to the elements properties in the Collection Editor and clicking the Cancel button, the Collection Editor was behaving abnormal and the objects were becoming uncontrollable. This is because after clicking the cancel button the Collection Editor was returning new element objects so the attached event handlers were getting lost.
Overall, this version fixes all design time issues and the code is optimized.
Assembly Version: 1.5.0.2
File Version: 1.0.0.4
Some bug fixes and improvements (update is recommended).
Added one mere Center-point style (Octagon).
Assembly Version: 1.5.0.3
File Version: 1.0.0.5
A bug fix for the elements' Tag property at design time (update is recommended).
Please feel free to report bugs or comment!
Using the code:
Although, you can do almost anything with this control, I will show you how to paint the Hour, Minute and Second hands of the clock with PathGradientBrush. Note in this fashion you can paint the elements with any brush.
It is very nice to see the clock's hands gradient so here is how you can do it. Basically, you set the brush property of the hands to a newly created gradient brush object in the hands' painting events. For more examples check the demo project.
vb Code:
Private Sub Clock1_HandsPainting(ByVal sender As Object, ByVal e As AnalogClock.PaintEventArgs) _
'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, _
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.
Well, if this is the case with you than you need to make sure that clock’s UtcOffset is always accurate. This is how you can do it:
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
May I suggest an update to users, another solution in order not to set manually UTCoffset...
Place next function in your solution:
Code:
Public Function GetUTCOffset() As TimeSpan
Dim baseUTC As DateTime = DateTime.Now
Dim localZone As TimeZone = TimeZone.CurrentTimeZone
Dim localTime As DateTime = localZone.ToLocalTime(baseUTC)
Dim localOffset As TimeSpan = localZone.GetUtcOffset(localTime)
Return localOffset
End Function
at form load event, use this:
Code:
Clock1.UtcOffset = GetUTCOffset()
And another trick i like alot, at double click event of the control:
Code:
Private Sub Clock1_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles Clock1.DoubleClick
Shell("rundll32.exe shell32.dll,Control_RunDLL timedate.cpl,,0")
End Sub