C#实现钟表程序设计
作者:步北宸 时间:2023-01-01 06:38:42
本文实例为大家分享了C#实现钟表程序设计的具体代码,供大家参考,具体内容如下
工作空间:
代码如下:
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;
using System.Diagnostics;
namespace 钟表
{
public partial class frmMain : Form
{
bool flsMouseDown = false;
Point fDownPosition;
public frmMain()
{
InitializeComponent();
}
private void ctsmiExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void frmMain_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
flsMouseDown = true;
fDownPosition = e.Location;
//Debug.WriteLine(e.Location);
}
}
private void frmMain_MouseMove(object sender, MouseEventArgs e)
{
if (flsMouseDown)
{
this.Left = this.Location.X + (e.X - fDownPosition.X);
this.Top = this.Location.Y + (e.Y - fDownPosition.Y);
}
}
private void frmMain_Mouseup(object sender, MouseEventArgs e)
{
flsMouseDown = false;
}
private void tmrMain_Tick(object sender, EventArgs e)
{
this.Refresh();//调用窗体的Paint事件,根据调制的时间来刷新paint时间
}
private void fDrawDot(Graphics aGraphics, double aValue, int aRadius)//画出表盘刻度
{
double lAngle = ((360 - aValue * 6) + 90) * Math.PI / 180;//根据角度绘画
Point lPoint = new Point((int)(200 + 180 * Math.Cos(lAngle)),//刻度所在的为值
(int)(200 - 180 * Math.Sin(lAngle)));
aGraphics.FillEllipse(Brushes.Black, new Rectangle(lPoint.X - aRadius,//FillEllipse为内部填充颜色方法,Rectangle为绘画圆,前俩为圆心坐标,后俩为宽和高
lPoint.Y - aRadius, 2 * aRadius, 2 * aRadius));
}
private void fDrawLine(Graphics aGraphics, double aValue, double aLength, Pen aPen)//画出时,分,秒的线
{//aValue为角度值,小时12个刻度每个占30°,分和秒60个刻度每个站6°avalue按照的是一个圆分成60个换成角度乘于6
//aLength是指针的长度
double lAngle = ((360 - aValue * 6) + 90) * Math.PI / 180;//角度转化为数值
Point lPoint = new Point((int)(200 + aLength * Math.Cos(lAngle)),//指针线的终点,坐标x,y通过直角三角求得
(int)(200 - aLength * Math.Sin(lAngle)));
aGraphics.DrawLine(aPen, new Point(200, 200), lPoint);//绘制俩点连成一条直线,起点相同都为(200,200)
}
private void frmMain_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//使指针转动显得平滑
e.Graphics.DrawImage(this.BackgroundImage, 0, 0, 400, 400);//规定表盘的大小和位置
for (int i = 0; i < 60; i++)
if (i % 5 == 0) fDrawDot(e.Graphics, i, 6);//绘制钟表刻度,大点和小点
else fDrawDot(e.Graphics, i, 2);
DateTime dt = DateTime.Now;//获取时间
Pen pSecond = new Pen(Color.Black, 2);
Pen pMinute = new Pen(Color.Red, 2);
Pen pHour = new Pen(Color.Blue, 4);
fDrawLine(e.Graphics, ((dt.Hour % 12) + dt.Minute / 60.0) * 5, 100, pHour);//绘画时,分,秒指针
fDrawLine(e.Graphics, dt.Minute+dt.Second/60.0,160, pMinute);
fDrawLine(e.Graphics, dt.Second+dt.Millisecond/1000.0, 170, pSecond);
}
Form1.Designer.cs
需添加:(以下代码)
this.DoubleBuffered = true;
this.Deactivate += new System.EventHandler(this.ctsmiExit_Click);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.frmMain_Paint);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseMove);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.frmMain_Mouseup);
来源:https://blog.csdn.net/qq_38534627/article/details/80626329
标签:C#,钟表
0
投稿
猜你喜欢
SpringBoot整合Shiro的方法详解
2022-04-13 15:05:56
Android 操作系统获取Root权限 原理详细解析
2021-12-07 16:20:55
详解Java的文件与目录管理以及输入输出相关操作
2022-05-03 15:07:22
java中public class与class的区别详解
2023-04-09 11:49:40
C# 显示、隐藏窗口对应的任务栏
2023-06-13 03:57:32
SpringDataJpa如何使用union多表分页条件查询
2023-05-16 04:28:49
Java中如何使用Response重定向
2023-08-03 07:41:07
Java实现线程同步方法及原理详解
2021-07-29 21:28:13
Linux命令行下编译Android NDK的示例代码
2023-02-13 17:19:54
Android实现打开各种文件的intent方法小结
2022-03-12 18:39:20
Android实现TCP断点上传 后台C#服务接收
2023-08-25 22:24:00
深入解析Android系统中应用程序前后台切换的实现要点
2022-09-11 01:26:30
再议C#中的装箱与拆箱的问题详解
2022-09-30 03:45:40
Android获取SD卡路径及SDCard内存的方法
2021-12-30 17:04:58
intellij idea使用git stash暂存一次提交的操作
2023-03-08 08:30:11
Java ArrayList类的基础使用讲解
2021-11-14 10:22:18
Android用过TextView实现跑马灯效果的示例
2023-07-27 11:16:54
Redis之GEO存储地理位置信息的使用
2023-12-22 14:29:49
java模拟实现双向链表
2022-06-05 09:16:16
java:程序包org.springframework.boot不存在的完美解决方法
2023-09-03 20:37:32