Results 1 to 6 of 6

Thread: Analog Clock Control

  1. #1

    Thread Starter
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Analog Clock Control

    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:
    1. Private Sub Clock1_HandsPainting(ByVal sender As Object, ByVal e As AnalogClock.PaintEventArgs) _
    2. Handles Clock1.SecondHandPainting, Clock1.MinuteHandPainting, Clock1.HourHandPainting
    3.  
    4.     'Get reference to the hand object.
    5.     Dim h As Hand = DirectCast(sender, Hand)
    6.     'Make sure the hand's graphics path contains more than 2 points.
    7.     If h.Path.PointCount > 2 Then
    8.         'Make the hand gradient.
    9.         Dim br As New Drawing2D.PathGradientBrush(h.Path)
    10.         br.CenterColor = Color.White
    11.         br.SurroundColors = New Color() {h.Color}
    12.         e.Brush = br
    13.         br.Dispose()
    14.     End If
    15.  
    16. 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:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Dim cdt As DateTime = CDate("#17:20:35#")  'Some custom start time for the clock.
    3.     Dim utcDt As DateTime = DateTime.UtcNow  'The current UTC dateTime. This is needed beacuse the clock internal works with UTC dateTime.
    4.     Me.Clock1.UtcOffset = New TimeSpan(0, cdt.Hour - utcDt.Hour, _
    5.                                            cdt.Minute - utcDt.Minute, cdt.Second - utcDt.Second)
    6. 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:
    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     'Set UTC offset to the system utc offset when the application loads
    3.     Me.Clock1.UtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)
    4. End Sub
    5.  
    6. Private Sub Clock1_TimeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clock1.TimeChanged
    7.     'Set UTC offset to the system utc offset every time clock time changes. If the property has the same value it will do nothing.
    8.     Me.Clock1.UtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)
    9. End Sub

    Download the latest version here
    Attached Files Attached Files

  2. #2
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Analog Clock Control

    It would be nice if it was a VB6 User Control (Nothing has to be registered).

    From my quick look, it -
    - does not appear to be UC
    - requires registering the DLL
    - Only for VB.NET

    Ah, such is life!

    Rob
    Rob C

  3. #3
    New Member
    Join Date
    Jan 2010
    Posts
    1

    Re: Analog Clock Control

    Thank You Very Much Realy Good Work

  4. #4
    Member
    Join Date
    Dec 2008
    Posts
    58

    Re: Analog Clock Control

    Great one, thank you.

    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

  5. #5
    New Member
    Join Date
    May 2012
    Posts
    10

    Re: Analog Clock Control

    Greetings,
    Last edited by dgvb; May 3rd, 2012 at 07:01 AM.

  6. #6
    Junior Member
    Join Date
    Oct 2005
    Posts
    16

    Re: Analog Clock Control

    This post is old but very good one.. last edited Feb 27th

    Did you found a solution for the 4.0 crippling/freeze issue ? I'm using VB 2010 express

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width