How To Create An Analog Clock With C#

How it works?

So we will first set some angle values to second, minute, hour hand. Then use C# code to draw the clock using the graphics class, save it as a bitmap variable and load it to a picturebox. Then lastly use the timer to move the second hand, minute hand and the hour hand.
Instructions

Open Visual Studio, then create a new Windows Form Application Project. Add a Picture box to the form, now double click on the form to switch to code view. Then copy and paste the below code after the "private void Form1_Load(object sender, EventArgs e)" heading right curly bracket "}". 
  1. private void t_Tick(object sender, EventArgs e)  
  2. {  
  3.         // create an image   
  4.         cg = Graphics.FromImage(bmp);  
  5.         //get time   
  6.         int ss = DateTime.Now.Second;  
  7.         int mm = DateTime.Now.Minute;  
  8.         int hh = DateTime.Now.Hour;  
  9.         int[] handCoord = new int[2];  
  10.         //get time   
  11.         cg.Clear(Color.Blue);  
  12.         //draw a circle   
  13.         cg.DrawEllipse(new Pen(Color.Black, 6 f), 0, 0, WIDTH, HEIGHT);  
  14.         //draw clock numbers   
  15.         cg.DrawString("12"new Font("Ariel", 12), Brushes.Black, new PointF(140, 3));  
  16.         cg.DrawString("1"new Font("Ariel", 12), Brushes.Black, new PointF(218, 22));  
  17.         cg.DrawString("2"new Font("Ariel", 12), Brushes.Black, new PointF(263, 70));  
  18.         cg.DrawString("3"new Font("Ariel", 12), Brushes.Black, new PointF(285, 140));  
  19.         cg.DrawString("4"new Font("Ariel", 12), Brushes.Black, new PointF(263, 212));  
  20.         cg.DrawString("5"new Font("Ariel", 12), Brushes.Black, new PointF(218, 259));  
  21.         cg.DrawString("6"new Font("Ariel", 12), Brushes.Black, new PointF(142, 279));  
  22.         cg.DrawString("7"new Font("Ariel", 12), Brushes.Black, new PointF(70, 259));  
  23.         cg.DrawString("8"new Font("Ariel", 12), Brushes.Black, new PointF(22, 212));  
  24.         cg.DrawString("9"new Font("Ariel", 12), Brushes.Black, new PointF(1, 140));  
  25.         cg.DrawString("10"new Font("Ariel", 12), Brushes.Black, new PointF(22, 70));  
  26.         cg.DrawString("11"new Font("Ariel", 12), Brushes.Black, new PointF(70, 22));  
  27.         //draw seconds hand   
  28.         handCoord = msCoord(ss, secHAND);  
  29.         cg.DrawLine(new Pen(Color.Red, 2 f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));  
  30.         //draw minutes hand   
  31.         handCoord = msCoord(mm, minHAND);  
  32.         cg.DrawLine(new Pen(Color.Black, 3 f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));  
  33.         //draw hours hand   
  34.         handCoord = hrCoord(hh % 12, mm, hrHAND);  
  35.         cg.DrawLine(new Pen(Color.Black, 3 f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));  
  36.         //load the bitmap image   
  37.         pictureBox1.Image = bmp;  
  38.         //display time in the heading   
  39.         this.Text = "Analog Clock - " + hh + ":" + mm + ":" + ss;  
  40.         cg.Dispose();  
  41.     }  
  42.     //coord for minute and second   
  43. private int[] msCoord(int val, int hlen)  
  44. {  
  45.         int[] coord = new int[2];  
  46.         val *= 6; // note: each minute and seconds make a 6 degree   
  47.         if (val >= 0 && val <= 100)  
  48.         {  
  49.             coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));  
  50.             coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));  
  51.         } else  
  52.         {  
  53.             coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));  
  54.             coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));  
  55.         }  
  56.         return coord;  
  57.     }  
  58.     //coord for hour   
  59. private int[] hrCoord(int hval, int mval, int hlen)   
  60. {  
  61.     int[] coord = new int[2];  
  62.     //each hour makes 60 degree with min making 0.5 degree   
  63.     int val = (int)((hval * 30) + (mval * 0.5));  
  64.     if (val >= 0 && val <= 180)  
  65.     {  
  66.         coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));  
  67.         coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));  
  68.     } else   
  69.     {  
  70.         coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));  
  71.         coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));  
  72.     }  
  73.     return coord;  
  74. }  
Copy and paste the below code inside the curly bracket {} under the "private void Form1_Load(object sender, EventArgs e)" heading.
  1. // create a new bitmap   
  2. bmp = new Bitmap(WIDTH + 1, HEIGHT + 1);   
  3. // placing in center   
  4. cx = WIDTH / 2;   
  5. cy = HEIGHT / 2;   
  6. //backcolor   
  7. this.BackColor = Color.White;   
  8. //timer   
  9. t.Interval = 1000; // i.e. tick in milisecond   
  10. t.Tick += new EventHandler(this.t_Tick);   
  11. t.Start();   
Then copy and paste the below code inside the curly bracket {} under the "public partial class Form1 : Form" heading.
  1. Timer t = new Timer();   
  2. int WIDTH = 300, HEIGHT = 300, secHAND = 140, minHAND = 110, hrHAND = 80;   
  3. // in center   
  4. int cy, cx;   
  5. Bitmap bmp;   
  6. Graphics cg;   
Full code of the Analog clock application,
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. namespace analogClock  
  11. {  
  12.     public partial class Form1: Form  
  13.     {  
  14.         Timer t = new Timer();  
  15.         int WIDTH = 300, HEIGHT = 300, secHAND = 140, minHAND = 110, hrHAND = 80;  
  16.         // in center  
  17.         int cy, cx;  
  18.         Bitmap bmp;  
  19.         Graphics cg;  
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.         }  
  24.         private void Form1_Load(object sender, EventArgs e)   
  25.         {  
  26.             // create a new bitmap  
  27.             bmp = new Bitmap(WIDTH + 1, HEIGHT + 1);  
  28.             // placing in center  
  29.             cx = WIDTH / 2;  
  30.             cy = HEIGHT / 2;  
  31.             //backcolor  
  32.             this.BackColor = Color.White;  
  33.             //timer  
  34.             t.Interval = 1000; // i.e. tick in milisecond  
  35.             t.Tick += new EventHandler(this.t_Tick);  
  36.             t.Start();  
  37.         }  
  38.         private void t_Tick(object sender, EventArgs e)   
  39.         {  
  40.                 // create an image  
  41.                 cg = Graphics.FromImage(bmp);  
  42.                 //get time  
  43.                 int ss = DateTime.Now.Second;  
  44.                 int mm = DateTime.Now.Minute;  
  45.                 int hh = DateTime.Now.Hour;  
  46.                 int[] handCoord = new int[2];  
  47.                 //get time  
  48.                 cg.Clear(Color.Blue);  
  49.                 //draw a circle  
  50.                 cg.DrawEllipse(new Pen(Color.Black, 6 f), 0, 0, WIDTH, HEIGHT);  
  51.                 //draw clock numbers  
  52.                 cg.DrawString("12"new Font("Ariel", 12), Brushes.Black, new PointF(140, 3));  
  53.                 cg.DrawString("1"new Font("Ariel", 12), Brushes.Black, new PointF(218, 22));  
  54.                 cg.DrawString("2"new Font("Ariel", 12), Brushes.Black, new PointF(263, 70));  
  55.                 cg.DrawString("3"new Font("Ariel", 12), Brushes.Black, new PointF(285, 140));  
  56.                 cg.DrawString("4"new Font("Ariel", 12), Brushes.Black, new PointF(263, 212));  
  57.                 cg.DrawString("5"new Font("Ariel", 12), Brushes.Black, new PointF(218, 259));  
  58.                 cg.DrawString("6"new Font("Ariel", 12), Brushes.Black, new PointF(142, 279));  
  59.                 cg.DrawString("7"new Font("Ariel", 12), Brushes.Black, new PointF(70, 259));  
  60.                 cg.DrawString("8"new Font("Ariel", 12), Brushes.Black, new PointF(22, 212));  
  61.                 cg.DrawString("9"new Font("Ariel", 12), Brushes.Black, new PointF(1, 140));  
  62.                 cg.DrawString("10"new Font("Ariel", 12), Brushes.Black, new PointF(22, 70));  
  63.                 cg.DrawString("11"new Font("Ariel", 12), Brushes.Black, new PointF(70, 22));  
  64.                 //draw seconds hand  
  65.                 handCoord = msCoord(ss, secHAND);  
  66.                 cg.DrawLine(new Pen(Color.Red, 2 f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));  
  67.                 //draw minutes hand  
  68.                 handCoord = msCoord(mm, minHAND);  
  69.                 cg.DrawLine(new Pen(Color.Black, 3 f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));  
  70.                 //draw hours hand  
  71.                 handCoord = hrCoord(hh % 12, mm, hrHAND);  
  72.                 cg.DrawLine(new Pen(Color.Black, 3 f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));  
  73.                 //load the bitmap image  
  74.                 pictureBox1.Image = bmp;  
  75.                 //display time in the heading  
  76.                 this.Text = "Analog Clock - " + hh + ":" + mm + ":" + ss;  
  77.                 cg.Dispose();  
  78.             }  
  79.             //coord for minute and second  
  80.         private int[] msCoord(int val, int hlen)  
  81.         {  
  82.                 int[] coord = new int[2];  
  83.                 val *= 6; // note: each minute and seconds make a 6 degree  
  84.                 if (val >= 0 && val <= 100) {  
  85.                     coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));  
  86.                     coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));  
  87.                 } else {  
  88.                     coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));  
  89.                     coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));  
  90.                 }  
  91.                 return coord;  
  92.             }  
  93.             //coord for hour  
  94.         private int[] hrCoord(int hval, int mval, int hlen)  
  95.         {  
  96.             int[] coord = new int[2];  
  97.             //each hour makes 60 degree with min making 0.5 degree  
  98.             int val = (int)((hval * 30) + (mval * 0.5));  
  99.             if (val >= 0 && val <= 180)   
  100.             {  
  101.                 coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));  
  102.                 coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));  
  103.             } else  
  104.             {  
  105.                 coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));  
  106.                 coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));  
  107.             }  
  108.             return coord;  
  109.         }  
  110.     }  
  111. }  
You can visit here for more explanation.

 

Ebook Download
View all
Learn
View all