WPF完成時鐘殊效。本站提示廣大學習愛好者:(WPF完成時鐘殊效)文章只能為提供參考,不一定能成為您想要的結果。以下是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技巧完成時鐘的後果,小編的程度無限,假如有毛病的處所請年夜家原諒,年夜家配合提高。