Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
can any one tell me how to make an analog clock in c#.net(window application)


thanks in advance
Posted

Use a timer, and then use trig to work out the points to draw your lines on the circle.
 
Share this answer
 
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace Clock
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Paint += new PaintEventHandler(drawclock);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
        }
        private void drawclock(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rec = new Rectangle(20,20,250,250);
            LinearGradientBrush linearbrush = new LinearGradientBrush(rec, Color.Yellow, 
                                                                      Color.Green,225);
            g.FillEllipse(linearbrush,20, 20, 200, 200);
            linearbrush.LinearColors = new Color[] { Color.Yellow, Color.Green, };
            g.FillEllipse(linearbrush,30, 30, 180, 180);
            linearbrush.LinearColors = new Color[] { Color.Green, Color.Yellow };
            g.FillEllipse(linearbrush,33,33,174,174);
            SolidBrush solidbrush = new SolidBrush(Color.White);
            Font textFont = new Font("Arial Bold", 12F);
            g.DrawString("12", textFont, solidbrush, 109, 40);
            g.DrawString("11", textFont, solidbrush, 75, 50);
            g.DrawString("10", textFont, solidbrush, 47, 75);
            g.DrawString("9", textFont, solidbrush, 43, 110);
            g.DrawString("8", textFont, solidbrush, 52, 145);
            g.DrawString("7", textFont, solidbrush, 75, 170);
            g.DrawString("6", textFont, solidbrush, 113, 180);
            g.DrawString("5", textFont, solidbrush, 150, 170);
            g.DrawString("4", textFont, solidbrush, 173, 145);
            g.DrawString("3", textFont, solidbrush, 182, 110);
            g.DrawString("2", textFont, solidbrush, 173, 75);
            g.DrawString("1", textFont, solidbrush, 150, 50);
            g.TranslateTransform(120,120,MatrixOrder.Append);
            int hour = DateTime.Now.Hour;
            int min = DateTime.Now.Minute;
            int sec = DateTime.Now.Second;
            // Create Pens
            Pen hourPen = new Pen(Color.White, 2);
            Pen minutePen = new Pen(Color.LightGray, 2);
            Pen secondPen = new Pen(Color.Red, 1);
            // Create angles
            double secondAngle = 2.0 * Math.PI * sec / 60.0;
            double minuteAngle = 2.0 * Math.PI * (min + sec / 60.0) / 60.0;
            double hourAngle = 2.0 * Math.PI * (hour + min / 60.0) / 12.0;
            // Set centre point
            Point centre = new Point(0, 0);
            // Draw Hour Hand
            Point hourHand = new Point((int)(40 * Math.Sin(hourAngle)),
                                        (int)(-40 * Math.Cos(hourAngle)));
            g.DrawLine(hourPen, centre, hourHand);
            // Draw Minute Hand
            Point minHand = new Point((int)(70 * Math.Sin(minuteAngle)),
                                       (int)(-70 * Math.Cos(minuteAngle)));
            g.DrawLine(minutePen, centre, minHand);
            // Draw Second Hand
            Point secHand = new Point((int)(70 * Math.Sin(secondAngle)),
                                       (int)(-70 * Math.Cos(secondAngle)));
            g.DrawLine(secondPen, centre, secHand);
            Invalidate();
            
        }
    }
}


[Modified: added <pre> tags]

From WW: By the way, we try not to do people's homework for them. We give them ideas and point them in the right direction, but giving them the exact code they need isn't helping them. If they are having problems, yeah, give them the corrections, but don't hand fee them their entire project.
 
Share this answer
 
v3
i'm much haapy to find this code for making your clock,
i will empliment this code and find a beutiful clock.

thnx of loat for help me

sir one questions,
agin..

there is three clock is visible, i want only one clock, help me ???
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900