<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了C#實現圖形介面的時鐘的具體程式碼,供大家參考,具體內容如下
秒針有跳躍兩個格子問題,主要是演演算法耗時沒考慮在TimeTicker的觸發事件內,導致程式執行有延遲。
時間執行正確(獲取的系統時間)。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Cool_Graphics { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Point center; private void Form1_Load(object sender, EventArgs e) { lastFormSize = this.Size; initPicBox(); timer1.Interval = welcomeTime / welcomeSplits; timer1.Enabled = true; } private void initPicBox() { //清空面板,找中心點 center = new Point(pictureBox1.Width / 2, pictureBox1.Height / 2); pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height); Graphics g = Graphics.FromImage(pictureBox1.Image); g.FillRectangle(Brushes.White, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); //按視窗大小,自動設定錶盤適宜半徑 r = pictureBox1.Width < pictureBox1.Height ? (pictureBox1.Width / 2 - margin) : (pictureBox1.Height / 2 - margin); //預設相對r比例設定 其他值 circle_width = GetRelativeValue(r, 100); pFlag1_length = GetRelativeValue(r, 25); pFlag1_width = GetRelativeValue(r, 80); pFlag2_length = GetRelativeValue(r, 50); pFlag2_width = GetRelativeValue(r, 250); second_length = GetRelativeValue(r, 1.15); second_tailLen = GetRelativeValue(r, 5); second_width = GetRelativeValue(r, 250); minute_length = GetRelativeValue(r, 250.0 / 190.0);//分針長度 minute_width = GetRelativeValue(r, 250.0 / 2.0);//分針寬度 hour_length = GetRelativeValue(r, 250.0 / 160.0);//時針長度 hour_width = GetRelativeValue(r, 250.0 / 3.0);//時針寬度 center_r = GetRelativeValue(r, 250.0 / 4.0);//錶盤中心點半徑 } int shanxinNum = 0; private void timer1_Tick(object sender, EventArgs e) { Graphics g = Graphics.FromImage(pictureBox1.Image); g.FillPie(new SolidBrush(Color.FromArgb(shanxinNum + 90, shanxinNum * 2 + 10, shanxinNum * 3 + 50)), center.X - r, center.Y - r, r * 2, r * 2, shanxinNum * 360 / welcomeSplits, 360 / welcomeSplits); if (shanxinNum++ > welcomeSplits / 2) { timer2.Interval = 1000; timer2.Enabled = true; } if (shanxinNum > welcomeSplits - 1) { shanxinNum = 0; timer1.Enabled = false; } pictureBox1.Refresh(); } int welcomeTime = 1000;//歡迎介面時間 ms int welcomeSplits = 60;//歡迎介面的切割個數 int margin = 10;//錶盤外邊距 int r = 250;//錶盤半徑 Color bg_color = Color.White;//背景色 Color circle_bg_color = Color.LightBlue;//圓盤背景色 float circle_width = 2;//外表盤圓寬度 Color circle_color = Color.Black;//外表盤圓顏色 float pFlag1_length = 10;//圓盤外部格標誌1長度 float pFlag1_width = 3;//圓盤外部格標誌1寬度 Color pFlag1_color = Color.Black;//圓盤外部格標誌1顏色 float pFlag2_length = 5;//圓盤外部格標誌2長度 float pFlag2_width = 1; //圓盤外部格標誌2寬度 Color pFlag2_color = Color.Black;//圓盤外部格標誌2顏色 float pSLine_length = 20;//下吊墜線長度 float pSLine_width = 1;//下吊墜線長度 Color pSLine_color = Color.Red;//下吊墜線長度 float second_length = 200;//秒針長度 float second_tailLen = 50;//秒針尾巴長度 float second_width = 1;//秒針寬度 Color second_color = Color.Red;//秒針顏色 float minute_length = 190;//分針長度 float minute_width = 2;//分針寬度 Color minute_color = Color.DarkGreen;//分針顏色 float hour_length = 160;//時針長度 float hour_width = 3;//時針寬度 Color hour_color = Color.DarkBlue;//時針顏色 float center_r = 4;//錶盤中心點半徑 Color center_color = Color.Black;//圓心點顏色 private void timer2_Tick(object sender, EventArgs e) { //Console.WriteLine(DateTime.Now.Millisecond); // timer2.Enabled = false; Graphics g = Graphics.FromImage(pictureBox1.Image); //面板清空 g.FillRectangle(new SolidBrush(bg_color), new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); //畫圓盤背景 g.FillEllipse(new SolidBrush(circle_bg_color), center.X - r, center.Y - r, r * 2, r * 2); //畫表盤外框圓 g.DrawEllipse(new Pen(circle_color, circle_width), center.X - r, center.Y - r, r * 2, r * 2); //話錶盤格 double span = Math.PI / 30;//每個格子間隔弧度值 for (float i = 0; i < 60; i++) { PointF p1 = new PointF(center.X + r * (float)Math.Sin(i * span), center.Y - r * (float)Math.Cos(i * span)); PointF p2; PointF ps2; if (i % 5 == 0) { p2 = new PointF(center.X + (r - pFlag1_length) * (float)Math.Sin(i * span), center.Y - (r - pFlag1_length) * (float)Math.Cos(i * span)); g.DrawLine(new Pen(pFlag1_color, pFlag1_width), p1, p2); /* ps2 = new PointF(p1.X,p1.Y+pSLine_length); g.DrawLine(new Pen(pSLine_color, pSLine_width), p1, ps2);*/ } else { p2 = new PointF(center.X + (r - pFlag2_length) * (float)Math.Sin(i * span), center.Y - (r - pFlag2_length) * (float)Math.Cos(i * span)); g.DrawLine(new Pen(pFlag2_color, pFlag2_width), p1, p2); } } //獲取當前時間 DateTime time = DateTime.Now; float millisecond = (float)time.Millisecond; float second = time.Second + millisecond / 1000; float minute = time.Minute + second / 60; float hour = time.Hour + minute / 60; PointF tempPF; //畫時針 tempPF = new PointF(center.X + hour_length * (float)Math.Sin(hour * 5 * span), center.Y - hour_length * (float)Math.Cos(hour * 5 * span)); g.DrawLine(new Pen(hour_color, hour_width), center, tempPF); //畫分針 tempPF = new PointF(center.X + minute_length * (float)Math.Sin(minute * span), center.Y - minute_length * (float)Math.Cos(minute * span)); g.DrawLine(new Pen(minute_color, minute_width), center, tempPF); //畫秒針 if (timer2.Interval == 1000) { second = time.Second; } tempPF = new PointF(center.X + second_length * (float)Math.Sin(second * span), center.Y - second_length * (float)Math.Cos(second * span)); PointF tailP = new PointF(center.X - second_tailLen * (float)Math.Sin(second * span), center.Y + second_tailLen * (float)Math.Cos(second * span)); g.DrawLine(new Pen(second_color, second_width), tailP, tempPF); 畫秒針附加效果 //for (int i = 1; i < 256; i++) //{ // tempPF = new PointF(center.X + second_length * (float)Math.Sin((second - i * 0.02) * span), center.Y - second_length * (float)Math.Cos((second - i * 0.02) * span)); // tailP = new PointF(center.X - second_tailLen * (float)Math.Sin((second - i * 0.02) * span), center.Y + second_tailLen * (float)Math.Cos((second - i * 0.02) * span)); // g.DrawLine(new Pen(Color.FromArgb(255,i,i), second_width), tailP, tempPF); //} 畫毫秒針 //tempPF = new PointF(center.X + second_length * (float)Math.Sin(millisecond * span * 60 / 1000), center.Y - second_length * (float)Math.Cos(millisecond * span * 60 / 1000)); //tailP = new PointF(center.X - second_tailLen * (float)Math.Sin(millisecond * span * 60 / 1000), center.Y + second_tailLen * (float)Math.Cos(millisecond * span * 60 / 1000)); //g.DrawLine(new Pen(second_color, second_width), tailP, tempPF); //畫中心點 g.FillEllipse(new SolidBrush(center_color), center.X - center_r, center.Y - center_r, center_r * 2, center_r * 2); pictureBox1.Refresh(); //timer2.Enabled = true; } private float GetRelativeValue(float src, double ratio) { return src / (float)ratio > 1 ? src / (float)ratio : 1; } Size lastFormSize; private void Form1_ResizeEnd(object sender, EventArgs e) { if (this.Size == lastFormSize) return; timer2.Enabled = false; shanxinNum = 0; initPicBox(); timer1.Interval = 17; timer1.Enabled = true; } FormWindowState lastState = FormWindowState.Normal; private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState != lastState) { lastState = this.WindowState; timer2.Enabled = false; shanxinNum = 0; initPicBox(); timer1.Interval = 17; timer1.Enabled = true; } } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45