Click here to Skip to main content
6,629,377 members and growing! (20,954 online)
Email Password   helpLost your password?
Languages » C# » Windows Forms     Intermediate

A Resizable Analog Clock in C# using GDI+ & Windows Forms

By samar abbas

An analog clock using anti-aliasing & double-buffering which can be resized, even into elliptical shapes.
C#, .NET, Win2K, WinXP, Win2003, Visual Studio, Dev
Posted:8 Sep 2003
Views:45,663
Bookmarked:21 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 3.42 Rating: 2.78 out of 5
7 votes, 41.2%
1
2 votes, 11.8%
2

3
3 votes, 17.6%
4
5 votes, 29.4%
5

Sample Image - time_ana.jpg

Introduction

Unlike other clock programs, this program possesses the following special features:

  • Resizability: The main feature is that it can be resized according to the user's demands. Even an elliptical shape can be created. The hands, numerals and clock face all can have their shape changed at will.
  • Code Size & Elegance: Special importance has been given to reduce the code size and create elegant code. This program is perhaps the smallest analog clock program in terms of number of lines.
  • Double Buffering: It uses double buffering to eliminate flicker.
  • Anti-aliasing: It uses anti-aliasing to reduce jaggedness.

The Program

The main part of the program is the plotting of the hands. For this, we use polar co-ordinates with the origin at the center of the window and with angle starting at the twelve-o'clock position running clockwise. The semi-width or x-radius of the clock is taken to be one-third of that of the enclosing window, ie. Width/3. Consequently, the x value is given by the sum of the x co-ordinate of the center and the radius multiplied by the sine of the angle:

int x_sec = Width/2  + 
    (int)(Width/3  * Math.Sin(2 * Math.PI * (double)i_sec/60));

The angle is simply the fraction of a full circle, where 60 seconds correspond to 2 pi radians. In case of the y co-ordinate of the hand, we must take into account the fact that the 12 o'clock position is that of minimum y value, so that the cosine must be subtracted:

int y_sec = Height/2 - 
    (int)(Height/3 * Math.Cos(2 * Math.PI * (double)i_sec/60));

It is often helpful to draw a diagram to elucidate the above concepts. Of course, the program uses standard coding conventions, ie. i,x,y denote integers, g denotes Graphics and f denotes Font. The minutes hand is drawn in a similar fashion. In case of hours, it must be remembered that only 12 hours make a full circle.

In order to make the numerals resizable, the following for loop is used:

  for(int j=1; j <= 12; j++)   // draw numerals

     {
      g.DrawString(""+j, f, Brushes.Red,
        Width/2 + (int)(Width/3 * Math.Sin( j * Math.PI/6))
        - (int)g.MeasureString(""+j,f).Width/2,
        Height/2 - (int)(Height/3 * Math.Cos( j * Math.PI/6))
        - (int)g.MeasureString(""+j,f).Height/2);
     }
  }

Here, the fact that each numeral represents one-sixth of one pi or semicircle is used to plot the values. Moreover, the calculated half value of the string widths and heights are subtracted in order to provide correct centering of the numerals.

To compile and run, enter

> cs time_ana.cs     
> time_ana.exe

History

Project completed on 6th Sept. 2003.

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

About the Author

samar abbas


Member

Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
Generalthis is good Pinmemberalrsds18:18 23 Sep '09  
Generalcreate elegant code PinmemberCarlos H. Perez14:18 9 Sep '03  
GeneralRe: create elegant code Pinmembertedvg1:04 10 Sep '03  
GeneralRe: create elegant code Pinsusseanderson5:01 16 Sep '03  
GeneralRe: create elegant code PinmemberKeith Rule6:34 16 Sep '03  
GeneralRe: create elegant code PinmemberTorben Jakobsen22:59 20 Sep '03  
GeneralRe: create elegant code Pinmemberjeff.hill15:23 21 Feb '05  
QuestionWhy not using this? PinmemberHamed Mosavi8:29 9 Feb '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Sep 2003
Editor: Marc Clifton
Copyright 2003 by samar abbas
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project