易采站长站为您分析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)
- { //进行拖放移动










