您好,登錄后才能下訂單哦!
本篇內容介紹了“C#如何實現時鐘表盤”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
按鍵“打開時鐘”按下后會出現表盤,按鍵“退出”按下后會關閉頁面。
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(); }
“C#如何實現時鐘表盤”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。