栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

WPF实现时钟特效

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
 /// 
 /// MainWindow.xaml 的交互逻辑
 /// 
 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;
 /// 
 /// MainWindow.xaml 的交互逻辑
 /// 
 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)

 
  
 

以上就是WPF技术实现时钟的效果,小编的水平有限,如果有错误的地方请大家谅解,大家共同进步。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/124569.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号