TClockView.xaml
<Label Name="lab商标" Foreground="White" Margin="0,0,0,211" HorizontalAlignment="Center" VerticalAlignment="Bottom" Height="Auto" Width="Auto" FontSize="13" >JackMoon</Label> <Label Name="lab创建时间" Foreground="White" Margin="0,91,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Height="Auto" Width="Auto">1987</Label> <!-- 秒针定义 --> <Rectangle Margin="0.2,0,0,9" Name="rectangleSecond" Stroke="White" Height="16" VerticalAlignment="Bottom" Width="1"> <Rectangle.RenderTransform> <RotateTransform x:Name="secondPointer" CenterX="0" CenterY="0" Angle="0" /> </Rectangle.RenderTransform> </Rectangle> <!-- --> <!-- 分钟定义 --> <Rectangle Margin="0,0,0,0" Name="rectangleMinute" Height="12" Stroke="LightGreen" Width="1"> <Rectangle.RenderTransform> <RotateTransform x:Name="minutePointer" CenterX="0" CenterY="9" Angle="45" /> </Rectangle.RenderTransform> </Rectangle> <!-- --> <!-- 时针定义 --> <Rectangle Margin="1,0,0,4" Name="rectangleHour" Height="8" Stroke="red" Width="1"> <Rectangle.RenderTransform> <RotateTransform x:Name="hourPointer" CenterX="1" CenterY="7" Angle="90" /> </Rectangle.RenderTransform> </Rectangle> <!----> <Label Content="08:08:08" FontSize="16" Foreground="White" Height="Auto" HorizontalAlignment="Center" Margin="114,0,113,86" Name="labTime" VerticalAlignment="Bottom" Width="Auto" /> </Grid> </UserControl>注意:时分秒的位置调整,还是需要根据自己打开后效果为准 (这个时钟的背景图片刻度有问题,大家自己找一个正确的哈~)
TClockView.xaml.cs using System; using System.Windows; using System.Windows.Controls;
namespace CableMonitor.UserControls { using System.Windows.Threading;
/// <summary> /// TClockView.xaml 的交互逻辑 /// </summary> public partial class TClockView : UserControl { //计时器 System.Timers.Timer timer = new System.Timers.Timer(1000); public TClockView() { 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; timer.Start(); } //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"); })); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { } }}主界面
<StackPanel Grid.ColumnSpan="1" Grid.Column="1" Margin="35,7,0,7" Orientation="Vertical" HorizontalAlignment="Left"> <localCon:TClockView Width="50" Height="50"/> </StackPanel>