一个简单的钟表程序,请帮忙改改

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private PictureBox pictureBox1 = new PictureBox();
        private Timer time = new Timer();
        private void Form1_Load(object sender, System.EventArgs e)
        {
            // Dock the PictureBox to the form and set its background to white.
            pictureBox1.Dock = DockStyle.Fill;
            pictureBox1.BackColor = Color.White;
            pictureBox1.Location = new Point(0, 0);
            //pictureBox1.
            // Connect the Paint event of the PictureBox to the event handler method.
            pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);


            // Add the PictureBox control to the Form. 
            this.Controls.Add(pictureBox1);
            time.Interval = 1000;
            time.Tick += new EventHandler(timer1_Tick);
            time.Start();
        }

        private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            // Create a local version of the graphics object for the PictureBox.
            Graphics g = e.Graphics;

            int secondHand = 100;
            int pointX = pictureBox1.Width / 2;
            int pointY = pictureBox1.Height / 2;
            int second = DateTime.Now.Second;
            int hour = DateTime.Now.Hour;
            int minu = DateTime.Now.Minute;
           //textBox1.Text = second.ToString();

            //hour hand
            double currentXH = secondHand * Math.Sin(Math.PI * 2 / 12 * hour) * 0.5;
            double currentYH = secondHand * Math.Cos(Math.PI * 2 / 12 * hour) * 0.5;

            //minute hand
            double currentXM = secondHand * Math.Sin(Math.PI * 2 / 60 * minu) * 0.8;
            double currentYM = secondHand * Math.Cos(Math.PI * 2 / 60 * minu) * 0.8;

            //second hand
            double currentX = secondHand * Math.Sin(Math.PI * 2 / 60 * second);
            double currentY = secondHand * Math.Cos(Math.PI * 2 / 60 * second);

            //draw clock 
            g.DrawEllipse(Pens.Blue, pointX - secondHand, pointY - secondHand, secondHand * 2, 2 * secondHand);

            Pen mi = new Pen(Color.Black, 1);
            Pen ho = new Pen(Color.Red, 2);

            for (int i = 0; i < 60; i++)
            {
                double degree = Math.PI * 2 / 60 * i;

                float xx = Convert.ToSingle(Math.Sin(degree)*secondHand+pointX);
                float yy = Convert.ToSingle(pointY-Math.Cos(degree) * secondHand);

                float xxmove = Convert.ToSingle(Math.Sin(degree) * 5);
                float yymove = Convert.ToSingle(Math.Cos(degree) * 5);

                if (i % 5 == 0)
                {
                    g.DrawLine(ho, xx- xxmove, yy + yymove, xx, yy);
                }
                else
                    g.DrawLine(mi, xx - xxmove, yy + yymove, xx, yy);
            }

            g.DrawLine(new Pen(Color.Blue, 4), pointX, pointY, Convert.ToInt32(pointX + currentXH), Convert.ToInt32(pointY - currentYH));
            g.DrawLine(new Pen(Color.Tomato, 2), pointX, pointY, Convert.ToInt32(pointX + currentXM), Convert.ToInt32(pointY - currentYM));
            g.DrawLine(Pens.Black, pointX, pointY, Convert.ToInt32(pointX + currentX), Convert.ToInt32(pointY - currentY));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }


    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值