Clock
An analogue clock face for Windows Forms – a live wall clock, a world clock for another time zone, or a fixed time shown as a picture – drawn entirely in code, so it is sharp at any size and display scale.
public class Clock : DoubleBufferedControlBase
Inherits from / implements: DoubleBufferedControlBase
Remarks
What it shows
By default the clock shows the computer's time and redraws once a second. Set
TimeZoneId to show the time in another zone, daylight saving included, or
StaticTime to stop the hands at a given time – for an appointment, a time stamp or a
"next collection" notice. Time says which time is on the face.
The hands are drawn from the time at each redraw and do not sweep: the second hand moves once per
RefreshInterval, the minute hand moves on the minute, and the hour hand moves half a degree a
minute.
The parts of the face
- The border (
ShouldDrawBorder,BorderThickness,BorderStartColor) and an optional glass shine on it (ShouldDrawGlassEffectOnBorder). - The face (
BackgroundStartColor,BackgroundEndColor) and an optional glass shine across its top (ShouldDrawGlassEffect). - The twelve hour labels (
ShouldDrawNumerals,TimeRepresentationMode) and the minute marks between them (ShouldDrawMinuteIndicatorVisual). - The hub (
ShouldDrawInnerRing) and the three hands, each a two-colour gradient with its own width. - A soft drop shadow under the whole clock (
ShouldDropShadowBeEnabled) and, optionally, under each hand (ShouldDrawDropShadowForTimeHandles).
Size and scale
Keep the control square: the face fills the client area, and the minute marks are drawn only when it is a circle. Widths and thicknesses are in logical pixels (1/96 inch) and are scaled for the display the clock is on. The control's corners are transparent, so the parent's background shows round the face.
Examples
A world clock for the London office beside the local one, with Roman numerals and a glass face.
using System.Drawing;
using System.Windows.Forms;
using Binarymission.WinForms.Controls.TimeControls;
var london = new Clock
{
Location = new Point(16, 16),
Size = new Size(200, 200),
TimeZoneId = "GMT Standard Time",
TimeRepresentationMode = TimeRepresentationMode.RomanNumeral,
BackgroundStartColor = Color.MidnightBlue,
BackgroundEndColor = Color.SteelBlue,
BorderThickness = 10,
ShouldDrawGlassEffect = true,
SecondHandStartColor = Color.OrangeRed,
SecondHandEndColor = Color.Gold
};
var caption = new Label { Location = new Point(16, 222), AutoSize = true, Text = "London" };
london.DoubleClick += (sender, e) => caption.Text = "London: " + london.Time.ToString("HH:mm");
form.Controls.Add(london);
form.Controls.Add(caption);Members at a glance
| Constructors | |
|---|---|
Clock | Initialises a clock that shows the computer's time, redrawn every second, with a brown face, a black 16-pixel border, white Arabic numerals and a drop shadow. |
| Properties | |
BackgroundEndColor | Gets or sets the colour the face's gradient ends in. |
BackgroundStartColor | Gets or sets the colour the face's gradient starts from. |
BorderEndColor | Gets or sets the colour the border ring's gradient ends in. |
BorderGlassEndColor | Gets or sets the colour the border's glass highlight fades to. |
BorderGlassRenderingAlphaComponent | Gets or sets how strong the border's glass highlight is. |
BorderGlassStartColor | Gets or sets the colour of the border's glass highlight where it is strongest. |
BorderStartColor | Gets or sets the colour the border ring's gradient starts from. |
BorderThickness | Gets or sets the width of the ring round the face. |
DropShadowColor | Gets or sets the colour of the shadow under the clock, which fades out from it. |
GlassEndColor | Gets or sets the colour the face's glass shine fades to at the bottom. |
GlassRenderingAlphaComponent | Gets or sets how strong the face's glass shine is. |
GlassStartColor | Gets or sets the colour at the top of the face's glass shine. |
GradientDrawingAngleForCircularArtefacts | Gets or sets the direction of the colour gradients on the border, the face and the hub disc. |
GradientDrawingAngleForTimeNeedles | Gets or sets the direction of the colour gradients on the three hands. |
HourHandEndColor | Gets or sets the colour the hour hand's gradient ends in. |
HourHandStartColor | Gets or sets the colour the hour hand's gradient starts from. |
HourNeedleWidth | Gets or sets how thick the hour hand is. |
InnerRingEndColor | Gets or sets the colour the hub disc's gradient ends in. |
InnerRingShapeBounds | Gets or sets the size and position of the hub disc at the centre of the face, measured from the centre. |
InnerRingStartColor | Gets or sets the colour the hub disc's gradient starts from. |
MaxBorderThickness | Gets or sets the largest value BorderThickness accepts. |
MinuteHandEndColor | Gets or sets the colour the minute hand's gradient ends in. |
MinuteHandStartColor | Gets or sets the colour the minute hand's gradient starts from. |
MinuteIndicatorVisual | Gets or sets the text drawn as each minute mark. |
MinuteIndicatorVisualColor | Gets or sets the colour of the minute marks. |
MinuteIndicatorVisualFont | Gets or sets the font the minute marks are drawn in. |
MinuteNeedleWidth | Gets or sets how thick the minute hand is. |
NumeralsColor | Gets or sets the colour of the hour labels. |
NumeralsFont | Gets or sets the font of the hour labels. |
RefreshInterval | Gets or sets how often the clock redraws itself, which is how often the second hand moves. |
SecondHandEndColor | Gets or sets the colour the second hand's gradient ends in. |
SecondHandStartColor | Gets or sets the colour the second hand's gradient starts from. |
SecondNeedleWidth | Gets or sets how thick the second hand is. |
ShouldDrawBorder | Gets or sets whether the ring round the face is drawn. |
ShouldDrawDropShadowForTimeHandles | Gets or sets whether each hand casts a shadow onto the face, offset down and to the right. |
ShouldDrawGlassEffect | Gets or sets whether a glass shine is drawn across the upper part of the face. |
ShouldDrawGlassEffectOnBorder | Gets or sets whether a thin glass highlight is drawn on the border ring. |
ShouldDrawInnerRing | Gets or sets whether a hub disc is drawn at the centre of the face, under the hands. |
ShouldDrawMinuteIndicatorVisual | Gets or sets whether a mark is drawn at each minute between the hour labels. |
ShouldDrawNumerals | Gets or sets whether the twelve hour labels are drawn round the face. |
ShouldDropShadowBeEnabled | Gets or sets whether a soft shadow is drawn under the clock, down and to the right. |
StaticTime | Gets or sets a fixed time for the hands to show instead of the running time. |
Time | Gets the time the clock shows: StaticTime when it is set, otherwise the time now in
TimeZoneId's zone, or on this computer when that is empty. |
TimeRepresentationMode | Gets or sets how the twelve hour labels are written: numbers, Roman numerals or labels of your own. |
TimeZoneId | Gets or sets the Windows time zone whose time the clock shows, such as "Eastern Standard Time" or
"Tokyo Standard Time". |
| Methods | |
Dispose | Stops the redraw timer and releases it and the label brushes. |
OnPaint | Draws the clock: shadow, border, face, labels, hub and hands, for Time. |
Restart | Replaces the timer that redraws the clock with a new one, so the next redraw is one
RefreshInterval from now. |
Constructors
Clock
Initialises a clock that shows the computer's time, redrawn every second, with a brown face, a black 16-pixel border, white Arabic numerals and a drop shadow.
Remarks
The redraw timer starts here, so the clock is live as soon as it is on a form.
Properties
BackgroundEndColor
Gets or sets the colour the face's gradient ends in.
- Value
- The default is
Color.RosyBrown.
See also
BackgroundStartColor
Gets or sets the colour the face's gradient starts from.
- Value
- The default is
Color.Brown.
Remarks
The face blends from this colour to BackgroundEndColor at
GradientDrawingAngleForCircularArtefacts. Set both to one colour for a plain face.
BorderEndColor
Gets or sets the colour the border ring's gradient ends in.
- Value
- The default is
Color.Black.
BorderGlassEndColor
Gets or sets the colour the border's glass highlight fades to.
- Value
- The default is
Color.Transparent.
BorderGlassRenderingAlphaComponent
Gets or sets how strong the border's glass highlight is.
- Value
- The opacity of
BorderGlassStartColor, from 28 (faint) to 128 (strong); a value set outside that range is replaced by 60. The default is 60.
See also
BorderGlassStartColor
Gets or sets the colour of the border's glass highlight where it is strongest.
- Value
- The default is
Color.White. Its alpha is replaced byBorderGlassRenderingAlphaComponent.
BorderStartColor
Gets or sets the colour the border ring's gradient starts from.
- Value
- The default is
Color.Black.
Remarks
The ring blends to BorderEndColor at GradientDrawingAngleForCircularArtefacts;
two shades of one metal colour give a bezel.
BorderThickness
Gets or sets the width of the ring round the face.
- Value
- In logical pixels. The default is 16. A value above
MaxBorderThicknessis reduced to it.
Remarks
The face, numerals and hands are fitted inside the ring, so a thicker border makes them smaller
rather than covering them. Has no effect while ShouldDrawBorder is false.
DropShadowColor
Gets or sets the colour of the shadow under the clock, which fades out from it.
- Value
- The default is
Color.Gray. Used whileShouldDropShadowBeEnabledistrue.
GlassEndColor
Gets or sets the colour the face's glass shine fades to at the bottom.
- Value
- The default is
Color.Transparent.
GlassRenderingAlphaComponent
Gets or sets how strong the face's glass shine is.
- Value
- The opacity of
GlassStartColor, from 28 (faint) to 128 (strong). The default is 72. A value outside that range is replaced by 60.
GlassStartColor
Gets or sets the colour at the top of the face's glass shine.
- Value
- The default is
Color.White. Its alpha is replaced byGlassRenderingAlphaComponent.
GradientDrawingAngleForCircularArtefacts
Gets or sets the direction of the colour gradients on the border, the face and the hub disc.
- Value
- In degrees, clockwise from left to right. The default is 45: from the top left to the bottom right.
GradientDrawingAngleForTimeNeedles
Gets or sets the direction of the colour gradients on the three hands.
- Value
- In degrees, clockwise from left to right. The default is 90: start colour at the top, end colour at the bottom.
HourHandEndColor
Gets or sets the colour the hour hand's gradient ends in.
- Value
- The default is
Color.Black.
See also
HourHandStartColor
Gets or sets the colour the hour hand's gradient starts from.
- Value
- The default is
Color.Black.
Remarks
Each hand blends between its start and end colours at GradientDrawingAngleForTimeNeedles,
across the whole dial – so the colour a hand shows depends on where it points.
HourNeedleWidth
Gets or sets how thick the hour hand is.
- Value
- In logical pixels. The default is 9.
Remarks
The hour and minute hands have rounded ends; the second hand has square ends. The lengths are fixed fractions of the dial's radius – about a half, seven tenths and four fifths for the hour, minute and second hands – so a clock of any size, on any display, has the same proportions.
InnerRingEndColor
Gets or sets the colour the hub disc's gradient ends in.
- Value
- The default is
Color.Black.
InnerRingShapeBounds
Gets or sets the size and position of the hub disc at the centre of the face, measured from the centre.
- Value
- In logical pixels, with (0, 0) at the centre of the face. The default is (-25, -25, 50, 50): a disc 50 pixels across, centred.
Remarks
Drawn only while ShouldDrawInnerRing is true, under the hands.
InnerRingStartColor
Gets or sets the colour the hub disc's gradient starts from.
- Value
- The default is
Color.Black.
See also
MaxBorderThickness
Gets or sets the largest value BorderThickness accepts.
- Value
- In logical pixels, from 0 to 100. The default is 32. A value outside that range is replaced by 32.
Remarks
The limit is applied when BorderThickness is set: lowering it afterwards does not thin a
border that is already thicker.
MinuteHandEndColor
Gets or sets the colour the minute hand's gradient ends in.
- Value
- The default is
Color.Black.
MinuteHandStartColor
Gets or sets the colour the minute hand's gradient starts from.
- Value
- The default is
Color.Black.
See also
MinuteIndicatorVisual
Gets or sets the text drawn as each minute mark.
- Value
- Usually one character, such as
".","·"or"|". The default is".";nullor empty also draws".".
See also
MinuteIndicatorVisualColor
Gets or sets the colour of the minute marks.
- Value
- The default is
Color.White.
MinuteIndicatorVisualFont
Gets or sets the font the minute marks are drawn in.
- Value
- The default is
null, which uses the control'sControl.Font. A larger font gives a larger mark.
MinuteNeedleWidth
Gets or sets how thick the minute hand is.
- Value
- In logical pixels. The default is 6.
See also
NumeralsColor
Gets or sets the colour of the hour labels.
- Value
- The default is
Color.White.
NumeralsFont
Gets or sets the font of the hour labels.
- Value
- The default is
null, which draws them in the control'sControl.Font.
See also
RefreshInterval
Gets or sets how often the clock redraws itself, which is how often the second hand moves.
- Value
- In milliseconds. The default is 1000, one second.
Remarks
Setting it restarts the timer. The value must be at least 1: a smaller one throws
ArgumentOutOfRangeException from the timer. A clock with a StaticTime, or one with
no second hand to show, can use a longer interval to save work.
SecondHandEndColor
Gets or sets the colour the second hand's gradient ends in.
- Value
- The default is
Color.DarkRed.
SecondHandStartColor
Gets or sets the colour the second hand's gradient starts from.
- Value
- The default is
Color.DarkRed.
See also
SecondNeedleWidth
Gets or sets how thick the second hand is.
- Value
- In logical pixels. The default is 2.
See also
ShouldDrawBorder
Gets or sets whether the ring round the face is drawn.
- Value
trueto draw it. The default istrue.
See also
ShouldDrawDropShadowForTimeHandles
Gets or sets whether each hand casts a shadow onto the face, offset down and to the right.
- Value
trueto draw the hands' shadows. The default isfalse.
See also
ShouldDrawGlassEffect
Gets or sets whether a glass shine is drawn across the upper part of the face.
- Value
trueto draw it. The default isfalse.
Remarks
The shine fades from GlassStartColor, at GlassRenderingAlphaComponent
opacity, down to GlassEndColor.
ShouldDrawGlassEffectOnBorder
Gets or sets whether a thin glass highlight is drawn on the border ring.
- Value
trueto draw it. The default isfalse.
Remarks
Drawn only on a border more than 3 logical pixels thick, in BorderGlassStartColor at
BorderGlassRenderingAlphaComponent opacity – which is 0 until it is set, so set that too.
ShouldDrawInnerRing
Gets or sets whether a hub disc is drawn at the centre of the face, under the hands.
- Value
trueto draw it. The default isfalse.
See also
ShouldDrawMinuteIndicatorVisual
Gets or sets whether a mark is drawn at each minute between the hour labels.
- Value
trueto draw the marks. The default istrue.
Remarks
The marks are drawn only when the control is square, so the face is a circle. While
ShouldDrawNumerals is set the marks at the hours are left out, where the labels are.
See also
ShouldDrawNumerals
Gets or sets whether the twelve hour labels are drawn round the face.
- Value
trueto draw them. The default istrue.
Remarks
Turning it off also hides the minute marks. TimeRepresentationMode says what the labels are.
ShouldDropShadowBeEnabled
Gets or sets whether a soft shadow is drawn under the clock, down and to the right.
- Value
trueto draw the shadow. The default istrue.
Remarks
The shadow needs room, so while it is on the face is drawn 20 pixels (at 100% scaling) inside each edge of the control and is smaller by that much.
See also
StaticTime
Gets or sets a fixed time for the hands to show instead of the running time.
- Value
- The time to show, of which only the hour, minute and second are used. The default is
null: the clock runs.
Remarks
While it is set the clock does not follow the time, whatever TimeZoneId says. Set
it back to null to start the clock again.
Example
// Show when the order was dispatched.
dispatchClock.StaticTime = order.DispatchedAt;Time
Gets the time the clock shows: StaticTime when it is set, otherwise the time now in
TimeZoneId's zone, or on this computer when that is empty.
- Value
- The time on the face, read afresh on each call.
See also
TimeRepresentationMode
Gets or sets how the twelve hour labels are written: numbers, Roman numerals or labels of your own.
- Value
- The default is
TimeRepresentationMode.Number.
See also
HourIndicatorLookupDictionary
TimeZoneId
Gets or sets the Windows time zone whose time the clock shows, such as "Eastern Standard Time" or
"Tokyo Standard Time".
- Value
- A time zone id as
Idgives it. The default isnull: the computer's own time zone. An empty string also means the computer's own. - Default
null
Remarks
The clock keeps running in that zone, daylight saving included, which is what a row of world clocks
needs. StaticTime still wins while it is set.
An id the computer does not know throws TimeZoneNotFoundException, so a mistyped one is
seen at once rather than quietly showing local time. GetSystemTimeZones lists the
ids the computer has.
Example
newYorkClock.TimeZoneId = "Eastern Standard Time";
tokyoClock.TimeZoneId = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time").Id;Methods
Dispose
Stops the redraw timer and releases it and the label brushes.
Parameters
disposingtruewhen called fromDispose().
OnPaint
Draws the clock: shadow, border, face, labels, hub and hands, for Time.
Parameters
e- The surface and clip rectangle to paint.
Remarks
The base class's handling runs first, so Control.Paint handlers draw under the clock.
Restart
Replaces the timer that redraws the clock with a new one, so the next redraw is one
RefreshInterval from now.
Remarks
The control starts its timer itself and setting RefreshInterval restarts it, so this
is needed only to bring the redraws into step with something else – the start of a second, say.