WPF实现时钟特效

 更新时间:2015年11月25日 09:58:40   作者:JackWang-CUMT  
这篇文章主要介绍了WPF实现时钟特效,过程很简单,感兴趣的小伙伴们可以参考一下

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# WPF上位机实现和下位机TCP通讯的方法

    C# WPF上位机实现和下位机TCP通讯的方法

    这篇文章主要介绍了C# WPF上位机实现和下位机TCP通讯的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • C#实现批量下载图片到本地示例代码

    C#实现批量下载图片到本地示例代码

    这篇文章主要给大家介绍了关于C#如何实现批量下载图片到本地的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • C#钩子Hook监听键盘鼠标事件实现窗体自动关闭

    C#钩子Hook监听键盘鼠标事件实现窗体自动关闭

    钩子(Hook)的作用主要体现在监视和拦截系统或进程中的各种事件消息,并进行自定义处理,本文主要介绍了C#如何利用钩子Hook监听键盘鼠标事件实现窗体自动关闭功能,感兴趣的可以了解下
    2025-01-01
  • c# Linq查询详解

    c# Linq查询详解

    这篇文章主要介绍了c# Linq查询的相关资料,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-04-04
  • 浅谈C#各种数组直接的数据复制/转换

    浅谈C#各种数组直接的数据复制/转换

    下面小编就为大家带来一篇浅谈C#各种数组直接的数据复制/转换。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • C#实现子窗体与父窗体通信方法实例总结

    C#实现子窗体与父窗体通信方法实例总结

    这篇文章主要介绍了C#实现子窗体与父窗体通信方法,实例总结了常用的四种窗体通信方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-09-09
  • c#委托把方法当成参数(实例讲解)

    c#委托把方法当成参数(实例讲解)

    本篇文章主要是对c#委托把方法当成参数的实例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01
  • C# 设计模式系列教程-桥接模式

    C# 设计模式系列教程-桥接模式

    桥接模式降低了沿着两个或多个维度扩展时的复杂度,防止类的过度膨胀,解除了两个或多个维度之间的耦合,使它们沿着各自方向变化而不互相影响。
    2016-06-06
  • c# 解决IIS写Excel的权限问题

    c# 解决IIS写Excel的权限问题

    使用以上方法必须对dcom进行配置,给用户使用office的权限
    2012-10-10
  • C#8 的模式匹配实现

    C#8 的模式匹配实现

    这篇文章主要介绍了C#8 的模式匹配实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12

最新评论