Analog Clock Control

The Analog Clock Control was 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.

Some Screen Shots


Background:

The Analog Clock control is a Windows User Control. 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 the 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 object 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.

Some VB.Net Code Examples

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.

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:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Some custom start time for the clock.
    Dim cdt As DateTime = CDate("#17:20:35#")
    'Get the current UTC dateTime. This is needed beacuse the clock internal works with UTC dateTime.
    Dim utcDt As DateTime = DateTime.UtcNow
    'Set clock's UtcOffset property.
    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. 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:

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 

Adding the Component

To use the control, you just need to add it into the VS.NET Toolbox. Right click in the Toolbox area, and select the "Choose Items" menu item. It will then open the "Choose Toolbox Item" window. You go to the directory that contains the "AnalogClockLib.dll" file and select it, then click the OK button. This will add the control onto the Toolbox.

Finally, you drag and drop the control onto your forms. Also, in order to see the descriptions of the properties or methods in the Code Designer, you should copy the "AnalogClockLib.xml" file into your project's folder.

License

Everyone is permitted to copy and use the Analog Clock Control freely for their personal or business needs. There are no restrictions what so ever.

Info

  • Assembly Version: 1.5.0.4
  • File Version: 1.0.0.6
  • Author: Arman Ghazanchyan

System Requirements

  • Windows
  • Dot Net Framework

Downloads



blog comments powered by Disqus