C# GDI+实现时钟表盘
作者:瀛海的鲲 时间:2023-06-20 07:11:32
本文实例为大家分享了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();
}
六、表盘上各点的计算
来源:https://blog.csdn.net/X888885304/article/details/123490787
标签:C#,时钟,表盘
0
投稿
猜你喜欢
c# wpf使用GMap.NET类库,实现地图轨迹回放
2023-04-20 06:46:27
C语言程序设计50例(经典收藏)
2023-07-10 08:33:19
Java 十大排序算法之归并排序刨析
2022-03-15 17:29:20
Kotlin 编程三分钟入门
2021-06-27 13:22:35
Java协议字节操作工具类详情
2023-11-02 06:25:02
Java8新特性之泛型的目标类型推断_动力节点Java学院整理
2023-11-26 10:38:21
Android打包篇:Android Studio将代码打包成jar包教程
2022-08-14 06:22:20
Java 超详细讲解类的定义方式和对象的实例化
2021-09-11 17:38:29
java数据结构和算法中哈希表知识点详解
2023-08-05 08:28:11
理解Java当中的回调机制(翻译)
2023-03-15 04:21:00
tk.mybatis实现uuid主键生成的示例代码
2022-01-22 00:17:13
Android编程实现给Button添加图片和文字的方法
2021-11-27 21:30:20
Java源码解析之Iterable接口
2023-04-17 08:06:57
android实现简单底部导航栏
2022-07-10 16:11:08
Android View移动的六种方法小结
2023-07-06 02:43:39
c++中vector<int>和vector<int*>的用法及区别
2023-03-28 02:29:37
svn 清理失败 (cleanup 失败) 的快速解决方法
2022-10-25 11:22:40
一文带你了解SpringBoot的启动原理
2023-11-28 20:44:42
Java 三种进制的数值常量操作
2021-11-14 21:39:41
java生成饼图svg及JFreeChart生成svg图表
2023-10-25 19:27:00