C# 简单的PictureBox表盘时钟表。

最近做了一个时钟表,图片的样子是如下这样的:

附上代码:

#region 时钟显示
        /// <summary>
        /// 时钟
        /// 李金轩 2020-11-13 16:29:31
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picClock_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                Graphics g;
                Pen pen;
                g = e.Graphics;
                int w = picClock.Width;//pictureBox宽
                int h = picClock.Height;//pictureBox高
                int x1 = picClock.Location.X;//pictureBox坐标X
                int y1 = picClock.Location.Y;//pictureBox坐标Y
                g.SmoothingMode = SmoothingMode.AntiAlias;//防止出现锯齿
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.TranslateTransform(w / 2, h / 2);//重新设置坐标原点    
                g.FillEllipse(Brushes.SaddleBrown, -4, -4, 7, 7);//绘表心
                int dialRadius = Math.Min(w, h) / 2;//圈的半径。math.min,Returns the smaller of two integers that have been compared with one another.
                for (int i = 0; i < 60; i++)//60分钟所以分60小块,我们画一个小圆点组成的圆圈
                {
                    int radius = 4;
                    if (i % 5 == 0)//被5整除即12个小时,用大点
                    { radius = 7; }
                    g.FillEllipse(Brushes.AliceBlue, new Rectangle(-radius, -(dialRadius - 5), radius, radius));//画个圆
                    g.RotateTransform(360 / 60);//旋转角度,以°为单位。每次转6°,一分钟一个点
                }

                //画秒针
                int second = int.Parse(DateTime.Now.ToString("ss"));//画秒针时用于计算角度
                pen = new Pen(Color.AliceBlue, 1);
                g.RotateTransform(6 * second);
                float y = (float)((-1) * (h / 2.4));
                g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
                pen.Dispose();

                //画分针
                int minute = int.Parse(DateTime.Now.ToString("mm"));//画分针
                pen = new Pen(Color.AliceBlue, 2);
                g.RotateTransform(-6 * second); //恢复系统偏移量,再计算下次偏移
                g.RotateTransform((float)(second * 0.1 + minute * 6));
                y = (float)((-1) * ((h - 20) / 2.6));
                g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
                pen.Dispose();


                //画时针
                int hour = int.Parse(DateTime.Now.ToString("HH"));//画时针
                pen = new Pen(Color.AliceBlue, 4);
                g.RotateTransform((float)(-second * 0.1 - minute * 6));//恢复系统偏移量,再计算下次偏移
                g.RotateTransform((float)(second * 0.01 + minute * 0.1 + hour * 30));
                y = (float)((-1) * ((h - 55) / 2.85));
                g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
                pen.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

首先,这个事件是写在图片控件里的,也就是PictureBox控件的Paint事件,代表在控件需要要重新绘制时才会发生的,也就是你的三个指针是要每秒刷新一次的,然后这个事件就会运行一次。

接着新建一个Timer时间控件写上重画时钟图片的方法,强制控件使其工作区无效并立即重绘自己和任何子控件(Refresh)

        /// <summary>
        /// 实时显示日期和时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerTime_Tick(object sender, EventArgs e)
        {
            try
            {
                //重新画时钟图片
                picClock.Refresh();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"温馨提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            
        }

表盘和指针都是可以改变颜色的,有Color的地方,都是可以自由更改的,长短,大小,粗细,这些可以根据自己的需求去改变参数值。

猜你喜欢

转载自blog.csdn.net/weixin_37081112/article/details/110307857