最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF實現(xiàn)時鐘特效

 更新時間:2015年11月25日 09:58:40   作者:JackWang-CUMT  
這篇文章主要介紹了WPF實現(xiàn)時鐘特效,過程很簡單,感興趣的小伙伴們可以參考一下

WPF在樣式定義和UI動畫上面相對于以前的技術(shù)有了不少的提升,下面給出WPF技術(shù)實現(xiàn)鐘表的效果:

1、Visual Studio新建一個WPF應(yīng)用程序,命名為WpfClock,新建一個images文件夾,并準(zhǔn)備一個鐘表的背景圖片和程序圖標(biāo)素材。

2、編輯MainWindow.xaml文件,對UI進(jìn)行定制,代碼如下(指針都是用Rectangle實現(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
 {
  //計時器
  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)
  {
   //進(jìn)行拖放移動
   this.DragMove();
  }
  private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
   //UI異步更新
   this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
   {
    //秒針轉(zhuǎn)動,秒針繞一圈360度,共60秒,所以1秒轉(zhuǎn)動6度
    secondPointer.Angle = DateTime.Now.Second * 6;
    //分針轉(zhuǎn)動,分針繞一圈360度,共60分,所以1分轉(zhuǎn)動6度
    minutePointer.Angle = DateTime.Now.Minute * 6;
    //時針轉(zhuǎn)動,時針繞一圈360度,共12時,所以1時轉(zhuǎn)動30度。
    //另外同一個小時內(nèi),隨著分鐘數(shù)的變化(繞一圈60分鐘),時針也在緩慢變化(轉(zhuǎn)動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文件,對后臺邏輯進(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
 {
  //計時器
  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)
  {
   //進(jìn)行拖放移動
   this.DragMove();
  }
  private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
   //UI異步更新
   this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
   {
    //秒針轉(zhuǎn)動,秒針繞一圈360度,共60秒,所以1秒轉(zhuǎn)動6度
    secondPointer.Angle = DateTime.Now.Second * 6;
    //分針轉(zhuǎn)動,分針繞一圈360度,共60分,所以1分轉(zhuǎn)動6度
    minutePointer.Angle = DateTime.Now.Minute * 6;
    //時針轉(zhuǎn)動,時針繞一圈360度,共12時,所以1時轉(zhuǎn)動30度。
    //另外同一個小時內(nèi),隨著分鐘數(shù)的變化(繞一圈60分鐘),時針也在緩慢變化(轉(zhuǎn)動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、編譯運行,如果運氣不錯的話,應(yīng)該能顯示如下效果:

總結(jié)

WPF可以用RotateTransform中的Angle進(jìn)行旋轉(zhuǎn),可以指定中心點(CenterX,CenterY)

 <Rectangle.RenderTransform>
  <RotateTransform x:Name="hourPointer" CenterX="0" CenterY="70" Angle="90" />
 </Rectangle.RenderTransform>

以上就是WPF技術(shù)實現(xiàn)時鐘的效果,小編的水平有限,如果有錯誤的地方請大家諒解,大家共同進(jìn)步。

相關(guān)文章

  • C# WPF上位機(jī)實現(xiàn)和下位機(jī)TCP通訊的方法

    C# WPF上位機(jī)實現(xiàn)和下位機(jī)TCP通訊的方法

    這篇文章主要介紹了C# WPF上位機(jī)實現(xiàn)和下位機(jī)TCP通訊的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • C#實現(xiàn)批量下載圖片到本地示例代碼

    C#實現(xiàn)批量下載圖片到本地示例代碼

    這篇文章主要給大家介紹了關(guān)于C#如何實現(xiàn)批量下載圖片到本地的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • C#鉤子Hook監(jiān)聽鍵盤鼠標(biāo)事件實現(xiàn)窗體自動關(guān)閉

    C#鉤子Hook監(jiān)聽鍵盤鼠標(biāo)事件實現(xiàn)窗體自動關(guān)閉

    鉤子(Hook)的作用主要體現(xiàn)在監(jiān)視和攔截系統(tǒng)或進(jìn)程中的各種事件消息,并進(jìn)行自定義處理,本文主要介紹了C#如何利用鉤子Hook監(jiān)聽鍵盤鼠標(biāo)事件實現(xiàn)窗體自動關(guān)閉功能,感興趣的可以了解下
    2025-01-01
  • c# Linq查詢詳解

    c# Linq查詢詳解

    這篇文章主要介紹了c# Linq查詢的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • 淺談C#各種數(shù)組直接的數(shù)據(jù)復(fù)制/轉(zhuǎn)換

    淺談C#各種數(shù)組直接的數(shù)據(jù)復(fù)制/轉(zhuǎn)換

    下面小編就為大家?guī)硪黄獪\談C#各種數(shù)組直接的數(shù)據(jù)復(fù)制/轉(zhuǎn)換。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-08-08
  • C#實現(xiàn)子窗體與父窗體通信方法實例總結(jié)

    C#實現(xiàn)子窗體與父窗體通信方法實例總結(jié)

    這篇文章主要介紹了C#實現(xiàn)子窗體與父窗體通信方法,實例總結(jié)了常用的四種窗體通信方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • c#委托把方法當(dāng)成參數(shù)(實例講解)

    c#委托把方法當(dāng)成參數(shù)(實例講解)

    本篇文章主要是對c#委托把方法當(dāng)成參數(shù)的實例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C# 設(shè)計模式系列教程-橋接模式

    C# 設(shè)計模式系列教程-橋接模式

    橋接模式降低了沿著兩個或多個維度擴(kuò)展時的復(fù)雜度,防止類的過度膨脹,解除了兩個或多個維度之間的耦合,使它們沿著各自方向變化而不互相影響。
    2016-06-06
  • c# 解決IIS寫Excel的權(quán)限問題

    c# 解決IIS寫Excel的權(quán)限問題

    使用以上方法必須對dcom進(jìn)行配置,給用戶使用office的權(quán)限
    2012-10-10
  • C#8 的模式匹配實現(xiàn)

    C#8 的模式匹配實現(xiàn)

    這篇文章主要介紹了C#8 的模式匹配實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12

最新評論

温泉县| 内丘县| 固始县| 万山特区| 台前县| 舟曲县| 大姚县| 怀仁县| 瓮安县| 台前县| 孝义市| 塔城市| 南城县| 岫岩| 岫岩| 彰武县| 岐山县| 广德县| 子长县| 巧家县| 青浦区| 韶山市| 芒康县| 余姚市| 郸城县| 石城县| 壤塘县| 南郑县| 徐闻县| 毕节市| 马尔康县| 阳信县| 三穗县| 治县。| 张家界市| 德惠市| 平和县| 徐水县| 蕲春县| 奈曼旗| 田林县|