


Introduction
This article presents a digital clock written using C# .Net 3.5 and Windows Presentation Foundation (WPF), with a version supporting styling using the RibbonStyleHandler from the Ribbon Control Library (and hence allowing styling consistent with RibbonWindow applications).
Using the Code
This is VERY simple, to create a new clock window, make a new instance of DigitalClockWindow! To add a control to an existing window, use DigitalClockControl.
About the Code
The DigitalClock class is comprised of DigitalClockDots and DigitalClockElement. The DigitalClockDots class is a UIElement representing two dots to separate hours from minutes from seconds. DigitalClockElement represents the following states: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Hence the DigitalClock is a Grid of DigitalClockElement DigitalClockElement DigitalClockDots DigitalClockElement DigitalClockElement DigitalClockDots DigitalClockElement DigitalClockElement.
The DigitalClockControl and DigitalClockWindow classes contain a thread for updating the clock when AutoUpdate is set to true, otherwise the clock may be updated via the .CurrentTime property of DigitalClockControl.
DigitalClockControl contains three properties for styling, DigitBrush the Brush used to render the digits (0 to 9), DotBrush the Brush used to render the separating dots, and ClockBackground the background of the clock. DigitalClockDots contains a single property RenderBrush for setting the brush used to render the dots. DigitalClockElement contains a similar property RenderBrush for setting the brush used to render the graphic components. Furthermore it also contains a Value property for setting the value to display (in the range 0 to 9).
Ribbon Styled Clock
In version 1.0.0.1 RibbonClockControl and RibbonClockWindow have been added which inherit from DigitalClockControl and DigitalControlWindow respectively; they add automatic styling based on the RibbonStyleChanged event, and can be seen in use in the CIRIP project (a Computational Intelligent Railway Intervention Planner).
References
ribboncontrol.aspx - The Ribbon Control library used in styling the control.
History
Version 1.0.0.0 - Initial build
Version 1.0.0.1 - Separated Ribbon styling from clock.
Version 1.0.0.1 - Added AutoUpdate and CurrentTime properties to allow display of custom times
Version 1.0.0.1 - Added RibbonClockControl and Window with Ribbon styling
Version 1.0.0.1 - Refactored controls to allow resizing
Version 1.0.0.1 - Changed clock from Window to UserControl and added DigitalClockWindow
Version 1.0.0.1 - Fixed bug where update thread kept process alive
|
|
 |
 | Stop update thread from control instead of from containing window Geert Verbakel | 23:10 3 Mar '10 |
|
 |
To better stop the clock when a window is closed:
private bool stopUpdateTread = false;
public DigitalClockControl() { InitializeComponent();
this.AutoUpdate = true; this.Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted; }
private void Dispatcher_ShutdownStarted(object sender, EventArgs e) { stopUpdateTread = true; }
and then in the updateThread:
updateThread = new Thread(delegate() { try
{ while (true && !stopUpdateTread) { DateTime d = DateTime.Now;
bool complete = false; this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new RefreshDelegate(delegate() { this.CurrentTime = d;
complete = true; }));
#region pause try
{ do
{ Thread.Sleep(900); } while (!complete && !stopUpdateTread); } catch (Exception) { } #endregion
} } catch (Exception) { } });
|
|
|
|
 |
 | New Version Released 1.0.0.1 Derek Bartram | 3:56 1 May '08 |
|
 |
A new version has been released which significantly improves the code quality and ease of use! As requested the control resizes too now.
|
|
|
|
 |
 | Referances King_kLAx | 1:39 27 Apr '08 |
|
 |
Is that the correct spelling?
I like a lot of programmers also suffer the lack of logic in the English language.
Great control btw
Cheers
Ian
|
|
|
|
 |
|
 |
You're correct, i'm off to change it now.
It's odd that the code project submission wizard doesn't have a built in spell checker.
Ps. I want intellisence for Word
|
|
|
|
 |
 | A few other comments Marc Clifton | 10:32 26 Apr '08 |
|
 |
When I type Alt-F4 to close the clock, the thread isn't terminated, so it remains in the process list. This also prevents further recompilation of the code until the process is terminated. I'm surprised you didn't discover that yourself.
How do I change the skin? Your article shows three skins, but the program only shows the first one.
How do I modify the window so it's sizable? I tried a few things but the code would subsequently crash.
Thanks!
Marc
|
|
|
|
 |
|
 |
Firstly thank you for the comments.
Marc Clifton wrote: When I type Alt-F4 to close the clock, the thread isn't terminated, so it remains in the process list
Marc Clifton wrote: I'm surprised you didn't discover that yourself.
In the context i'm using it's not a problem as the thread gets registered elsewhere and gets closed there. A slight oversight which I will correct when I get a moment.
Marc Clifton wrote: How do I change the skin? Your article shows three skins, but the program only shows the first one.
Take a look at the Ribbon Control library in the referances section. The styling of the control is done by RibbonStyleHandler. Calling RibbonStyleHander.restyle(RibbonStyle.Blue|Green|Gray|Black) will give you some options. Also take a look at RibbonStyleConfigWindow which is currently being developed and finalised. In the next version i'm restructing the code significantlly better which will give better access to manual styling.
Marc Clifton wrote: How do I modify the window so it's sizable? I tried a few things but the code would subsequently crash.
It's not implemented yet is the simple answer. Off the top of my head though I know you can perform some render transforms, so that would perhaps be the quick fix until I make to control more sizable (which I will endevour to add in the next release also).
|
|
|
|
 |
|
 |
Ps. OMG how much time do you have!?! 124 articles, it's taken me ages just to produce 15. There is some good stuff in there though for sure; personal thanks as I've used your work in the past.
|
|
|
|
 |
|
 |
Derek Bartram wrote: OMG how much time do you have!?!
LOL! Well, where there's a will...
A lot of the articles based on actual things I've had to develop, so there is a "killing two birds with one stone" effect.
Derek Bartram wrote: I've used your work in the past
Marc
|
|
|
|
 |
|
|
 |
|
 |
Derek Bartram wrote: What do you do..... work for Santa during the winter months!?! lol
Both of those articles are based on an exhibit on fractals that, I believe, is still running (after 17 years, OMG) here[^] in San Diego CA.
Marc
|
|
|
|
 |
 | Where do I download the Ribbon Control Library? Marc Clifton | 10:15 26 Apr '08 |
|
 |
[edit] Never mind. I googled it and found your article. Wowzers! [/edit]
Marc
|
|
|
|
 |
|
 |
Thank you for pointing that out; it's now been referanced in the article.
|
|
|
|
 |
|
|
Last Updated 26 Apr 2008 |
Advertise |
Privacy |
Terms of Use |
Copyright ©
CodeProject, 1999-2010