Click here to Skip to main content
6,630,586 members and growing! (17,034 online)
Email Password   helpLost your password?
General Programming » Date and Time » General     Advanced License: The Code Project Open License (CPOL)

Analog Clock Control

By VBDT

The analog clock control is a control that has almost all the functionality that a clock control can have, and it is fully modifiable.
VB 8.0, Windows, .NET 2.0, GDI+, VS2005, Dev
Version:2 (See All)
Posted:28 Jun 2007
Updated:6 Aug 2009
Views:66,938
Bookmarked:79 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
39 votes for this article.
Popularity: 6.47 Rating: 4.07 out of 5

1
5 votes, 13.2%
2
4 votes, 10.5%
3
10 votes, 26.3%
4
19 votes, 50.0%
5

Introduction

Here, an analog clock has been created with a VB.NET control library. It is a clock control which has almost all the functionality that this type of controls 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 the .NET environment.

I created this control to help someone in VB Forums. At the beginning, it was a very simple clock, but then it became somewhat advanced after I added many properties, events, and a functionality that makes the clock very flexible.

ClocksImg.png

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.

Background

The clock control is a Windows UserControl. Almost all the elements of the clock have been constructed (the core of the element) with the GraphicsPath data type. They contain a member variable Base-Path which is a GraphicsPath of the element. These Base-Paths are used differently for each element. For example, the marker's Base-Path represents a GraphicsPath constructed at 12 hour position and than rotated using a Matrix object. Since it is rotated only once, there is no need fore 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.

Using the code

Although you can do almost anything with this control, I will show you only how to paint the hour-hand of the clock with a PathGradientBrush. Note, in this fashion, you can paint the elements with any brush.

It is very nice to see the clock hand's gradient, so here is how you can do that. Basically, you set the Brush property of the hands to a newly created gradient brush object in the hands' paint event.

Private Sub Clock1_HourHandPainting(ByVal sender As Object, _
        ByVal e As AnalogClock.PaintEventArgs) Handles Clock1.HourHandPainting
    'Make sure the hour hand's graphics path contains more than 2 points.
    If Me.Clock1.HourHand.Path.PointCount > 2 Then
        'Make the hour hand gradient
        Dim br As New Drawing2D.PathGradientBrush(Me.Clock1.HourHand.Path)
        br.CenterColor = Color.White
        br.SurroundColors = New Color() {Me.Clock1.HourHand.Color}
        e.Brush = br
        br.Dispose()
    End If
End Sub

For more examples, check the demo project.

Analog clock control information

History

  • 1.0.0.0 -- Original version posted.
  • 1.0.2813.38699 -- Some minor changes to the number and marker styles. Also. the UTC offset values range is changed that accepts values from -23:00:00 to 23:00:00.
  • 1.0.2847.27310 -- This update adds a property that can be used to get/set the clock's frame line color. Also, there are some corrections to the clock. The update is recommended.
  • 1.0.2917.23783 -- A small bug fix.
  • 1.0.3002.36694 -- Added minute marker width property.
  • 1.0.3223.6598 -- This update includes major changes to the implementation of the clock. If you liked the previous versions of the analog clock, then you will love this one. In this version, you can modify every element of the clock at design and run time. There are many good stuff I would like to mention, but it is better you try it yourself. I will remind you to check all the properties of the clock, in particular, the Element category in the Properties window which contains the clock's hands, markers etc. I hope you will like it.
  • 1.5.0.2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

VBDT


Member

Occupation: Software Developer (Senior)
Location: United States United States

Other popular Date and Time articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
QuestionThis is working with which version of .net? [modified] Pinmemberunmesha3:53 27 Oct '09  
GeneralIs possible make in c# Pinmemberprof300022:27 7 Sep '09  
GeneralRe: Is possible make in c# PinmemberVBDT10:09 8 Sep '09  
GeneralStops cursor keys working? PinmemberRoger CO3:06 16 Jul '09  
GeneralRe: Stops cursor keys working? PinmemberVBDT8:34 6 Aug '09  
GeneralUnfair PinmemberRunnerDude7:51 29 Mar '08  
GeneralRe: Unfair PinmemberVBDT10:42 16 Nov '08  
GeneralNo code! PinmemberAChaves8:53 25 Mar '08  
GeneralRe: No code! PinmemberVBDT10:41 16 Nov '08  
GeneralSetting the Time? PinmemberJoe Sweeney17:39 24 Mar '08  
QuestionRe: Setting the Time? PinmemberNeoTheKiller6:26 13 Oct '08  
AnswerRe: Setting the Time? [modified] PinmemberVBDT13:57 13 Oct '08  
AnswerRe: Setting the Time? PinmemberVBDT1:54 28 Oct '08  
GeneralRe: Setting the Time? PinmemberMember 225477112:54 3 Aug '09  
GeneralRe: Setting the Time? PinmemberVBDT13:35 3 Aug '09  
GeneralRe: Setting the Time? Pinmembersupercat95:52 6 Aug '09  
GeneralControl running in VS2008 Pinmemberpeterennis8:41 12 Mar '08  
GeneralRe: Control running in VS2008 [modified] PinmemberVBDT9:48 12 Mar '08  
AnswerRe: Control running in VS2008 Pinmemberpeterennis7:39 14 Mar '08  
Generalcannot see component in toolbar? Pinmemberneoice8:17 3 Nov '07  
GeneralRe: cannot see component in toolbar? PinmemberVBDT16:21 5 Nov '07  
GeneralACTIVEX Support Pinmemberpboby1:21 23 Oct '07  
GeneralDemo's Source Pinmembercan kayacan12:17 18 Oct '07  
GeneralRe: Demo's Source PinmemberVBDT19:36 18 Oct '07  
GeneralNice control PinmemberSachin Mangesh Naik4:16 12 Oct '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Aug 2009
Editor: Sean Ewington
Copyright 2007 by VBDT
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project