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#索引器简单实例代码

    C#索引器简单实例代码

    打开.Net Framework源代码随便看几个类,就会发现索引器的影子。索引器可以被重载,可以接收一个或者多个参数,但是不可以定义为静态的。可以用关联数组的方式访问索引器。
    2013-03-03
  • C#实现将数组内元素打乱顺序的方法

    C#实现将数组内元素打乱顺序的方法

    这篇文章主要介绍了C#实现将数组内元素打乱顺序的方法,涉及C#数组遍历及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • 浅析C# 索引器(Indexer)

    浅析C# 索引器(Indexer)

    这篇文章主要介绍了C# 索引器(Indexer)的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • C#实现在底图上动态生成文字和图片

    C#实现在底图上动态生成文字和图片

    这篇文章主要为大家详细介绍了C#实现在底图上动态生成文字和图片,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • FTPClientHelper辅助类 实现文件上传,目录操作,下载等操作

    FTPClientHelper辅助类 实现文件上传,目录操作,下载等操作

    这篇文章主要分享了一个FTPClientHelper辅助类和介绍了常用的FTP命令,需要的朋友可以参考下。
    2016-06-06
  • C# 向二进制文件进行读写的操作方法

    C# 向二进制文件进行读写的操作方法

    该例子使用 BinaryStream 和 BinaryWriter 对二进制文件进行读写操作先上代码再根据我理解的所分享给各位朋友
    2013-04-04
  • Unity实现10天签到系统

    Unity实现10天签到系统

    这篇文章主要为大家详细介绍了Unity实现10天签到系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • C#编程实现带有Aero效果的窗体示例

    C#编程实现带有Aero效果的窗体示例

    这篇文章主要介绍了C#编程实现带有Aero效果的窗体,涉及C#调用动态链接库针对窗体属性的相关操作技巧,需要的朋友可以参考下
    2017-07-07
  • C# 串口接收数据中serialPort.close()死锁的实例

    C# 串口接收数据中serialPort.close()死锁的实例

    下面小编就为大家分享一篇C# 串口接收数据中serialPort.close()死锁的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-11-11
  • C# 8.0新特性介绍

    C# 8.0新特性介绍

    C# 语言是在2000发布的,至今已正式发布了7个版本,每个版本都包含了许多令人兴奋的新特性和功能更新。下面通过本文给大家分享下C# 8.0的三个令人兴奋的新特性,需要的朋友参考下吧
    2017-10-10

最新评论