CS_Prj01 用C#生成一个桌面指针式时钟程序

发布于:2025-06-19 ⋅ 阅读:(12) ⋅ 点赞:(0)

1.运行结果

2.程序

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 WindowsClock
{
    public partial class Form1 : Form
    {
        private Timer timer;
        private Point center;
        private int clockRadius;

        public Form1()
        {
            InitializeComponent();
            this.Text = "指针式时钟";
            this.ClientSize = new Size(400, 450);
            this.DoubleBuffered = true;
            this.Paint += ClockForm_Paint;

            // 设置计时器每秒更新一次
            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += Timer_Tick;
            timer.Start();
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            this.Invalidate(); // 触发重绘
        }
        private void ClockForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            // 计算中心点和半径
            center = new Point(ClientSize.Width / 2, ClientSize.Height / 2 - 25);
            clockRadius = Math.Min(ClientSize.Width, ClientSize.Height) / 2 - 40;

            // 绘制时钟背景
            DrawClockFace(g);

            // 获取当前时间
            DateTime now = DateTime.Now;

            // 绘制指针
            DrawHourHand(g, now.Hour, now.Minute);
            DrawMinuteHand(g, now.Minute, now.Second);
            DrawSecondHand(g, now.Second);

            // 绘制中心点
            DrawCenterCap(g);

            // 绘制数字时间显示
            DrawDigitalTime(g, now);
        }
        private void DrawClockFace(Graphics g)
        {
            // 绘制外圆
            g.FillEllipse(Brushes.White, center.X - clockRadius, center.Y - clockRadius,
                         clockRadius * 2, clockRadius * 2);
            g.DrawEllipse(new Pen(Color.Black, 3), center.X - clockRadius, center.Y - clockRadius,
                         clockRadius * 2, clockRadius * 2);

            // 绘制刻度
            for (int i = 0; i < 60; i++)
            {
                double angle = Math.PI * 2 * i / 60.0;
                int innerLength = i % 5 == 0 ? 15 : 5;
                int outerLength = i % 5 == 0 ? 25 : 15;

                int x1 = center.X + (int)((clockRadius - innerLength) * Math.Sin(angle));
                int y1 = center.Y - (int)((clockRadius - innerLength) * Math.Cos(angle));
                int x2 = center.X + (int)((clockRadius - outerLength) * Math.Sin(angle));
                int y2 = center.Y - (int)((clockRadius - outerLength) * Math.Cos(angle));

                g.DrawLine(new Pen(Color.Black, i % 5 == 0 ? 3 : 1), x1, y1, x2, y2);
            }

            // 绘制数字
            Font font = new Font("Arial", 14, FontStyle.Bold);
            for (int i = 1; i <= 12; i++)
            {
                double angle = Math.PI * 2 * (i - 3) / 12.0;
                int x = center.X + (int)((clockRadius - 40) * Math.Sin(angle));
                int y = center.Y - (int)((clockRadius - 40) * Math.Cos(angle));
                g.DrawString(i.ToString(), font, Brushes.Black, x - 10, y - 10);
            }
        }
        private void DrawHourHand(Graphics g, int hours, int minutes)
        {
            double angle = Math.PI * 2 * (hours % 12 + minutes / 60.0) / 12.0 - Math.PI / 2;
            int length = clockRadius * 3 / 5;
            DrawHand(g, angle, length, Color.Black, 8);
        }

        private void DrawMinuteHand(Graphics g, int minutes, int seconds)
        {
            double angle = Math.PI * 2 * (minutes + seconds / 60.0) / 60.0 - Math.PI / 2;
            int length = clockRadius * 4 / 5;
            DrawHand(g, angle, length, Color.Black, 5);
        }

        private void DrawSecondHand(Graphics g, int seconds)
        {
            double angle = Math.PI * 2 * seconds / 60.0 - Math.PI / 2;
            int length = clockRadius * 4 / 5;
            DrawHand(g, angle, length, Color.Red, 2);
        }

        private void DrawHand(Graphics g, double angle, int length, Color color, int width)
        {
            int x = center.X + (int)(length * Math.Cos(angle));
            int y = center.Y + (int)(length * Math.Sin(angle));

            Pen pen = new Pen(color, width);
            pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
            g.DrawLine(pen, center, new Point(x, y));
        }
        private void DrawCenterCap(Graphics g)
        {
            int capSize = 10;
            g.FillEllipse(Brushes.Red, center.X - capSize, center.Y - capSize, capSize * 2, capSize * 2);
        }

        private void DrawDigitalTime(Graphics g, DateTime time)
        {
            string timeString = time.ToString("HH:mm:ss");
            Font font = new Font("Arial", 24, FontStyle.Bold);
            SizeF size = g.MeasureString(timeString, font);

            int x = center.X - (int)(size.Width / 2);
            int y = center.Y + clockRadius + 20;

            g.DrawString(timeString, font, Brushes.Blue, x, y);
        }
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到