WPF实现时钟特效
程序员文章站
2022-07-06 12:43:12
wpf在样式定义和ui动画上面相对于以前的技术有了不少的提升,下面给出wpf技术实现钟表的效果:
1、visual studio新建一个wpf应用程序,命名为wpfclo...
wpf在样式定义和ui动画上面相对于以前的技术有了不少的提升,下面给出wpf技术实现钟表的效果:
1、visual studio新建一个wpf应用程序,命名为wpfclock,新建一个images文件夹,并准备一个钟表的背景图片和程序图标素材。
2、编辑mainwindow.xaml文件,对ui进行定制,代码如下(指针都是用rectangle实现的,当然可以用图片代替):
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文件,对后台逻辑进行定制,代码如下:
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)
<rectangle.rendertransform> <rotatetransform x:name="hourpointer" centerx="0" centery="70" angle="90" /> </rectangle.rendertransform>
以上就是wpf技术实现时钟的效果,小编的水平有限,如果有错误的地方请大家谅解,大家共同进步。
上一篇: 轻松学习C#的属性