C# Simple PictureBox dial clock watch.

I recently made a clock watch, the picture looks like this:

Attach the code:

#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

First of all, this event is written in the picture control, that is, the Paint event of the PictureBox control, which means that it will only happen when the control needs to be repainted, that is, your three pointers are refreshed every second, and then this The event will run once.

Then create a new Timer time control and write a method to redraw the clock picture, force the control to invalidate its workspace and immediately redraw itself and any child controls (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);
            }
            
        }

Both the dial and the pointer can change the color. Where there is Color, you can freely change the length, size, thickness, and these parameters can be changed according to your needs.

Guess you like

Origin blog.csdn.net/weixin_37081112/article/details/110307857