WPF实现时钟特效

2019-12-30 11:13:00丽君
  • 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. using System;  using System.Collections.Generic; 
    2. using System.Linq;  using System.Text; 
    3. using System.Windows;  using System.Windows.Controls; 
    4. using System.Windows.Data;  using System.Windows.Documents; 
    5. using System.Windows.Input;  using System.Windows.Media; 
    6. using System.Windows.Media.Imaging;  using System.Windows.Navigation; 
    7. using System.Windows.Shapes;  namespace WpfClock