網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了C#實(shí)現(xiàn)圖形界面的時(shí)鐘的具體代碼,供大家參考,具體內(nèi)容如下
秒針有跳躍兩個(gè)格子問題,主要是算法耗時(shí)沒考慮在TimeTicker的觸發(fā)事件內(nèi),導(dǎo)致程序運(yùn)行有延遲。
時(shí)間運(yùn)行正確(獲取的系統(tǒng)時(shí)間)。
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()
? ? ? ? {
? ? ? ? ? ? //清空面板,找中心點(diǎn)
? ? ? ? ? ? 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));
?
? ? ? ? ? ? //按窗口大小,自動(dòng)設(shè)置表盤適宜半徑
? ? ? ? ? ? r = pictureBox1.Width < pictureBox1.Height ? (pictureBox1.Width / 2 - margin) : (pictureBox1.Height / 2 - margin);
? ? ? ? ? ? //默認(rèn)相對r比例設(shè)置 其他值
? ? ? ? ? ? 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);//時(shí)針長度
? ? ? ? ? ? hour_width = GetRelativeValue(r, 250.0 / 3.0);//時(shí)針寬度
? ? ? ? ? ? center_r = GetRelativeValue(r, 250.0 / 4.0);//表盤中心點(diǎn)半徑
? ? ? ? }
?
? ? ? ? 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;//歡迎界面時(shí)間 ms
? ? ? ? int welcomeSplits = 60;//歡迎界面的切割個(gè)數(shù)
? ? ? ? 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;//圓盤外部格標(biāo)志1長度
? ? ? ? float pFlag1_width = 3;//圓盤外部格標(biāo)志1寬度
? ? ? ? Color pFlag1_color = Color.Black;//圓盤外部格標(biāo)志1顏色
? ? ? ? float pFlag2_length = 5;//圓盤外部格標(biāo)志2長度
? ? ? ? float pFlag2_width = 1; //圓盤外部格標(biāo)志2寬度
? ? ? ? Color pFlag2_color = Color.Black;//圓盤外部格標(biāo)志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;//時(shí)針長度
? ? ? ? float hour_width = 3;//時(shí)針寬度
? ? ? ? Color hour_color = Color.DarkBlue;//時(shí)針顏色
? ? ? ? float center_r = 4;//表盤中心點(diǎn)半徑
? ? ? ? Color center_color = Color.Black;//圓心點(diǎn)顏色
?
? ? ? ? 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;//每個(gè)格子間隔弧度值
? ? ? ? ? ? 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);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
?
? ? ? ? ? ? //獲取當(dāng)前時(shí)間
? ? ? ? ? ? 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;
?
? ? ? ? ? ? //畫時(shí)針
? ? ? ? ? ? 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);
?
? ? ? ? ? ? //畫中心點(diǎn)
? ? ? ? ? ? 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;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
原文鏈接:https://blog.csdn.net/y85171642/article/details/9729791
相關(guān)推薦
- 2022-09-25 Linux安裝Nginx詳細(xì)教程
- 2022-10-10 python中sort()函數(shù)用法詳解_python
- 2022-11-21 Python?Flask框架開發(fā)之運(yùn)用SocketIO實(shí)現(xiàn)WebSSH方法詳解_python
- 2022-12-26 Python中pywifi模塊的基本用法講解_python
- 2022-07-26 arduino上傳程序出錯(cuò)不成功常見的問題解決
- 2022-03-08 SQL實(shí)現(xiàn)分頁查詢方法總結(jié)_數(shù)據(jù)庫其它
- 2022-12-22 關(guān)于Python?ImportError:?No?module?named?通用解決方法_pytho
- 2022-08-13 深入理解Linux內(nèi)核select多路復(fù)用原理
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支