C#实现图形界面的时钟
作者:「已注销」 发布时间:2022-10-03 15:10:50
标签:C#,时钟
本文实例为大家分享了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;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
来源:https://blog.csdn.net/y85171642/article/details/9729791


猜你喜欢
- /// <summary> /// 汉字转拼音缩写 /// </summary> /// <param nam
- 本文实例讲述了java获取百度网盘真实下载链接的方法。分享给大家供大家参考。具体如下:目前还存在一个问题,同一ip在获取3次以后会出现验证码
- 1. 问题描述springboot的面世,成为Java开发者的一大福音,大大提升了开发的效率,其实springboot只是在maven的基础
- 服务器端我们用软件模拟,是一个很小巧的软件,下载软件NetAssist:http://xiazai.jb51.net/201403/tool
- 前言在Activity间传递的数据一般比较简单,但是有时候实际开发中也会传一些比较复杂的数据,本节一起来学习更多Activity间数据的传递
- 如果你想知道java annotation是什么?你可以先看看:“http://www.infoq.com/articles/Annotat
- 本文实例为大家分享了Java实现图书借阅系统的具体代码,供大家参考,具体内容如下为图书阅览室开发一个图书借阅系统,最多可存50本图书,实现图
- 这篇文章主要介绍了java private关键字用法实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 定义 1、如果注解中有属性,那么必须给属性赋值。package com.lxc.Test;// 定义一个注解public @int
- 一、构造函数构造函数的最大作用就是创建对象时完成初始化,当我们在new一个对象并传入参数的时候,会自动调用构造函数并完成参数的初始化。如下:
- 本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下:package com
- Android onClick 与 setOnClickListener区别为Android Widgets添加点击事件处理函数又
- idea手动刷新git分支相信很多小伙伴都遇到过这样的问题,在git上新建的分支却在idea的git分支中找不到又不知在哪里刷新,博主最近也
- 现在android的每一个项目都会需要设置为全屏,现在介绍两种设置为全屏的方式。一、在配置文件中设置android:theme=”@andr
- 本文主要讲解安装AndroidStudio和配置环境变量遇到一些问题,以及解决方法。需要的软件:AndriodStudio安装包.java
- Spring注解AspectJ操作AOP一、被增强类新建一个被增强的类 User,下面有个 add() 方法。package com.pin
- 现在很多app的首页都有一个倒计时控件,比如说3秒或者5秒自动跳转界面,或者点击控件直接跳过首先,自定义控件CircleProgressba
- 1.sonarQube的简介SonarQube是一款自动化代码审查工具,用于检测代码中的错误、漏洞和代码异味。它可以与你现有的工作流集成,以
- 延迟队列的使用场景还比较多,例如:1、超时未收到支付回调,主动查询支付状态;2、规定时间内,订单未支付,自动取消;。。。总之,但凡需要在未来
- 在Android开发中,往往要用到自定义的控件来实现我们的需求或效果。在使用自定义 控件时,难免要用到自定义属性,那怎么使用自定义属性呢?在