WPF鍾表效果實現


WPF在樣式定義和UI動畫上面相對於以前的技術有了不少的提升,下面給出WPF技術實現鍾表的效果:

1、Visual Studio新建一個WPF應用程序,命名為WpfClock,新建一個images文件夾,並准備一個鍾表的背景圖片和程序圖標素材。

2、編輯MainWindow.xaml文件,對UI進行定制,代碼如下(指針都是用Rectangle實現的,當然可以用圖片代替):

 1 <Window x:Class="WpfClock.MainWindow"
 2  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4  Title="MainWindow" Margin="2" Height="327" Width="311" AllowsTransparency="True" 
 5  WindowStyle="None" Background="Transparent" WindowStartupLocation="CenterScreen" 
 6  Icon="images/clock.ico" 
 7  ResizeMode="NoResize" Topmost="False" Opacity="1">
 8         <Grid Width="300" Height="300" MouseLeftButtonDown="Grid_MouseLeftButtonDown">
 9         <Image Source="images/backGround.png"></Image>
10 
11         <Label Name="lab商標" Foreground="White" Margin="0,0,0,211" HorizontalAlignment="Center" VerticalAlignment="Bottom" Height="Auto" Width="Auto" FontSize="13" >JackMoon</Label>
12         <Label Name="lab創建時間" Foreground="White" Margin="0,91,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Height="Auto" Width="Auto">1987</Label>
13 
14         <!-- 秒針定義 -->
15         <Rectangle Margin="150,0,149,150" Name="rectangleSecond" Stroke="White" Height="120" VerticalAlignment="Bottom" Width="1">
16             <Rectangle.RenderTransform>
17                 <RotateTransform x:Name="secondPointer" CenterX="0" CenterY="120" Angle="0" />
18             </Rectangle.RenderTransform>
19         </Rectangle>
20         <!-- -->
21 
22         <!-- 分鍾定義 -->
23         <Rectangle Margin="150,49,149,151" Name="rectangleMinute" Stroke="LightGreen" Width="1">
24             <Rectangle.RenderTransform>
25                 <RotateTransform x:Name="minutePointer" CenterX="0" CenterY="100" Angle="45" />
26             </Rectangle.RenderTransform>
27         </Rectangle>
28         <!-- -->
29 
30         <!-- 時針定義 -->
31         <Rectangle Margin="150,80,149,150" Name="rectangleHour" Stroke="LightYellow" Width="1">
32             <Rectangle.RenderTransform>
33                 <RotateTransform x:Name="hourPointer" CenterX="0" CenterY="70" Angle="90" />
34             </Rectangle.RenderTransform>
35         </Rectangle>
36         <!---->
37          <Label Content="08:08:08" FontSize="16" Foreground="White" Height="Auto" HorizontalAlignment="Center" Margin="114,0,113,86" Name="labTime" VerticalAlignment="Bottom" Width="Auto" />
38 
39     </Grid>
40 </Window>

3、編輯MainWindow.xaml.CS文件,對后台邏輯進行定制,代碼如下:

 1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Windows;  6 using System.Windows.Controls;  7 using System.Windows.Data;  8 using System.Windows.Documents;  9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 namespace WpfClock 15 { 16     using System.Threading; 17     using System.Windows.Threading; 18     /// <summary>
19     /// MainWindow.xaml 的交互邏輯 20     /// </summary>
21     public partial class MainWindow : Window 22  { 23         //計時器
24         System.Timers.Timer timer = new System.Timers.Timer(1000); 25         public MainWindow() 26  { 27  InitializeComponent(); 28             #region 初始化時間
29             secondPointer.Angle = DateTime.Now.Second * 6; 30             minutePointer.Angle = DateTime.Now.Minute * 6; 31             hourPointer.Angle = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5); 32             this.labTime.Content = DateTime.Now.ToString("HH:mm:ss"); 33             #endregion
34             timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 35             timer.Enabled = true; 36  } 37 
38         private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 39  { 40             //進行拖放移動
41             this.DragMove(); 42  } 43        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 44  { 45            //UI異步更新
46             this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
47  { 48                 //秒針轉動,秒針繞一圈360度,共60秒,所以1秒轉動6度
49                 secondPointer.Angle = DateTime.Now.Second * 6; 50                 //分針轉動,分針繞一圈360度,共60分,所以1分轉動6度
51                 minutePointer.Angle = DateTime.Now.Minute * 6; 52                 //時針轉動,時針繞一圈360度,共12時,所以1時轉動30度。 53                 //另外同一個小時內,隨着分鍾數的變化(繞一圈60分鍾),時針也在緩慢變化(轉動30度,30/60=0.5)
54                 hourPointer.Angle = (DateTime.Now.Hour * 30)+ (DateTime.Now.Minute * 0.5); 55                 //更新時間值
56                 this.labTime.Content = DateTime.Now.ToString("HH:mm:ss"); 57  })); 58  } 59 
60  } 61 }

4、編譯運行,如果運氣不錯的話,應該能顯示如下效果:

5、總結

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

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM