Click here to Skip to main content
15,887,386 members
Articles / Programming Languages / C#
Article

A Simple Clock

Rate me:
Please Sign up or sign in to vote.
4.13/5 (7 votes)
15 Oct 2002 145.5K   1.9K   55   16
This program shows how to make a simple clock.

Sample Image - clock.jpg

Introduction

This program shows how to make a clock using C#. The program is basically very easy, there is only a little math involved which I think shouldn't be that difficult for any of you programmers out there. The program uses a timer that is fired every .5 seconds; myTimer_Tick() takes the system's current time (hour, minute, second) and saves them to the appropriate variables. The titlebar is then updated showing the time in numbers, and at last a call to updateClock() is made, which in turn draws the clock's hands.

updateClock() makes a new bitmap and copies the bmp image of the clock.bmp (which is saved in the variable 'bitmap' in the constructor and taken from the program's execution directory) to another Bitmap variable. Then, it figures out the angle of every hand and draws them on that variable. Finally, our bitmap variable is loaded to the PictureBox on our form.

C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Clock
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Timer myTimer;
        private System.ComponentModel.IContainer components;
        //Variables and objects created by us (not the designer)

        //Holds current time components
        private int hour, minute, second;
        //The endpoint of clock's hand
        private int xCordinate=150, yCordinate=150;
        //Radius of the face of the clock
        private int radius;
        //Holds the clock's face
        private Bitmap bitmap;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //Show in the titlebar
            this.Text += " Getting time...";
            //Set the radius as half the picturebox size
            radius = this.pictureBox1.Width/2;
            //Read the clock's face and save it to a bitmap object
            bitmap = new Bitmap(Application.StartupPath + "\\clock.bmp");
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

This section is all the code generated by the VS.NET Forms Designer.

C#
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    this.components = new System.ComponentModel.Container();
    System.Resources.ResourceManager resources =
        new System.Resources.ResourceManager(typeof(Form1));
    this.pictureBox1 = new System.Windows.Forms.PictureBox();
    this.myTimer = new System.Windows.Forms.Timer(this.components);
    this.SuspendLayout();
    //
    // pictureBox1
    //
    this.pictureBox1.Name = "pictureBox1";
    this.pictureBox1.Size = new System.Drawing.Size(304, 304);
    this.pictureBox1.TabIndex = 0;
    this.pictureBox1.TabStop = false;
    //
    // myTimer
    //
    this.myTimer.Enabled = true;
    this.myTimer.Interval = 500;
    this.myTimer.Tick += new System.EventHandler(this.myTimer_Tick);
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(300, 300);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                         this.pictureBox1});
    this.FormBorderStyle =
                System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.Icon =
      ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    this.MaximizeBox = false;
    this.Name = "Form1";
    this.ShowInTaskbar = false;
    this.Text = "Clock";
    this.ResumeLayout(false);

}
#endregion
C#
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        /// <summary>
        /// The timer is fired every .6 second
        /// (I call it a little before one second so
        /// if the functions takes a while it won't
        /// miss drawing a second.) It will then 
        /// get the current time and update the possition
        /// of the hands through updateClock()
        /// </summary>
        private void myTimer_Tick(object sender, System.EventArgs e)
        {
            //Get the user's timezone
            TimeZone myTime     = TimeZone.CurrentTimeZone;
            TimeSpan myTimeSpan = 
                myTime.GetUtcOffset(new DateTime(DateTime.Today.Year, 
                DateTime.Today.Month, DateTime.Today.Day));
            DateTime today    = DateTime.UtcNow;
            //Localtime = UTC + UTC Offset
            today             = today.Add(myTimeSpan);

            //Holds the current time components
            hour     = today.Hour;
            minute   = today.Minute;
            second   = today.Second;

            //Update the titlebar's time
            this.Text = "Clock " + hour + ":" + minute + ":" + second;
            
            //Update the clock
            updateClock();

        }

        /// <summary>
        /// Draw the 3 hands on a new bitmap object and load it to picturBox.
        /// </summary>
        private void updateClock()
        {
            //The angle that the hand makes with PI/2 in radians
            double angle=0;
            //Relative lengths of hands
            double hourSize=.65, minuteSize=.85, secondSize=.8;

            //Make a new bitmap from the object 
            //saved from the disk, make a new pen
            //and use antialiasing on the bitmap 
            //so our hands will look smooth
            System.Drawing.Pen pen = new System.Drawing.Pen(Color.Black, 4);
            Bitmap bitmaptemp = (Bitmap) this.bitmap.Clone(); 
            Graphics gr = Graphics.FromImage(bitmaptemp);
            gr.SmoothingMode = 
              System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            Point point1 = new Point(radius, radius), point2;

            /* 
             * basically what we do in these steps is that
             * we find the current hour,
             * minute or seconds is what portion of the totall.
             * Say we have 30 seconds;
             * 30 seconds is 30/60 which is half of the circle
             * if we our talking about seconds. 
             * So we multiply this by 2PI which is 360 degrees
             * or one complete revoulution and we get the chunk
             * of the circle that should have been covered
             * by those seconds. Then if we get the Cosine
             * of this angle and multiply it by
             * the radius we get the X component of the endpoint
             * of our seconds hand and if
             * we multiply Sine of the angle by the radius
             * we get the Y component of this
             * end point. That's all we need in order to draw
             * our hand, since our starting
             * point is the center of the clock or basically
             * point (radius, radius) where
             * radius is just the clock's face width/2.
             * If this is kind of confusing draw
             * a sketch a unit circle with a ray going
             * from the center and see how the
             * sine and cosine of that angle from PI/2
             * (not from 0, becuase our clock starts
             * from PI/2 or 12 o'clock) relate
             * to the X and Y component of the endpoint of the ray.
             * 
             */

            //HOUR
            if (hour <= 12)
                angle = ((Math.PI / 2) - ((double)hour/12)* 2 * Math.PI);
            else if (hour > 12)
                angle = ((Math.PI / 2) - ((double)(hour-12)/12)* 2 * Math.PI);
            xCordinate = radius + (int)(radius * Math.Cos(angle) * hourSize);
            yCordinate = radius - (int)(radius * Math.Sin(angle) * hourSize);
            //Draw
            point2 = new Point(xCordinate, yCordinate);
            gr.DrawLine(pen, point1, point2);

            //MINUTE
            angle = ((Math.PI / 2) - ((double)minute/60) * 2 * Math.PI);
            pen.Width = 2;
            xCordinate = radius + (int)(radius * Math.Cos(angle) * minuteSize);
            yCordinate = radius - (int)(radius * Math.Sin(angle) * minuteSize);
            //Draw
            point2 = new Point(xCordinate, yCordinate);
            gr.DrawLine(pen, point1, point2);

            //SECOND
            angle = ((Math.PI / 2) - (((double)second/60) * 2 * Math.PI));
            pen.Width = 1;
            xCordinate = radius + (int)(radius * Math.Cos(angle) * secondSize);
            yCordinate = radius - (int)(radius * Math.Sin(angle) * secondSize);
            //Draw
            point2 = new Point(xCordinate, yCordinate);
            gr.DrawLine(pen, point1, point2);

            //Load the new bitmap to the picturebox
            this.pictureBox1.Image = bitmaptemp;
            pen.Dispose();
            gr.Dispose();                        
        }
    }
}

Above, I have a chunk of comments trying to explain what happens with the Math part. I hope I made sense!

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


Written By
Web Developer
United States United States
Started programming when I was 8 using BASIC under DOS. Later I switched to C and then C++. Recently I have been doing PHP and just started with C#.

Comments and Discussions

 
GeneralMy vote of 5 Pin
D-Kishore2-Sep-12 20:22
D-Kishore2-Sep-12 20:22 
good
Questionthe hour hand moves only every hour Pin
adrianb176622-Apr-12 18:04
adrianb176622-Apr-12 18:04 
GeneralThanks Pin
robocon117-Dec-10 2:22
robocon117-Dec-10 2:22 
GeneralYou need clock.bmp or code will crash HARD Pin
Poikilos12-Dec-05 17:18
Poikilos12-Dec-05 17:18 
Generalquestion Pin
debug1215-Oct-05 10:21
debug1215-Oct-05 10:21 
QuestionGDI? Pin
Aaron Sulwer2-Oct-05 6:41
Aaron Sulwer2-Oct-05 6:41 
QuestionDon't like the maths? Pin
mokka'logic22-Feb-05 19:51
sussmokka'logic22-Feb-05 19:51 
AnswerRe: Don't like the maths? Pin
jaashh24-Mar-10 18:40
jaashh24-Mar-10 18:40 
Generalwtf Pin
Anonymous11-Aug-04 9:43
Anonymous11-Aug-04 9:43 
GeneralRe: wtf Pin
Christian Graus11-Aug-04 12:28
protectorChristian Graus11-Aug-04 12:28 
GeneralRe: wtf Pin
Anonymous22-Feb-05 20:19
Anonymous22-Feb-05 20:19 
GeneralRe: wtf Pin
Pyro Joe25-Feb-05 16:26
Pyro Joe25-Feb-05 16:26 
GeneralRe: wtf Pin
Aaron Sulwer2-Oct-05 6:44
Aaron Sulwer2-Oct-05 6:44 
Generalgood code Pin
neha_roy155515-Jul-04 2:40
neha_roy155515-Jul-04 2:40 
Generalgood Pin
ziv200317-Mar-04 16:25
ziv200317-Mar-04 16:25 
Generalvery clean and nice coded Pin
Antonio Lopez22-May-03 7:44
Antonio Lopez22-May-03 7:44 

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.