用C#实现电子时钟功能

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 电子时钟
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        const int s_pinlen = 100;
        const int m_pinlen = 75;
        const int h_pinlen = 50;

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.timer1.Enabled  = true;
        }

        private void myClock(int h, int m, int s)
        {
            Graphics myGraphics = this.pictureBox1.CreateGraphics();
            myGraphics.Clear(Color.White);
            Pen myPen = new Pen(Color.Black, 1);//设置画笔的颜色和宽度
            myGraphics.DrawEllipse(myPen, pictureBox1.ClientRectangle);
            Point CPoint = new Point(pictureBox1.ClientRectangle.Width / 2,
                pictureBox1.ClientRectangle.Height / 2);
            Point SPoint = new Point((int)(CPoint.X + (Math.Sin(6 * s * Math.PI / 180)) * s_pinlen),
                (int)(CPoint.Y - (Math.Cos(6 * s * Math.PI / 180)) * s_pinlen));
            Point MPoint = new Point((int)(CPoint.X + (Math.Sin(6 * m * Math.PI / 180)) * m_pinlen),
                (int)(CPoint.Y - (Math.Cos(6 * m * Math.PI / 180)) * m_pinlen));
            Point HPoint=new Point ((int)(CPoint .X +(Math .Sin (((30*h)+(m/2))*Math .PI /180))*h_pinlen ),
                (int)(CPoint .Y -(Math .Cos(((30*h)+(m/2))*Math .PI /180))*h_pinlen ));
            myGraphics.DrawLine(myPen, CPoint, SPoint); //绘制秒针
            myPen = new Pen(Color.Black, 2);
            myGraphics.DrawLine(myPen, CPoint, MPoint); //绘制分针
            myPen = new Pen(Color.Black, 4);
            myGraphics.DrawLine(myPen, CPoint, HPoint); //绘制时针
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int h = DateTime.Now.Hour;
            int m = DateTime.Now.Minute;
            int s = DateTime.Now.Second;
            myClock(h, m, s);
            this.Text = "现在时间:" + h.ToString() + ":" + m.ToString() + ":" + s.ToString();
           
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值