In this tutorial we are going to use a bit of graphics class to design a C# analog clock. We have also used a timer to continuously move our clock’s pointer.
Concept
Concept
- We will first initialize some angle values to second, minute, hour hand.
- Then draw the clock using the graphics class, save it as a bitmap variable and load it in the picturebox.
- Finally Use the timer to move the second hand.
Also Read: C/C++ TIC-TAC-TOE GAME
Also Read: Make Analog Clock in C Using Graphics
Instructions
- Create a new windows form applicaton project in visual c#.
- Add a picture box on the form.
- Double click the form to switch to code view.
- Delete all the existing code and paste the code given below.
C# Analog Clock Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace AnalogClock { public partial class Form1 : Form { Timer t = new Timer(); int WIDTH = 300, HEIGHT = 300, secHAND = 140, minHAND = 110, hrHAND = 80; //center int cx, cy; Bitmap bmp; Graphics g; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //create bitmap bmp = new Bitmap(WIDTH + 1, HEIGHT + 1); //center cx = WIDTH / 2; cy = HEIGHT / 2; //backcolor this.BackColor = Color.White; //timer t.Interval = 1000; //in millisecond t.Tick += new EventHandler(this.t_Tick); t.Start(); } private void t_Tick(object sender, EventArgs e) { //create graphics g = Graphics.FromImage(bmp); //get time int ss = DateTime.Now.Second; int mm = DateTime.Now.Minute; int hh = DateTime.Now.Hour; int[] handCoord = new int[2]; //clear g.Clear(Color.White); //draw circle g.DrawEllipse(new Pen(Color.Black, 1f), 0, 0, WIDTH, HEIGHT); //draw figure g.DrawString("12", new Font("Arial", 12), Brushes.Black, new PointF(140, 2)); g.DrawString("3", new Font("Arial", 12), Brushes.Black, new PointF(286, 140)); g.DrawString("6", new Font("Arial", 12), Brushes.Black, new PointF(142, 282)); g.DrawString("9", new Font("Arial", 12), Brushes.Black, new PointF(0, 140)); //second hand handCoord = msCoord(ss, secHAND); g.DrawLine(new Pen(Color.Red, 1f), new Point(cx, cy), new Point(handCoord[0], handCoord[1])); //minute hand handCoord = msCoord(mm, minHAND); g.DrawLine(new Pen(Color.Black, 2f), new Point(cx, cy), new Point(handCoord[0], handCoord[1])); //hour hand handCoord = hrCoord(hh % 12, mm, hrHAND); g.DrawLine(new Pen(Color.Gray, 3f), new Point(cx, cy), new Point(handCoord[0], handCoord[1])); //load bmp in picturebox1 pictureBox1.Image = bmp; //disp time this.Text = "Analog Clock - " + hh + ":" + mm + ":" + ss; //dispose g.Dispose(); } //coord for minute and second hand private int[] msCoord(int val, int hlen) { int[] coord = new int[2]; val *= 6; //each minute and second make 6 degree if (val >= 0 && val <= 180) { coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180)); coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180)); } else { coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180)); coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180)); } return coord; } //coord for hour hand private int[] hrCoord(int hval, int mval, int hlen) { int[] coord = new int[2]; //each hour makes 30 degree //each min makes 0.5 degree int val = (int)((hval * 30) + (mval * 0.5)); if (val >= 0 && val <= 180) { coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180)); coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180)); } else { coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180)); coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180)); } return coord; } } } |
I recieved an error which "Error 1 'Analog_Clock.Form1' does not contain a definition for 'pictureBox1_Click' and no extension method 'pictureBox1_Click' accepting a first argument of type 'Analog_Clock.Form1' could be found (are you missing a using directive or an assembly reference?) c:usersstevedocumentsvisual studio 2012ProjectsAnalog ClockAnalog ClockForm1.Designer.cs 42 68 Analog Clock
"
It directed me to the Form1.Designer,cs, how can i go about this?
What is hlen and why we not use main () function?
Thanks for a straightforward example. My clock gadget stopped working, so this is a great substitute after I made some tweaks to change the size.
Note to the others with questions, main() gets created for you when you create a C# forms project. You need to add a load event to for Form1_Load to be called. Then add the code here into form1.cs.
What is mean ( COORD ) ?
doesn’t work
@fil
Pls give us less details so we can appreciate your substiantiated comment even more..
There is no out put plz help as..