winform 显示时钟效果

摘要:
1、winform里拖拽控件timer2、为timer控件绑定事件3、程序代码如下1usingSystem;2usingSystem.Drawing;3usingSystem.Windows.Forms;4usingSystem.Drawing.Drawing2D;56namespaceVH_CriticalReport.userControl7{8publicpartialclassClockC

1、winform里拖拽控件 timer

winform 显示时钟效果第1张

2、为timer控件 绑定事件

winform 显示时钟效果第2张

3、程序代码如下

winform 显示时钟效果第3张winform 显示时钟效果第4张
1 usingSystem;
2 usingSystem.Drawing;
3 usingSystem.Windows.Forms;
4 usingSystem.Drawing.Drawing2D;
5 
6 namespaceVH_CriticalReport.userControl
7 {
8     public partial classClockControl : UserControl
9 {
10         const int screenWidth = 150; //屏幕宽度
11         const int screenHeight = 150; //屏幕高度
12 
13         publicClockControl()
14 {
15 InitializeComponent();
16 
17             this.Width = screenWidth + 1;
18             this.Height = screenHeight + 1;
19             this.DoubleBuffered = true; //控件缓冲,避免闪烁
20             this.SetStyle(ControlStyles.ResizeRedraw, true);
21 clockTimer.Start();
22 
23 }
24 
25         private void clockTimer_Tick(objectsender, EventArgs e)
26 {
27 Invalidate();
28 }
29 
30         protected override voidOnPaint(PaintEventArgs e)
31 {
32             DateTime dtNow =DateTime.Now;
33             string dayOfWeek = dtNow.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));//星期几
34             Brush brush = new SolidBrush(Color.Black); //填充图形
35             Pen pen = new Pen(Color.Black); //画笔
36             Font hourFont = new Font("Arial", 10, FontStyle.Bold);//时钟数字的字体
37             Font dateFont = new Font("Arial", 6); //日期的字体
38             int dialRadius = Math.Min(screenWidth, screenHeight) / 2; //圆的半径
39 
40             Graphics g =e.Graphics;
41             g.SmoothingMode =SmoothingMode.HighQuality;
42 
43             //默认坐标系统原点是左上角,现在把原点移到屏幕中心, 右下左上对应的轴:x,y,-x,-y
44 g.TranslateTransform(dialRadius, dialRadius);
45 
46             //画时钟最外层的圆线(pen,x,y,width,height)
47             //圆的中心点坐标计算:(width/2+x,height/2+y),据此可得出要使圆在坐标原点(0,0)的x,y坐标值           
48             g.DrawEllipse(pen, -screenWidth / 2, -dialRadius, screenWidth, screenHeight);
49 
50             GraphicsState state =g.Save();
51             //画矩形、日期、星期几       
52             int rectWidth = 70;
53             int rectHeight = 30;
54             g.DrawRectangle(pen, -rectWidth / 2, rectHeight, rectWidth, rectHeight);
55             g.DrawString(dtNow.ToString("yyyy-MM-dd"), dateFont, brush, -rectWidth / 2, rectHeight + 2);
56             g.DrawString(dayOfWeek.PadLeft(8, ' '), dateFont, brush, -rectWidth / 2, rectHeight + 15);
57 g.Restore(state);
58 
59             //画时钟的60个圆点
60             //Save(),Restore(state)配合使用,使得平移、缩放、旋转等操作只对它们作用域之间的代码有效,
61             //save开始到restore之间这绘画,就像有绘制了一个图层,restore之后将两个图层放到一起
62             state =g.Save();
63             for (int i = 0; i < 60; i++)
64 {
65                 int w = i % 5 == 0 ? 5 : 3;
66                 g.FillEllipse(brush, 0, -dialRadius, w, w);
67                 //围绕指定点按照顺时针方向旋转角度360 / 60 = 6度
68                 g.RotateTransform(6);
69 }
70 g.Restore(state);
71 
72             //画时钟的12个数字,如果用上面RotateTransform方法则数字会倾斜、倒立,故不用
73             state =g.Save();
74             for (int i = 0; i < 12; i++)
75 {
76                 //已知圆中心占坐标(x0,y0),半径r,角度a0,则圆上任一点坐标(x,y)计算:
77                 //x = x0 + r * cos(ao * 3.14 /180) 
78                 //y = y0 + r * sin(ao * 3.14 /180) 
79                 Point point = new Point(-6, -6); //当为(0,0)时全部数字偏右下移,故手动调整
80                 double dd = Math.PI / 180 * i * (360 / 12); //每次转360/12度
81                 float x = point.X + (float)((dialRadius - 12) *Math.Cos(dd));
82                 float y = point.Y + (float)((dialRadius - 12) *Math.Sin(dd));
83 
84                 //因为是从顺时钟3点钟开始画,所以索引i需要加上3
85                 int j = i + 3;
86                 if (j > 12)
87                     j = j - 12;
88 g.DrawString(j.ToString(), hourFont, brush, x, y);
89 }
90 g.Restore(state);
91 
92             //画时钟的图形
93             state =g.Save();
94             g.RotateTransform((dtNow.Hour - 12 + dtNow.Minute / 60f) * 360f /12f);
95             //时钟指针默认指向12点钟方向,分钟指针也一样
96             g.DrawPolygon(new Pen(brush), newPoint[]
97 {
98                 new Point(0,  20), new Point( 10, 0),
99                 new Point(0, -60), new Point(-10, 0)
100                 
101 });
102 g.Restore(state);
103 
104             //画分钟的图形
105             state =g.Save();
106             g.RotateTransform((dtNow.Minute + dtNow.Second / 60f) * 360f /60f);
107             g.DrawPolygon(new Pen(brush), newPoint[]
108 {
109                 new Point(0,  20), new Point( 6, 0),
110                 new Point(0, -80), new Point(-6, 0)
111                
112 });
113 g.Restore(state);
114 
115             //画秒钟的图形
116             state =g.Save();
117             g.RotateTransform(dtNow.Second * 360f /60f);
118             g.FillRectangle(brush, -1, -dialRadius + 10, 2, dialRadius);
119 g.Restore(state);
120 }
121 
122 
123 
124 }
125 }
View Code

4、在调用地方,加载时钟;

winform 显示时钟效果第5张

5,、显示时钟效果

winform 显示时钟效果第6张

免责声明:文章转载自《winform 显示时钟效果》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MFC浅析(7) CWnd类虚函数的调用时机、缺省实现 .SpringBootSecurity学习(23)前后端分离版之OAuth2.0 其它模式下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C# WinForm 文件上传下载

/// <summary> /// WebClient上传文件至服务器 /// </summary> /// <param name="fileNamePath">文件名,全路径格式</param> /// <param name="uriString">服务器文件夹路径</...

WinForm控件之【BindingNavigator】【DataSet】【BindingSource】【DataGridView】

基本介绍 数据类控件,数据加载绑定便捷应用相当广泛,具体看例子自行扩展吧; 常设置属性 BindingNavigator--BindingSource:数据来源,绑定后默认项会根据相应的操作按钮执行操作; BindingNavigator--Items:显示项的集合; DataSet--Tables:数据集内数据表的集合; BindingSource--D...

C# winform DataGridView 常见属性

C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行、列的隐藏和删除 ⑦ 禁止列或者行的Resize ⑧ 列宽和行高以及列头的高度和行头的宽度的自动调整 ⑨ 冻结列或行 ⑩ 列顺序的调整 ⑪ 行头列头的单元格⑫ 剪切板的操作...

Winform ListView 窗体闪烁问题解决

在 winform 编程时, ListView 添加数据时 控件闪烁 , 参考如下解决方法,得到改善。 首先,自定义一个类ListViewNF,继承自System.Windows.Forms.ListView 代码如下: public classListViewNF : System.Windows.Forms.ListView {...

C# Winform程序设计运行在高分屏下模糊解决办法

前段时间在开发一个坐标转换程序,开发环境是Windows10 64位专业版(V1803)和Visual Stuido 2015社区版,电脑屏幕是15.6英寸分辨率1920*1080,采用的是WinForm程序设计方法开发。在程序运行时遇到了这样一个问题,在Visual Studio设计的很清晰的菜单和界面,运行的时候菜单和控件上字体变得很模糊,界面大小也发...

C#winform中ListView的使用

使用ListView模仿Windows系统的资源管理器界面,实现文件(夹)的浏览、重命名、删除及查询等功能,主要功能界面展示如下: 1.MainForm.cs及MainForm.Designer.cs 1 using System; 2 using System.Collections.Generic; 3 using System....