1界面设计部分
<Window x:Class="钟表.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:钟表"
mc:Ignorable="d"
Title="钟表" Height="800" Width="800" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen">
<Canvas Width="800" Height="800" Name="BG">
<Image Source="img/dp.png" Name="img" Width="750" Height="750" Canvas.Left="22" Canvas.Top="25" />
<Image Name="xs" Width="22" Height="170" Source="img/sz.png" Canvas.Left="396" Canvas.Top="250"></Image>
<Image Name="fz" Width="22" Height="190" Source="img/fz.png" Canvas.Left="396" Canvas.Top="226"></Image>
<Image Name="mz" Width="22" Height="240" Source="img/mz.png" Canvas.Left="396" Canvas.Top="213"></Image>
</Canvas>
</Window>
2.xmal.cs代码
DateTime date = DateTime.Now;
int hour = date.Hour;
int minute = date.Minute;
int second = date.Second;
//秒针
Storyboard S = new Storyboard();
//添加一个旋转引用
RotateTransform rtf3 = new RotateTransform();
mz.RenderTransform = rtf3;
mz.RenderTransformOrigin = new Point(0.5, 0.74);
DoubleAnimation da = new DoubleAnimation(hour * 6, 360 + hour * 6, new Duration(TimeSpan.FromSeconds(60)));
da.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTarget(da, mz);
Storyboard.SetTargetProperty(da, new PropertyPath("RenderTransform.Angle"));
S.Children.Add(da);
S.Begin();
//分钟
Storyboard M = new Storyboard();
//添加一个旋转引用
RotateTransform rtf2 = new RotateTransform();
fz.RenderTransform = rtf2;
fz.RenderTransformOrigin = new Point(0.5, 0.86);
DoubleAnimation da2 = new DoubleAnimation(6 * minute+ hour / 10, 360 + 6 * minute + hour / 10, new Duration(TimeSpan.FromMinutes(60)));
da2.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTarget(da2, fz);
Storyboard.SetTargetProperty(da2, new PropertyPath("RenderTransform.Angle"));
M.Children.Add(da2);
M.Begin();
//时针
Storyboard H = new Storyboard();
//添加一个旋转引用
RotateTransform rtf1 = new RotateTransform();
xs.RenderTransform = rtf1;
xs.RenderTransformOrigin = new Point(0.5, 0.85);
DoubleAnimation da3 = new DoubleAnimation(30 * hour + 0.5*minute, 360 + 30 * hour + minute / 2, new Duration(TimeSpan.FromHours(12)));
da3.RepeatBehavior = RepeatBehavior.Forever;
Storyboard.SetTarget(da3, xs);
Storyboard.SetTargetProperty(da3, new PropertyPath("RenderTransform.Angle"));
H.Children.Add(da3);
H.Begin();