WPF實(shí)現(xiàn)時(shí)鐘特效
WPF在樣式定義和UI動(dòng)畫上面相對(duì)于以前的技術(shù)有了不少的提升,下面給出WPF技術(shù)實(shí)現(xiàn)鐘表的效果:
1、Visual Studio新建一個(gè)WPF應(yīng)用程序,命名為WpfClock,新建一個(gè)images文件夾,并準(zhǔn)備一個(gè)鐘表的背景圖片和程序圖標(biāo)素材。
2、編輯MainWindow.xaml文件,對(duì)UI進(jìn)行定制,代碼如下(指針都是用Rectangle實(shí)現(xiàn)的,當(dāng)然可以用圖片代替):
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 { //計(jì)時(shí)器 System.Timers.Timer timer = new System.Timers.Timer(1000); public MainWindow() { InitializeComponent(); #region 初始化時(shí)間 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) { //進(jìn)行拖放移動(dòng) this.DragMove(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //UI異步更新 this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { //秒針轉(zhuǎn)動(dòng),秒針繞一圈360度,共60秒,所以1秒轉(zhuǎn)動(dòng)6度 secondPointer.Angle = DateTime.Now.Second * 6; //分針轉(zhuǎn)動(dòng),分針繞一圈360度,共60分,所以1分轉(zhuǎn)動(dòng)6度 minutePointer.Angle = DateTime.Now.Minute * 6; //時(shí)針轉(zhuǎn)動(dòng),時(shí)針繞一圈360度,共12時(shí),所以1時(shí)轉(zhuǎn)動(dòng)30度。 //另外同一個(gè)小時(shí)內(nèi),隨著分鐘數(shù)的變化(繞一圈60分鐘),時(shí)針也在緩慢變化(轉(zhuǎn)動(dòng)30度,30/60=0.5) hourPointer.Angle = (DateTime.Now.Hour * 30)+ (DateTime.Now.Minute * 0.5); //更新時(shí)間值 this.labTime.Content = DateTime.Now.ToString("HH:mm:ss"); })); } } }
3、編輯MainWindow.xaml.CS文件,對(duì)后臺(tái)邏輯進(jìn)行定制,代碼如下:
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 { //計(jì)時(shí)器 System.Timers.Timer timer = new System.Timers.Timer(1000); public MainWindow() { InitializeComponent(); #region 初始化時(shí)間 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) { //進(jìn)行拖放移動(dòng) this.DragMove(); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //UI異步更新 this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() => { //秒針轉(zhuǎn)動(dòng),秒針繞一圈360度,共60秒,所以1秒轉(zhuǎn)動(dòng)6度 secondPointer.Angle = DateTime.Now.Second * 6; //分針轉(zhuǎn)動(dòng),分針繞一圈360度,共60分,所以1分轉(zhuǎn)動(dòng)6度 minutePointer.Angle = DateTime.Now.Minute * 6; //時(shí)針轉(zhuǎn)動(dòng),時(shí)針繞一圈360度,共12時(shí),所以1時(shí)轉(zhuǎn)動(dòng)30度。 //另外同一個(gè)小時(shí)內(nèi),隨著分鐘數(shù)的變化(繞一圈60分鐘),時(shí)針也在緩慢變化(轉(zhuǎn)動(dòng)30度,30/60=0.5) hourPointer.Angle = (DateTime.Now.Hour * 30)+ (DateTime.Now.Minute * 0.5); //更新時(shí)間值 this.labTime.Content = DateTime.Now.ToString("HH:mm:ss"); })); } } }
4、編譯運(yùn)行,如果運(yùn)氣不錯(cuò)的話,應(yīng)該能顯示如下效果:
總結(jié)
WPF可以用RotateTransform中的Angle進(jìn)行旋轉(zhuǎn),可以指定中心點(diǎn)(CenterX,CenterY)
<Rectangle.RenderTransform> <RotateTransform x:Name="hourPointer" CenterX="0" CenterY="70" Angle="90" /> </Rectangle.RenderTransform>
以上就是WPF技術(shù)實(shí)現(xiàn)時(shí)鐘的效果,小編的水平有限,如果有錯(cuò)誤的地方請(qǐng)大家諒解,大家共同進(jìn)步。
- WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效
- WPF實(shí)現(xiàn)文本描邊+外發(fā)光效果的示例代碼
- WPF實(shí)現(xiàn)動(dòng)畫效果
- WPF實(shí)現(xiàn)3D粒子波浪效果
- WPF實(shí)現(xiàn)平面三角形3D運(yùn)動(dòng)效果
- WPF實(shí)現(xiàn)文字粒子閃爍動(dòng)畫效果
- WPF實(shí)現(xiàn)3D立方體波浪墻效果
- WPF實(shí)現(xiàn)進(jìn)度條實(shí)時(shí)更新效果
- WPF實(shí)現(xiàn)轉(zhuǎn)圈進(jìn)度條效果
- WPF實(shí)現(xiàn)流光動(dòng)畫特效
相關(guān)文章
C#實(shí)現(xiàn)將數(shù)組內(nèi)元素打亂順序的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將數(shù)組內(nèi)元素打亂順序的方法,涉及C#數(shù)組遍歷及隨機(jī)數(shù)操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)在底圖上動(dòng)態(tài)生成文字和圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05FTPClientHelper輔助類 實(shí)現(xiàn)文件上傳,目錄操作,下載等操作
這篇文章主要分享了一個(gè)FTPClientHelper輔助類和介紹了常用的FTP命令,需要的朋友可以參考下。2016-06-06Unity實(shí)現(xiàn)10天簽到系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)10天簽到系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04C#編程實(shí)現(xiàn)帶有Aero效果的窗體示例
這篇文章主要介紹了C#編程實(shí)現(xiàn)帶有Aero效果的窗體,涉及C#調(diào)用動(dòng)態(tài)鏈接庫(kù)針對(duì)窗體屬性的相關(guān)操作技巧,需要的朋友可以參考下2017-07-07C# 串口接收數(shù)據(jù)中serialPort.close()死鎖的實(shí)例
下面小編就為大家分享一篇C# 串口接收數(shù)據(jù)中serialPort.close()死鎖的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-11-11