windowsForm基础编写时钟

界面展示:

windowsForm基础编写时钟windowsForm基础编写时钟

用到的控件有:Button(按钮)Timer控件(它的设置代码里面有)点击按钮进行刷新

源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HClock
{
    public partial class Clock : Form
    {
        private Graphics m_graphic;
        private Timer m_timer;
        private float m_width;
        private float m_height;
        private Color m_bgcolor;
        private Color m_backcolor;
        private Color m_scalecolor;
        private Color m_seccolor;
        private Color m_mincolor;
        private Color m_hourcolor;
        private float m_radius;

        public Clock()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            m_timer = new Timer();
            m_timer.Interval = 1000;
            m_timer.Enabled = true;
            m_width=this .ClientSize.Width ;
            m_height =this .ClientSize.Height ;
            m_bgcolor =Color.GreenYellow;
            m_backcolor=Color.Gold;
            m_scalecolor =Color .Black ;
            m_seccolor =Color .Gold ;
            m_mincolor =Color.Blue;
            m_hourcolor =Color .Red  ;
             
            if (m_width>m_height )
            {
                m_radius =(float )(m_height -8)/2;
            }
            else
            {
                m_radius =(float )(m_width -8)/2;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Invalidate();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            m_graphic =  g ;
            m_graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            m_graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //设置坐标原点
            Pen pen = new Pen(m_backcolor, 2);
            m_graphic.FillEllipse(new SolidBrush(m_bgcolor), 50, 50, 200, 200);
            m_graphic.DrawEllipse(pen, 46, 46, 208, 208);
            m_graphic.DrawEllipse(pen, 50, 50, 200, 200);
            m_graphic.TranslateTransform(150, 150);

            //画小刻度
            for (int i = 0; i < 60; i++)
            {
                m_graphic.FillRectangle(new SolidBrush(m_scalecolor), -2, 2 - 100, 4, 10);
                m_graphic.RotateTransform(6);
            }
            //画大刻度
            for (int i = 0; i < 12; i++)
            {
                m_graphic.FillRectangle(new SolidBrush(m_scalecolor), -3, 2 - 100, 6, 18);
                m_graphic.RotateTransform(30);
            }
            Font drawFont = new Font("Arial", 12);

            SolidBrush drawBrush = new SolidBrush(Color.Black);
            m_graphic.DrawString("6", drawFont, drawBrush, -7, 70);
            m_graphic.DrawString("12", drawFont, drawBrush, -9, -80);
            m_graphic.DrawString("3", drawFont, drawBrush, 70, -7);
            m_graphic.DrawString("9", drawFont, drawBrush, -80, -7);
            //获取当期时间
            int second = DateTime.Now.Second;
            int minute = DateTime.Now.Minute;
            int hour = DateTime.Now.Hour;
            string s = Convert.ToString(second);
            string m = Convert.ToString(minute);
            string h = Convert.ToString(hour);
            if (second >= 0 && second <= 9)
            {
                s = "0" + s;
            }
            if (minute >= 0 && minute <= 9)
            {
                m = "0" + m;
            }
            if (hour >= 0 && hour <= 9)
            {
                h = "0" + h;
            }
            Font f1 = new Font("粗体", 12, FontStyle.Regular);
            StringFormat sf1 = new StringFormat();
            SolidBrush s1 = new SolidBrush(Color.Black);
            Rectangle r1 = new Rectangle(-30, 10, 80, 20);
            //参数分别为左上角矩形坐标,宽度和长度

            g.FillRectangle(Brushes.GhostWhite, r1);//填充颜色
            g.DrawString(  h + ":" +m +":"+ s, f1, s1, r1, sf1);
            g.DrawString(  h + ":" + m + ":" + s, f1, s1, r1, sf1);
            //画秒针

            pen.Color = m_seccolor;
            m_graphic.RotateTransform(6 * second);
            m_graphic.DrawLine(pen, 0, 0, 0, (-1) * (float)(m_radius / 2));
            pen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
            //画分针
             
            pen.Color = m_mincolor;
            m_graphic.RotateTransform(-6 * second);
            m_graphic.RotateTransform((float)(0.1 * second + 6 * minute));
            m_graphic.DrawLine(pen, 0, 0, 0, (-1) * (float)(m_radius / 3));
            pen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
            //画时针
             
            pen.Color = m_hourcolor;
            m_graphic.RotateTransform((float)(0.1 * second + 6 * minute) * (-1));
            m_graphic.RotateTransform((float)(30 / 3600 * second + 30 / 60 * minute + hour * 30));
            m_graphic.DrawLine(pen, 0, 0, 0, (-1) * (float)(m_radius / 4));
            pen.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
        }
    }
}