溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#如何實(shí)現(xiàn)時(shí)鐘表盤

發(fā)布時(shí)間:2022-06-14 09:23:01 來(lái)源:億速云 閱讀:161 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“C#如何實(shí)現(xiàn)時(shí)鐘表盤”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

一、設(shè)計(jì)如下圖界面

按鍵“打開時(shí)鐘”按下后會(huì)出現(xiàn)表盤,按鍵“退出”按下后會(huì)關(guān)閉頁(yè)面。

C#如何實(shí)現(xiàn)時(shí)鐘表盤

二、多線程初始化和函數(shù)初始化

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

對(duì)按鍵 重命名

void generateBtn()
 {
            Button bt1 = button1;
            bt1.Text = "打開時(shí)鐘";
            Button bt2 = button2;
            bt2.Text = "退出";
}

三、按鍵“打開時(shí)鐘”按下

打開多線程,運(yùn)行時(shí)鐘相關(guān)程序。

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; //關(guān)閉窗體繼續(xù)執(zhí)行
            thread.Start();
        }

四、時(shí)鐘主函數(shù)

給定表盤圓心坐標(biāo)和半徑,初始化一些畫筆顏色和畫刷大小以及一些變量,通過(guò)半徑和圓心計(jì)算表盤上各點(diǎn)的坐標(biāo),讀取并記錄電腦上的時(shí)間戳,計(jì)算時(shí)針?lè)轴樏脶樀慕嵌?,通過(guò)之前計(jì)算結(jié)果繪制表盤。

每隔一秒(檢測(cè)讀取到的時(shí)、分、秒發(fā)生變化)擦除一遍頁(yè)面,重新計(jì)算坐標(biāo)點(diǎn),重新繪制新的表盤。

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;
                }
            }
        }

五、“退出”按鍵按下后

得益于多線程的使用,在表盤一直處于死循環(huán)監(jiān)測(cè)時(shí)間變化的同時(shí),依然能檢測(cè)到Button的按下。
“退出”按下后,退出頁(yè)面。

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

六、表盤上各點(diǎn)的計(jì)算

C#如何實(shí)現(xiàn)時(shí)鐘表盤

“C#如何實(shí)現(xiàn)時(shí)鐘表盤”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI