![]() |
General Programming »
Date and Time »
General
Advanced
License: The Code Project Open License (CPOL)
Analog Clock ControlBy VBDTThe 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
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.

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.
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.
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.
General
News
Question
Answer
Joke
Rant
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 |