WinForm 圆形时钟

   DateTime Time;
        private void timer_Tick(object sender,EventArgs e)
        {
            this.Time = DateTime.Now;
            Invalidate();
        }
        private void UCTime_Paint(object sender, PaintEventArgs e)
        {
            Graphics dc = e.Graphics;
            Pen pn = new Pen(ForeColor);
            SolidBrush br = new SolidBrush(ForeColor);
            InitCoordinates(dc);           //设置绘画的坐标变换
            DrawDots(dc, br);              //绘画范围界面
            DrawHourHand(dc, pn);          //绘画时针,计算旋转的度数
            DrawMinuteHand(dc, pn);        //绘画分针
            DrawSecondHand(dc, pn);        //绘画秒针

        }
        /// <summary>
        /// 设置绘画的坐标变换
        /// </summary>
        /// <param name="dc">画布</param>
        protected void InitCoordinates(Graphics dc)
        {
            if (this.Width == 0 || this.Height == 0)
                return;
            dc.TranslateTransform(this.Width / 2, this.Height / 2);     //平移坐标原点
            dc.ScaleTransform(this.Height / 250F, this.Width / 250F);   //调整比例大小
        }
        /// <summary>
        /// 绘画范围界面
        /// </summary>
        /// <param name="dc">画布</param>
        /// <param name="brush">笔刷</param>
        protected void DrawDots(Graphics dc, Brush brush)
        {
            int iSize;
            for (int i = 0; i <= 59; i++)
            {
                if (i % 5 == 0)
                    iSize = 15;
                else
                    iSize = 5;
                dc.FillEllipse(brush,-iSize/2,-100-iSize/2,iSize,iSize);
                dc.RotateTransform(6);
            }
        }
        /// <summary>
        /// 绘画时针
        /// </summary>
        /// <param name="grfx">画布</param>
        /// <param name="pn">笔</param>
        protected virtual void DrawHourHand(Graphics grfx, Pen pn)
        {
            GraphicsState gs = grfx.Save();
            //12个小时一共360度,一刻钟30度
            grfx.RotateTransform(360.0F * Time.Hour / 12 + 30.0F * Time.Minute / 60);
            grfx.DrawLine(pn, 0, 0, 0, -50);
            grfx.Restore(gs);
        }
        /// <summary>
        /// 绘画分针
        /// </summary>
        /// <param name="grfx">画布</param>
        /// <param name="pn">笔</param>
        protected virtual void DrawMinuteHand(Graphics grfx, Pen pn)
        {
            GraphicsState gs = grfx.Save();
            //每分钟6度
            grfx.RotateTransform(360.0F * Time.Minute / 60 + 6.0F * Time.Second / 60);
            grfx.DrawLine(pn, 0, 0, 0, -70);
            grfx.Restore(gs);
        }
        /// <summary>
        /// 绘画秒针
        /// </summary>
        /// <param name="grfx">画布</param>
        /// <param name="pn">笔</param>
        protected virtual void DrawSecondHand(Graphics grfx, Pen pn)
        {
            GraphicsState gs = grfx.Save();
            grfx.RotateTransform(360.0F * Time.Second / 60);
            grfx.DrawLine(pn, 0, 0, 0, -100);
            grfx.Restore(gs);
        }

posted @ 2013-01-09 09:29  liyx0618  阅读(338)  评论(0编辑  收藏  举报