A Simple Clock

Oct 13, 2002

1 min read

.NET1.0

Win95

Win98

WinME

Win2K

WinXP

C#

Windows

.NET

Visual-Studio

Dev

Intermediate

Author picture

by kavehdr

Contributor

147k Views

4.13/ 5

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.

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.

        #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
CS
        

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

License

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