WinForm制作一个显示唯美的时钟,记录美好时刻

#头条创作挑战赛#

声明定义变量与初始化:

       //设置秒针,分钟,时针的长度
        int s_p = 150, m_p = 110, h_p = 80;
       private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "美美的时钟";
        }

添时时间控件:

private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");//获取当前系统时间并输出
            toolStripStatusLabel1.BackColor = System.Drawing.Color.Pink;  
            toolStripStatusLabel1.ForeColor = System.Drawing.Color.Brown;   
            mycolock();//调用mycolock()函数
        }

定义时钟函数:

        void mycolock()//定义时间函数
        {
            Graphics graphics = pictureBox1.CreateGraphics();//创建Graphics对象
            graphics.Clear(Color.White);//清空
            Pen mypen = new Pen(Color.Pink, 5);//设置为黑色,笔的粗细为2
            graphics.DrawEllipse(mypen, pictureBox1.ClientRectangle);//画表盘
            int h = DateTime.Now.Hour;//获取当前系统时间的小时
            int m = DateTime.Now.Minute;//获取当前系统时间的分钟
            int s = DateTime.Now.Second;//获取当前系统时间的秒

            Point C = new Point(pictureBox1.ClientRectangle.Width / 2, pictureBox1.Height / 2);//获取圆心坐标

            Point S = new Point((int)(C.X + (Math.Sin(6 * s * Math.PI / 180)) * s_p), (int)(C.Y - (Math.Cos(6 * s * Math.PI / 180)) * s_p));//获取秒针另一端点的坐标
            Point M = new Point((int)(C.X + (Math.Sin(6 * m * Math.PI / 180)) * m_p), (int)(C.Y - (Math.Cos(6 * m * Math.PI / 180)) * m_p));//获取分针另一端点的坐标
            Point H = new Point((int)(C.X + (Math.Sin(((30 * h) + m / 2) * Math.PI / 180)) * h_p), (int)(C.Y - (Math.Cos((30 * h + (m / 2)) * Math.PI / 180)) * h_p));//获取时针另一端点的坐标

            graphics.DrawLine(mypen, C, S);//画秒针
            mypen = new Pen(Color.Red, 9);//改变画笔的粗细,设置为3
            graphics.DrawLine(mypen, C, M);//画分针
            mypen = new Pen(Color.Green, 10);//改变画笔的粗细,设置为4
            graphics.DrawLine(mypen, C, H);//画时针
            //画刻度
            GraphicsState state = graphics.Save();
            state = graphics.Save();
            int dialRadius = Math.Min(C.X, C.Y);//取最小值
            Font hourFont = new Font("Arial", 15, FontStyle.Bold);//定义字号
            Brush brush = new SolidBrush(Color.Red);
            for (int i = 0; i < 60; i++)
            {
                string dian = ".";//设 . 代表刻度
                if (i % 5 == 0)//每到五个刻度,不显示  .
                {
                    dian = "";
                }
                Point point = new Point(-6, -6); //当为(0,0)时全部数字偏右下移,故手动调整
                double dd = Math.PI / 180 * i * (360 / 60); //每次转360/60度
                float x = point.X + (float)((dialRadius - 12) * Math.Cos(dd));
                float y = point.Y + (float)((dialRadius - 12) * Math.Sin(dd));
                //画圈
                graphics.DrawString(dian, hourFont, brush, x + 175, y + 160);

            }
            graphics.Restore(state);
            state = graphics.Save();
            for (int i = 0; i < 12; i++)
            {
                //已知圆中心占坐标(x0,y0),半径r,角度a0,则圆上任一点坐标(x,y)计算:
                //x = x0 + r * cos(ao * 3.14 /180) 
                //y = y0 + r * sin(ao * 3.14 /180) 
                Point point = new Point(-6, -6); //当为(0,0)时全部数字偏右下移,故手动调整
                double dd = Math.PI / 180 * i * (360 / 12); //每次转360/12度
                float x = point.X + (float)((dialRadius - 12) * Math.Cos(dd));
                float y = point.Y + (float)((dialRadius - 12) * Math.Sin(dd));
                //因为是从顺时钟3点钟开始画,所以索引i需要加上3
                int j = i + 3;
                if (j > 12)
                    j = j - 12;
                //画圈并显示数字
                graphics.DrawString(j.ToString(), hourFont, brush, x + 175, y + 160);
            }
            graphics.Restore(state);
        }
展开阅读全文

页面更新:2024-04-22

标签:分针   秒针   刻度   粗细   时针   坐标   时钟   函数   唯美   定义   美好   时刻   时间   系统

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top