代码如下
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;namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Clock myclock = new Clock();
        private void timer1_Tick(object sender, EventArgs e)
        {
            Pen mypen = new Pen(Color.Black, 3);
            Point pt1, pt2;
            for (int Hour = 0; Hour < 12; Hour++)
            {
                pt1 = new Point(Convert.ToInt32(myclock.Center_P.X + 100 * Math.Sin(Hour * Math.PI / 6.0)), Convert.ToInt32(myclock.Center_P.Y - 100 * Math.Cos(Hour * Math.PI / 6.0)));
                pt2 = new Point(Convert.ToInt32(myclock.Center_P.X + 110 * Math.Sin(Hour * Math.PI / 6.0)), Convert.ToInt32(myclock.Center_P.Y - 110 * Math.Cos(Hour * Math.PI / 6.0)));
                System.Drawing.Graphics g = this.CreateGraphics();
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.DrawLine(mypen, pt1, pt2);
            }
        }        private void button1_Click(object sender, EventArgs e)
        {        }
      
        private void timer2_Tick(object sender, EventArgs e)
        {
            Pen Hour_Pen = new Pen(Color.Black, 4);
            Pen Minute_Pen = new Pen(Color.Black, 3);
            Pen Second_Pen = new Pen(Color.Black, 1);
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.Clear(Color.White);
            g.DrawLine(Second_Pen, myclock.Center_P, myclock.Second_P);
            g.DrawLine(Minute_Pen, myclock.Center_P, myclock.Minute_P);
            g.DrawLine(Hour_Pen, myclock.Center_P, myclock.Hour_P);
            myclock.CalculateTime();
    }
      public class Clock
    {        public  int Hour;
        public  int Minute;
        public  int Second;
        public Point Center_P;
        public  Point Hour_P;
        public  Point Minute_P;
        public  Point Second_P;
        
        public Clock()
        {
            Hour = DateTime.Now.Hour;
            Minute = DateTime.Now.Minute;
            Second = DateTime.Now.Second;
            Center_P = new Point(150, 150);
            Second_P= new Point(Convert.ToInt32(Center_P.X +100* Math.Sin(Second * Math.PI / 30.0)), Convert.ToInt32(Center_P.Y - 100*Math.Cos(Second * Math.PI / 30.0)));
            Minute_P = new Point(Convert.ToInt32(Center_P.X + 75 * Math.Sin(Minute * Math.PI / 30.0 + Second * Math.PI / 1800.0)), Convert.ToInt32(Center_P.Y - 75 * Math.Cos(Minute * Math.PI / 30.0 + Second * Math.PI / 1800.0)));
            Hour_P = new Point(Convert.ToInt32(Center_P.X + 50 * Math.Sin((Hour * 30.0 + Minute * 0.5 + Second / 120.0) / 360.0 * 2 * Math.PI)), Convert.ToInt32(Center_P.Y - 50 * Math.Cos((Hour * 30.0 + Minute * 0.5 + Second / 120.0) / 360.0 * 2 * Math.PI)));
        }
   public void CalculateTime()
        {
            Second++;
           if (Second == 60)
            {
                Second = 0;
                Minute++;
                if (Minute == 60)
                    Minute = 0;
                    Hour++;
                    if (Hour == 12)
                    {
                        Hour = 0;
                    }
                }
            }
            
            Second_P = new Point(Convert.ToInt32(Center_P.X + 100 * Math.Sin(Second * Math.PI / 30.0)), Convert.ToInt32(Center_P.Y - 100 * Math.Cos(Second * Math.PI / 30.0)));
            Minute_P = new Point(Convert.ToInt32(Center_P.X + 75 * Math.Sin(Minute * Math.PI / 30.0 + Second * Math.PI / 1800.0)), Convert.ToInt32(Center_P.Y - 75 * Math.Cos(Minute * Math.PI / 30.0 + Second * Math.PI / 1800.0)));
            //Hour_P = new Point(Convert.Tolnt32(Center_P.X + 50 * Math.Sin(Hour * 30.0 + Minute * 0.5 + Second / 120.0) / 360.0 * 2 * Math.PI)), Convert.Tolnt32(Center_P.Y - 50 * Math.Cos(Hour * 30.0 + Minute * 0.5 + Second /120.0) /360.0 * 2 * Math.PI)));
            Hour_P = new Point(Convert.ToInt32(Center_P.X + 50 * Math.Sin((Hour * 30.0 + Minute * 0.5 + Second / 120.0) / 360.0 * 2 * Math.PI)), Convert.ToInt32(Center_P.Y - 50 * Math.Cos((Hour * 30.0 + Minute * 0.5 + Second / 120.0) / 360.0 * 2 * Math.PI)));            
        }
      }
}

解决方案 »

  1.   

    timer1 是否设置为可用 即 Enabled = true;
      

  2.   

             都没有 timer1.Interval = 1000;
                timer2.Interval = 1000;
                timer1.Start();
              timer2.Start();
    这几行。我加上去就直接Error 1 The name 'timer1' does not exist in the current context
    The name 'timer2' does not exist in the current context
    The name 'timer3' does not exist in the current context
    The name 'timer4' does not exist in the current context
      

  3.   

     画图代码放到Form1_Paint中试试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;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void drawClock()
            {
                Graphics graphic = this.CreateGraphics();
                Rectangle rect = new Rectangle(30, 76, 180, 180);            graphic.Clear(Color.White);            Pen myPen = new Pen(Color.Black, 1);
                graphic.DrawEllipse(myPen, rect.Width / 2, rect.Height / 2, 180, 180);//画表盘
            }        private void Form1_Paint(object sender, PaintEventArgs e)
            {
                drawClock();
            }
        }
    }