Click here to Skip to main content
Click here to Skip to main content

Three Zone World Clock

, 3 Mar 2005
Rate this:
Please Sign up or sign in to vote.
A three time zone world clock

Sample Image - maximum width is 600 pixels

Introduction

This article demonstrates how an irregular, skinnable world clock can be created and how application settings can be persisted using XML. The clock has been quite handy for working out who's awake and at their desk on a development project that spans multiple time zones.

Background

This Three Zone World Clock was inspired by the article by Iulian Serban called How to build a Clock control. I have used a stripped down version of Iulian Serban's clock painting code and added logic for dealing with three clocks, handling transparency and the loading and saving of settings.

Using the code

I found when I started experimenting with transparent bitmaps on forms that there are a myriad ways of stuffing up transparency. After many searches and experiments I wrote this routine which seems to handle the transparency correctly. The routine loads a named skin file up from the application directory and then sets this as the background image for the form. Amazingly without the g.Clear statements subsequent bitmaps would be layered one on top of another - this was certainly a suprise feature for me!

private void SetSkin(string skinName, bool initializing)
{
      //Keep the new skin name in the application settings object<BR>      m_appSettings.SkinFile = skinName;<BR>
      //Find the application directory and then the full skin path<BR>      string appPath = Path.GetDirectoryName(Application.ExecutablePath);<BR>      string skinPath = appPath + "\\" + skinName;

      //Load skin if it exists
      if(File.Exists(skinPath))
      {
            Bitmap skinBmp = (Bitmap)Bitmap.FromFile(skinPath);

            //Clear form background to prevent overlays
            if(!initializing)
            {
                  Graphics g = this.CreateGraphics();
                  g.Clear(Color.Black);
                  g.Clear(TransparencyKey);
            }

            //Apply transparency to the bitmap
            skinBmp.MakeTransparent(skinBmp.GetPixel(1,1));

            //Set the background image of the form
            BackgroundImage = skinBmp;

            //Apply transparency to the Form
            TransparencyKey = skinBmp.GetPixel(1,1);

            //Get the form to refresh
            Invalidate();
      }
}

Persisting the Application State to XML

An XML file seemed to be the obvious choice for storing the application settings. The XML serializer was used to persist an AppSettings object containing three ClockSettings objects. Once the settings object model has been created, the code to save and load settings is pleasingly minimal. One pitfall I came across when using the XML serializer is that you must include default constructors for the classes being persisted, even if these default constructors serve no apparent purpose they have to be there or you get a lovely System.InvalidOperationException

private void SaveSettings()
{
      string appPath = Path.GetDirectoryName(Application.ExecutablePath);
      m_appSettings.Save(appPath + "\\settings.xml");
      m_saved = true;
}

Future Enhancements

Dave has been busy suggesting enhancements I could make, but heck I want my lunchtimes back so here is a list of "nice to have" things that I probably will not get time to code...

  • More clocks - would be nice if there were a configurable number of clocks (adding clocks is relatively easy but its making them look nice that's the tricky bit). At the moment the code is hardwired to three clocks, but I think I've left the door half open to add more.
  • An editor for setting up clock names and times - I agree that hacking the XML is not very nice!
  • Deployment from the top of the screen - not sure what Dave's getting at here but it has something to do with the clocks sliding down like a roller blind from the top of the screen. Hmmm.
  • AM/PM indicator. Like all true analog clocks, my clocks currently give absolutely no clue as to whether its day or night.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Bill Hoyland
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionWere you able to develop another version in asp.net using ajax? Pinmembersmileoftushar3-Dec-09 5:52 
GeneralGreat!! PinmemberTonchi14-Mar-05 11:18 
QuestionBeerse? PinmemberPatje3-Mar-05 21:20 
AnswerRe: Beerse? Pinmemberbillhoyland10-Mar-05 3:22 
Hi - never been there but I just like the name, it sounds like a drink we have here in England...
 
Bill
GeneralRe: Beerse? PinmemberPatje10-Mar-05 5:31 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 3 Mar 2005
Article Copyright 2005 by Bill Hoyland
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid