C#画一个时钟

画一个时钟不一定要用到三角函数,今天在网上发现了一个极好的方法,避免了三角函数计算的误差,比较靠谱。
具体是通过依次旋转坐标轴,使坐标轴的Y轴分别和时针、分针、秒针在一条直线上,这样DrawLine的两端都在Y轴上了。

效果图如下:

代码如下:

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 MyClock
{
    public partial class Form1 : 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 Form1()
        {
            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_timer.Tick += new EventHandler(Timer_Tick);

            m_width = this.ClientSize.Width;
            m_height = this.ClientSize.Height;
            m_bgcolor = Color.AliceBlue;
            m_backcolor = Color.Gray;
            m_scalecolor = Color.Gray;
            m_seccolor = Color.Red;
            m_mincolor = Color.Blue;
            m_hourcolor = Color.Green;

            if (m_width > m_height)
            {
                m_radius = (float)(m_height - 8) / 2;
            }
            else
            {
                m_radius = (float)(m_width - 8) / 2;
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            m_graphic = e.Graphics;
            m_graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            m_graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //设置坐标原点
            m_graphic.TranslateTransform((float)(m_width/ 2), (float)(m_height/2));
            m_graphic.FillEllipse(new SolidBrush(m_bgcolor), -m_radius, -m_radius, m_radius * 2, m_radius * 2);
            //画外边框
            Pen pen = new Pen(m_backcolor, 2);
            m_graphic.DrawEllipse(pen, m_radius * (-1), m_radius * (-1), m_radius * 2, m_radius * 2);
            //画小刻度
            for (int i = 0; i < 60; i++)
            {
                m_graphic.FillRectangle(new SolidBrush(m_scalecolor), -2, 2 - m_radius, 4, 10);
                m_graphic.RotateTransform(6);
            }
            //画大刻度
            for (int i = 0; i < 12; i++)
            {
                m_graphic.FillRectangle(new SolidBrush(m_scalecolor), -3, 2 - m_radius, 6, 18);
                m_graphic.RotateTransform(30);
            }
            //获取当期时间
            int second = DateTime.Now.Second;
            int minute = DateTime.Now.Minute;
            int hour = DateTime.Now.Hour;
            //画秒针
            pen.Color = m_seccolor;
            m_graphic.RotateTransform(6 * second);
            m_graphic.DrawLine(pen, 0, 0, 0, (-1)*(float)(m_radius/1.5));
            //画分针
            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 / 2));
            //画时针
            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 / 2.5));
        }
    }
}
项目下载地址: http://yun.baidu.com/share/link?shareid=28100492&uk=3508115909

参考网址:http://www.cnblogs.com/feiyangqingyun/archive/2011/07/07/2100154.html
注:参考网址中的时针角度的计算应该是错的,在上面的代码中已改正。

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值