wpf在样式定义和ui动画上面相对于以前的技术有了不少的提升,下面给出wpf技术实现钟表的效果:
1、visual studio新建一个wpf应用程序,命名为wpfclock,新建一个images文件夹,并准备一个钟表的背景图片和程序图标素材。
2、编辑mainwindow.xaml文件,对ui进行定制,代码如下(指针都是用rectangle实现的,当然可以用图片代替):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
using system; using system.collections.generic; using system.linq; using system.text; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; namespace wpfclock { using system.threading; using system.windows.threading; /// <summary> /// mainwindow.xaml 的交互逻辑 /// </summary> public partial class mainwindow : window { //计时器 system.timers.timer timer = new system.timers.timer(1000); public mainwindow() { initializecomponent(); #region 初始化时间 secondpointer.angle = datetime.now.second * 6; minutepointer.angle = datetime.now.minute * 6; hourpointer.angle = (datetime.now.hour * 30) + (datetime.now.minute * 0.5); this .labtime.content = datetime.now.tostring( "hh:mm:ss" ); #endregion timer.elapsed += new system.timers.elapsedeventhandler(timer_elapsed); timer.enabled = true ; } private void grid_mouseleftbuttondown( object sender, mousebuttoneventargs e) { //进行拖放移动 this .dragmove(); } private void timer_elapsed( object sender, system.timers.elapsedeventargs e) { //ui异步更新 this .dispatcher.invoke(dispatcherpriority.normal, (action)(() => { //秒针转动,秒针绕一圈360度,共60秒,所以1秒转动6度 secondpointer.angle = datetime.now.second * 6; //分针转动,分针绕一圈360度,共60分,所以1分转动6度 minutepointer.angle = datetime.now.minute * 6; //时针转动,时针绕一圈360度,共12时,所以1时转动30度。 //另外同一个小时内,随着分钟数的变化(绕一圈60分钟),时针也在缓慢变化(转动30度,30/60=0.5) hourpointer.angle = (datetime.now.hour * 30)+ (datetime.now.minute * 0.5); //更新时间值 this .labtime.content = datetime.now.tostring( "hh:mm:ss" ); })); } } } |
3、编辑mainwindow.xaml.cs文件,对后台逻辑进行定制,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
using system; using system.collections.generic; using system.linq; using system.text; using system.windows; using system.windows.controls; using system.windows.data; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.imaging; using system.windows.navigation; using system.windows.shapes; namespace wpfclock { using system.threading; using system.windows.threading; /// <summary> /// mainwindow.xaml 的交互逻辑 /// </summary> public partial class mainwindow : window { //计时器 system.timers.timer timer = new system.timers.timer(1000); public mainwindow() { initializecomponent(); #region 初始化时间 secondpointer.angle = datetime.now.second * 6; minutepointer.angle = datetime.now.minute * 6; hourpointer.angle = (datetime.now.hour * 30) + (datetime.now.minute * 0.5); this .labtime.content = datetime.now.tostring( "hh:mm:ss" ); #endregion timer.elapsed += new system.timers.elapsedeventhandler(timer_elapsed); timer.enabled = true ; } private void grid_mouseleftbuttondown( object sender, mousebuttoneventargs e) { //进行拖放移动 this .dragmove(); } private void timer_elapsed( object sender, system.timers.elapsedeventargs e) { //ui异步更新 this .dispatcher.invoke(dispatcherpriority.normal, (action)(() => { //秒针转动,秒针绕一圈360度,共60秒,所以1秒转动6度 secondpointer.angle = datetime.now.second * 6; //分针转动,分针绕一圈360度,共60分,所以1分转动6度 minutepointer.angle = datetime.now.minute * 6; //时针转动,时针绕一圈360度,共12时,所以1时转动30度。 //另外同一个小时内,随着分钟数的变化(绕一圈60分钟),时针也在缓慢变化(转动30度,30/60=0.5) hourpointer.angle = (datetime.now.hour * 30)+ (datetime.now.minute * 0.5); //更新时间值 this .labtime.content = datetime.now.tostring( "hh:mm:ss" ); })); } } } |
4、编译运行,如果运气不错的话,应该能显示如下效果:
总结
wpf可以用rotatetransform中的angle进行旋转,可以指定中心点(centerx,centery)
1
2
3
|
<rectangle.rendertransform> <rotatetransform x:name= "hourpointer" centerx= "0" centery= "70" angle= "90" /> </rectangle.rendertransform> |
以上就是wpf技术实现时钟的效果,小编的水平有限,如果有错误的地方请大家谅解,大家共同进步。