Click here to Skip to main content
15,354,641 members
Articles / Desktop Programming / WPF
Posted 22 Dec 2008


65 bookmarked

Binary Clock 3D Screensaver in WPF

Rate me:
Please Sign up or sign in to vote.
4.88/5 (20 votes)
20 Jan 2009CPOL6 min read
Screensaver of a 3D binary clock - demonstrating WPF's layout possibilities, styling/templating and data binding

Table of Contents


Although you may find several binary clock projects throughout the web, this article will show you not only how to create a binary clock, but also how to host it in space and use it as a 3D rotating screensaver – all of this utilizing Windows Presentation Foundation and its powerful concepts such as data binding, data templates, styling, automatic layout, animation and 3D support.

This binary clock project originated as a mere experiment to help me understand the above WPF concepts. What you see here is therefore part of my WPF learning process, which I thought might be useful also for other enthusiasts of the technology.


Maybe you already know the concept of a Binary Clock: it's a clock where you read the 6 digits of current time as 6 columns, each consisting of some lit and unlit LEDs. Actually, we can better call it a pseudo-binary clock, since it shows the digits in BCD (binary coded decimal) notion. If you like the concept, but don't have it on your desk yet, then you can have it at least as your screensaver by downloading the attached binaries.

The Core - Binary Clock

First, the binary clock has a simple task: to show its six digits, one by one. Secondly, the digits themselves don't even need to know anything about their visual representation, thus to represent a BCD digit, it’s fully enough to have a purely data class for them. The idea behind displaying a data class is to apply a DataTemplate to it. This way we could simply put six instances of the Digit class into some items container of our window and leave the rest of the look-and-feel-work to be done by our DataTemplate.

This is what the basic structure of that template looks like:

<DataTemplate DataType="{x:Type local:Digit}">


    <!-- BCD Digits consist of 4 bits -->
    <StackPanel Orientation="Vertical">

        <Rectangle Style="{StaticResource fourthBit}" />
        <Rectangle Style="{StaticResource thirdBit}" />
        <Rectangle Style="{StaticResource secondBit}" />
        <Rectangle Style="{StaticResource firstBit}" />


Each BCD digit consists of four binary digits/bits. They're stacked one above the other, the least significant bit being placed at the bottom and the most significant bit at the top.

It’s the style applied to any specific bit which tells whether that bit needs to be lit or unlit at a certain moment of time. For example, the least significant bit needs to be lit if the BCD digit’s value modulo 2 gives nonzero (that is, every other second of time). Similarly, the bit above the least significant bit needs to be lit if the BCD digit’s value modulo 4 gives a result of 2 or greater number, etc. This logic can be generalized by using a WPF value converter, having some simple modulo computations inside. The DigitModuloConverter class will tell us if the specified modulo of any integer value is greater than the supplied parameter or not.

class DigitModuloConverter : DependencyObject, IValueConverter
    public static DependencyProperty ModuloModeProperty = ...
    public int ModuloMode { ... }
    public object Convert(object value, Type targetType, object parameter,
                          System.Globalization.CultureInfo culture)
        int source = (int)value;
        int param = 0;
        Int32.TryParse(parameter as string, out param);
        return (source % this.ModuloMode > param);

Having that converter in our toolbox, we can directly convert a BCD digit’s value to the on/off state of its bits. This is achieved by Data Triggers of WPF: each bit will use a highlighted fill and highlighted stroke color whenever its assigned DigitModuloConverter tells so. Otherwise, it remains or switches back to a basic, dark color.


    <!-- Digit converters -->    
    <local:DigitModuloConverter x:Key="moduloTwoConverter" ModuloMode="2" />
    <local:DigitModuloConverter x:Key="moduloFourConverter" ModuloMode="4" />
    <local:DigitModuloConverter x:Key="moduloEightConverter" ModuloMode="8" />
    <local:DigitModuloConverter x:Key="moduloSixteenConverter" ModuloMode="16" />

    <!-- Appearance of 1st bit in Digit (the least significant one) -->
    <Style TargetType="Rectangle" x:Key="firstBit"
                                  BasedOn="{StaticResource bitsCommonStyle}">

            <DataTrigger Binding="{Binding Path=Value,
                                   Converter={StaticResource moduloTwoConverter}}"
                <Setter Property="Stroke" 
			Value="{DynamicResource fullRectBorderBrush}" />

                <Setter Property="Fill" Value="{DynamicResource fullRectBrush}" />


When all of this is set up, it’s enough to have a timer which updates our BCD digit values each second. The BCD digits – i.e. instances of the Digit class – are declared in XAML and their update is done in the setTimeToDigits() function.

private void setTimeToDigits()
    System.DateTime now = System.DateTime.Now;

    this.hourUpper.Value = (int)now.Hour / 10;
    this.hourLower.Value = now.Hour % 10;

    this.minUpper.Value = (int)now.Minute / 10;
    this.minLower.Value = now.Minute % 10;

    this.secUpper.Value = (int)now.Second / 10;
    this.secLower.Value = now.Second % 10;

The Digit instances need to have their IsUpperDigit and IsHourDigit properties properly set. The styles of the bits read these attributes and hide some unnecessary bits based on them. E.g. the BCD digit of “tens” of hours will have the upper 2 bits hidden, because we have only 24 hours in a day. Also, the BCD digit of “tens” of minutes will have the most significant bit hidden, since even to display 59 minutes we need only 3 bits for the “tens” part.

<StackPanel ...>
    <StackPanel ...>
            <local:Digit x:Name="hourUpper" IsUpperDigit="True" IsHourDigit="True"/>


            <local:Digit x:Name="hourLower" IsHourDigit="True" />

    <StackPanel ...>

            <local:Digit x:Name="minUpper" IsUpperDigit="True" />

            <local:Digit x:Name="minLower" />

    <StackPanel ...>
            <local:Digit x:Name="secUpper" IsUpperDigit="True" />


            <local:Digit x:Name="secLower" />

Screensaver in 3D and Configuration with Live Preview

What a Windows screensaver needs to be is basically an *.exe module accepting some well-defined command line arguments. A screensaver primarily needs to display a full screen window, but it should also support a preview mode and provide some optional settings widow for customization.

This project goes even further: it supports not only a simple preview mode, but inside the settings window it offers also a live preview to interactively show how each change will affect the output. 


Click to see the video demonstrating the Live Preview.

This could be achieved by reusing the BinaryClockPage class, which provides the core functionality for the binary clock (see the previous section).

Image 3

The 3D effect of the clock is achieved by adding the Digit class instances into a ViewPort3D object. This way we project 2D Rectangles to a 3D surface in space:


    <!-- 2D elements on 3D surface -->

        <!-- Give the plane a slight rotation -->

                    <AxisAngleRotation3D x:Name="planeRotation"
                                         Axis="0, -1, -0.17" />



        <!-- The Geometry, Material, and Visual for the Viewport2DVisual3D -->

            <MeshGeometry3D ... />

            <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True"


The binary clock has a rotating animation around the vertical axis. The speed of this rotation can be adjusted through the settings window with the help of a simple slider. Another slider serves for setting the camera distance, thus making the binary clock appear closer or further from the screen.  

Every time the slider for rotation speed is moved, the rotation animation needs to be restarted to apply the new animation duration.

Multi-monitor Support

The screensaver will handle multiple monitors present in your system in a very straightforward way: it will replicate the same content on each monitor. Should you notice that the clocks in these different instances are not always synchronized then yes, you're simply right. This visual side effect is caused by the fact that each clock instance uses its own timer, started only after the previous instance was created. The issue can possibly be addressed in some future update of the article.

Skinning Support

Skinning of BinaryClock screensaver is achieved utilizing WPF's ResourceDictionary concept. This allows us to change skins in runtime. Each "skin" is placed into a separate XAML file containing a ResourceDictionary, so that we can switch to use any of these XAML files in runtime. Each ResourceDictionary defines the same brush objects, but with different color settings. Thus, simply by loading a different ResourceDictionary in runtime, our binary clock colors will be automatically switched to the new skin (this is fully transparent to us, since we reference these brushes as DynamicResource objects).

Code snippet from the XAML file defining the "Blue" skin:

<ResourceDictionary ...>
    <!-- Unlit LED border color -->
    <SolidColorBrush x:Key="emptyRectBorderBrush" Color="#FF00087F" />

This is how the skin's elements are used by BinaryClock:

<!-- Basic appearance of all Digits -->
<Style TargetType="Rectangle" x:Key="bitsCommonStyle">
	<Setter Property="Stroke" Value="{DynamicResource emptyRectBorderBrush}" />

Future Enhancements

If you like this screensaver and have some proposals for improvements, please feel free to let me know, as I still plan to add some new features in the future. These may include new settings to adjust 3D effects, possibility to have a readable, decimal hint, but also bugfixes, etc. 


  • 18 Jan 2009: Skinning support
  • 23 Dec 2008: Small bugfix for multi-monitor systems, based on a reader comment
  • 22 Dec 2008: Initial revision


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


About the Author

Attila Kúr
Software Developer (Senior)
Slovakia Slovakia
No Biography provided

Comments and Discussions

GeneralA suggestion Pin
mght9-Jan-09 5:00
Membermght9-Jan-09 5:00 
GeneralRe: A suggestion Pin
Attila Kúr9-Jan-09 6:23
MemberAttila Kúr9-Jan-09 6:23 
GeneralRe: A suggestion Pin
delyk21-Jan-09 4:41
Memberdelyk21-Jan-09 4:41 
GeneralRe: A suggestion Pin
mght22-Jan-09 10:03
Membermght22-Jan-09 10:03 
GeneralMemory Leak Pin
s_reckers6-Jan-09 9:33
Members_reckers6-Jan-09 9:33 
GeneralRe: Memory Leak Pin
Attila Kúr6-Jan-09 10:02
MemberAttila Kúr6-Jan-09 10:02 
GeneralRe: Memory Leak Pin
Attila Kúr9-Jan-09 11:52
MemberAttila Kúr9-Jan-09 11:52 
GeneralRe: Memory Leak Pin
s_reckers12-Jan-09 2:41
Members_reckers12-Jan-09 2:41 
GeneralRe: Memory Leak Pin
SpaceQ11-Feb-09 9:44
MemberSpaceQ11-Feb-09 9:44 
GeneralGreat! [modified] Pin
Emil - Gabriel23-Dec-08 3:17
MemberEmil - Gabriel23-Dec-08 3:17 
GeneralRe: Great! [modified] Pin
Attila Kúr23-Dec-08 4:47
MemberAttila Kúr23-Dec-08 4:47 
GeneralRe: Great! Pin
rcollina30-Dec-08 9:15
Memberrcollina30-Dec-08 9:15 
QuestionSource Code? Pin
jklucker22-Dec-08 9:13
Memberjklucker22-Dec-08 9:13 
AnswerRe: Source Code? Pin
Attila Kúr22-Dec-08 9:58
MemberAttila Kúr22-Dec-08 9:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.