C# Analog Clock

Today we are going to design analog clock as the first post in 2017.
We can add digital clock to C# form very easily using “DateTimePicker”.
Analog Clock cannot add like that. We must design it for our form. Let’s design an analog clock.
First of all add a new form (Analog Clock) to your application.
Next add a “pictureBox” and a “timer” to the form.
As the next step write the following code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Globalization;
using System.IO;


namespace Clock
{
    public partial class Form1 : Form
    {
        Bitmap bmp;
        Graphics g;
        Timer t = new Timer();


     int WIDTH = 100, HEIGHT = 100, secHAND = 50, minHAND = 50, hrHAND =  25;
     int cx, cy;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bmp = new Bitmap(WIDTH + 1, HEIGHT + 1);
            cx = WIDTH / 2;
            cy = HEIGHT / 2;
            this.BackColor = SystemColors.Control;

            t.Interval = 1000;
            t.Tick += new EventHandler(this.timer1_Tick);
            t.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            g = Graphics.FromImage(bmp);

            int ss = DateTime.Now.Second;
            int mm = DateTime.Now.Minute;
            int hh = DateTime.Now.Hour;

            int[] handCoord = new int[2];

            g.Clear(Color.Transparent);

            g.DrawEllipse(new Pen(Color.Black, 1f), 0, 0, WIDTH, HEIGHT);

            {
           g.DrawString("12", new Font("Cambria", 8), Brushes.Black, 42, 1);
           g.DrawString("3", new Font("Cambria", 8), Brushes.Black, 90, 42);
           g.DrawString("9", new Font("Cambria", 8), Brushes.Black, 1, 42);            g.DrawString("6", new Font("Cambria", 8), Brushes.Black, 44, 86);
           g.DrawString("1", new Font("Cambria", 8), Brushes.Black, 68, 5);
           g.DrawString("2", new Font("Cambria", 8), Brushes.Black, 84, 20);
           g.DrawString("3", new Font("Cambria", 8), Brushes.Black, 1, 42);
           g.DrawString("4", new Font("Cambria", 8), Brushes.Black, 84, 66);
           g.DrawString("5", new Font("Cambria", 8), Brushes.Black, 68, 82);
           g.DrawString("7", new Font("Cambria", 8), Brushes.Black, 22, 80);
           g.DrawString("8", new Font("Cambria", 8), Brushes.Black, 7, 63);
           g.DrawString("10", new Font("Cambria", 8), Brushes.Black, 7, 20);
           g.DrawString("11", new Font("Cambria", 8), Brushes.Black, 22, 5);
           }

            handCoord = msCoord(ss, secHAND);
            g.DrawLine(new Pen(Color.Red, 1f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

            handCoord = msCoord(mm, minHAND);
            g.DrawLine(new Pen(Color.Black, 2f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

            handCoord = hrCoord(hh % 12, mm, hrHAND);
            g.DrawLine(new Pen(Color.Gray, 3f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));

            pictureBox1.Image = bmp;
            g.Dispose();
        }

        private int[] msCoord(int val, int hlen)
        {
            int[] coord = new int[2];
            val *= 6;

            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;
        }

        private int[] hrCoord(int hval, int mval, int hlen)
        {
            int[] coord = new int[2];

            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;
        }
    }
}


Now You Can Add An Analog Clock To Your Application.






Comments

Popular posts from this blog

Basic Prolog (List)

SOLID Principles