C# GDI+ 时钟表盘

一、设计如下图界面

        按键“打开时钟”按下后会出现表盘,按键“退出”按下后会关闭页面。

二、多线程初始化和函数初始化

        public Form1()
        {
            InitializeComponent();
            generateBtn();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        对按键 重命名

        void generateBtn()
        {
            Button bt1 = button1;
            bt1.Text = "打开时钟";
            Button bt2 = button2;
            bt2.Text = "退出";
        }

三、按键“打开时钟”按下

打开多线程,运行时钟相关程序。

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadStart thStart = new ThreadStart(Start);
            Thread thread = new Thread(thStart);
            thread.Priority = ThreadPriority.Highest;
            thread.IsBackground = true; //关闭窗体继续执行
            thread.Start();
        }

四、时钟主函数

        给定表盘圆心坐标和半径,初始化一些画笔颜色和画刷大小以及一些变量,通过半径和圆心计算表盘上各点的坐标,读取并记录电脑上的时间戳,计算时针分针秒针的角度,通过之前计算结果绘制表盘。

        每隔一秒(检测读取到的时、分、秒发生变化)擦除一遍页面,重新计算坐标点,重新绘制新的表盘。

        private void Start()
        {

            var graph = this.pictureBox1.CreateGraphics();

            double[] Sin = new double[60];
            double[] Cos = new double[60];
            float[] x = new float[60];
            float[] y = new float[60];
            float[] x1 = new float[60];
            float[] y1 = new float[60];
            float[] x2 = new float[60];
            float[] y2 = new float[60];
            float[] x3 = new float[60];
            float[] y3 = new float[60];
            float[] x4 = new float[60];
            float[] y4 = new float[60];
            float x5 =0;
            float y5 =0;
            float r0 = 0;


            string tradeTime = DateTime.Now.ToString("hhmmss",System.Globalization.DateTimeFormatInfo.InvariantInfo);
            var pencoler1 = new Pen(Color.Red);
            var pencoler2 = new Pen(Color.Yellow);
            var pencoler3 = new Pen(Color.Black);
            var pensize = new Pen(Color.Black, 3);
            var pensize1 = new Pen(Color.Red, 2);
            var pensize2 = new Pen(Color.Black, 4);

            int i;
            int z = 0;
            int x0 = 500;
            int y0 = 300;
            int r = 300;
            int sec, min, hour;
            int secn = 0, minn = 0, hourn = 0;
            int Time;

            for (i = 0; i < 60; i++)
            {
                //if (i == 0 || i == 30 || i == 15 || i == 45) {  continue; }
                double d = (((i * 6) - 90) * 3.1415) / 180;
                Sin[i] = Math.Sin(d);
                Cos[i] = Math.Cos(d);
                y[i] = (float)(Sin[i] * r) + y0;
                x[i] = (float)(Cos[i] * r) + x0;
                x1[i] = x[i] - (x[i] - x0) / 10;
                y1[i] = y[i] - (y[i] - y0) / 10;
                x2[i] = x[i] - (x[i] - x0) / 7 - r / 20;
                y2[i] = y[i] - (y[i] - y0) / 7 - r / 20;
                x3[i] = x[i] - (x[i] - x0) / 20;
                y3[i] = y[i] - (y[i] - y0) / 20;
                x4[i] = x[i] - (x[i] - x0) / 2;
                y4[i] = y[i] - (y[i] - y0) / 2;
                x5 = x0 - r / 40;
                y5 = y0 - r / 40;
                r0 = r / 20;

            }

            Brush penbrush = new SolidBrush(Color.Black);
            Font penfont = new Font("华文行楷", r / 20);

            for (; ; )
            {
                tradeTime = DateTime.Now.ToString("hhmmss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
                Time = int.Parse(tradeTime);
                sec = Time % 100;
                min = Time / 100 % 100;
                hour = Time / 10000;
                if (hour == 12) { hour = 0; }

                if (secn != sec || minn != min || hourn != hour)
                {
                    //MessageBox.Show(sec.ToString());
                    graph.Clear(this.BackColor);
                    //graph.DrawEllipse(pencoler2, x0 - r, y0 - r, 2*r, 2*r);

                    for (i = 0; i < 60; i++)
                    {
                        if (i % 5 == 0)
                        {
                            z = i / 5;
                            if (z == 0) z = 12;
                            graph.DrawLine(pensize, x[i], y[i],x1[i],y1[i]);
                            graph.DrawString(z.ToString(), penfont, penbrush, x2[i], y2[i]);
                        }
                        else
                        {
                            graph.DrawLine(pencoler3, x[i], y[i], x3[i], y3[i]);
                        }
                    }
                    graph.DrawLine(pensize1, x0, y0, x[sec], y[sec]);
                    graph.DrawLine(pensize2, x0, y0, x[min], y[min]);
                    graph.DrawLine(pensize2, x0, y0, x4[hour*5], y4[hour*5]);
                    //graph.DrawPie(pencoler2, x0-r, y0-r, 2*r, 2*r, sec * 6-90, 1);
                    //graph.DrawPie(pencoler3, x0-r , y0-r , r*2, r*2, min * 6-90, 1);
                    //graph.DrawPie(pencoler3, x0-r/2 , y0-r/2 , r, r, hour*30-90, 2);

                    secn = sec;
                    minn = min;
                    hourn = hour;
                    graph.FillEllipse(new SolidBrush(Color.Yellow), x5 , y5 , r0, r0);
                    continue;
                }
            }
        }

五、“退出”按键按下后

        得益于多线程的使用,在表盘一直处于死循环监测时间变化的同时,依然能检测到Button的按下。

        “退出”按下后,退出页面。

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

六、表盘上各点的计算

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一些关于 C# GDI+ 的面试题: 1. 什么是 GDI+?它有什么作用? GDI+ 是 .NET Framework 中的图形设备界面,它提供了一组开发人员可以用来创建高质量图形和图像的类和方法。通过 GDI+,我们可以创建各种各样的图形和图像,包括线条、矩形、圆形、多边形、文本、图片等,还可以进行图像处理、图形绘制等操作。 2. GDI+ 和 GDI 的区别是什么? GDI+ 是 GDI 的升级版,在功能上比 GDI 更加强大,它提供了更多的绘图方法和更高级的对象模型,同时还支持 alpha 通道、图像处理等高级功能。此外,GDI+ 还支持更多的图像格式,包括 BMP、JPEG、PNG、GIF、TIFF 等。 3. 如何使用 GDI+ 绘制一条直线? 在 C# 中,我们可以使用 System.Drawing 命名空间中的 Pen 和 Graphics 对象来绘制直线,具体的代码示例如下: ``` // 创建 Pen 对象 Pen pen = new Pen(Color.Black); // 创建 Graphics 对象 Graphics g = this.CreateGraphics(); // 绘制直线 g.DrawLine(pen, 0, 0, 100, 100); ``` 4. 如何使用 GDI+ 绘制一个矩形? 和绘制直线类似,我们可以使用 System.Drawing 命名空间中的 Pen 和 Graphics 对象来绘制矩形,具体的代码示例如下: ``` // 创建 Pen 对象 Pen pen = new Pen(Color.Black); // 创建 Graphics 对象 Graphics g = this.CreateGraphics(); // 创建 Rectangle 对象 Rectangle rect = new Rectangle(0, 0, 100, 100); // 绘制矩形 g.DrawRectangle(pen, rect); ``` 5. 如何使用 GDI+ 绘制一个圆形? 和绘制直线、矩形类似,我们可以使用 System.Drawing 命名空间中的 Pen 和 Graphics 对象来绘制圆形,具体的代码示例如下: ``` // 创建 Pen 对象 Pen pen = new Pen(Color.Black); // 创建 Graphics 对象 Graphics g = this.CreateGraphics(); // 创建 Rectangle 对象 Rectangle rect = new Rectangle(0, 0, 100, 100); // 绘制圆形 g.DrawEllipse(pen, rect); ``` 以上是几个常见的关于 C# GDI+ 的面试题和答案,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值