windows代码:
<Window x:Class="_04_钟表续.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:_04_钟表续"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="525" Loaded="Window_Loaded">
<Canvas Name="BG" >
<Border Name="br" Width="200" Height="200" BorderThickness="3" BorderBrush="Black" CornerRadius="100" Canvas.Left="150" Canvas.Top="69"/>
<Image Width="8" Height="25" Source="img/shuzi/btn1.png" Stretch="Fill" Canvas.Left="241" Canvas.Top="70"/>
<Image Width="8" Height="25" Source="img/shuzi/btn2.png" Stretch="Fill" Canvas.Left="251" Canvas.Top="70"/>
<Image Width="14" Height="25" Source="img/shuzi/btn1.png" Stretch="Fill" Canvas.Left="290" Canvas.Top="85"/>
<Image Width="14" Height="25" Source="img/shuzi/btn2.png" Stretch="Fill" Canvas.Left="320" Canvas.Top="115"/>
<Image Width="14" Height="25" Source="img/shuzi/btn3.png" Stretch="Fill" Canvas.Left="336" Canvas.Top="158"/>
<Image Width="14" Height="25" Source="img/shuzi/btn4.png" Stretch="Fill" Canvas.Left="320" Canvas.Top="203"/>
<Image Width="14" Height="25" Source="img/shuzi/btn5.png" Stretch="Fill" Canvas.Left="290" Canvas.Top="230"/>
<Image Width="14" Height="25" Source="img/shuzi/btn6.png" Stretch="Fill" Canvas.Left="244" Canvas.Top="244"/>
<Image Width="14" Height="25" Source="img/shuzi/btn7.png" Stretch="Fill" Canvas.Left="201" Canvas.Top="230"/>
<Image Width="14" Height="25" Source="img/shuzi/btn8.png" Stretch="Fill" Canvas.Left="166" Canvas.Top="203"/>
<Image Width="14" Height="25" Source="img/shuzi/btn9.png" Stretch="Fill" Canvas.Left="150" Canvas.Top="158"/>
<Image Width="8" Height="25" Source="img/shuzi/btn1.png" Stretch="Fill" Canvas.Left="166" Canvas.Top="115"/>
<Image Width="8" Height="25" Source="img/shuzi/btn0.png" Stretch="Fill" Canvas.Left="172" Canvas.Top="115"/>
<Image Width="8" Height="24" Source="img/shuzi/btn1.png" Stretch="Fill" Canvas.Left="199" Canvas.Top="86"/>
<Image Width="8" Height="24" Source="img/shuzi/btn1.png" Stretch="Fill" Canvas.Left="207" Canvas.Top="86"/>
</Canvas>
</Window>
wpf代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using System.Windows.Media.Animation;
namespace _04_钟表续
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
#region 获取当前时间
DateTime time = DateTime.Now;
int shi = time.Hour;
int fen = time.Minute;
int miao = time.Second;
#endregion
#region 时针
Image S = new Image();
S.Width = 15;
S.Height = 50;
S.Source = new BitmapImage(new Uri("img/zheng/S.png", UriKind.Relative));
S.Stretch = Stretch.Fill;
Canvas.SetLeft(S, Canvas.GetLeft(br) + br.Width / 2 - S.Width / 2);
Canvas.SetTop(S, Canvas.GetTop(br) + br.Height / 2 + 14 - S.Height);
BG.Children.Add(S);
//设置旋转方向
RotateTransform rotate = new RotateTransform();
//给对象设置旋转中心
S.RenderTransform = rotate;
//旋转中心设置百分比
S.RenderTransformOrigin = new Point(0.5, 0.85);
Storyboard story = new Storyboard();
DoubleAnimation daS = new DoubleAnimation(shi*30+fen*0.5, (shi * 30 + fen * 0.5)+360, new Duration(TimeSpan.FromHours(12)));
Storyboard.SetTarget(daS, S);
Storyboard.SetTargetProperty(daS, new PropertyPath("RenderTransform.Angle"));
daS.RepeatBehavior = RepeatBehavior.Forever;
story.Children.Add(daS);
story.Begin();
#endregion
#region 分针
Image F = new Image();
F.Width = 15;
F.Height = 65;
F.Source = new BitmapImage(new Uri("img/zheng/F.png", UriKind.Relative));
F.Stretch = Stretch.Fill;
Canvas.SetLeft(F, Canvas.GetLeft(br) + br.Width / 2 - F.Width / 2);
Canvas.SetTop(F, Canvas.GetTop(br) + br.Height / 2+14 - F.Height);
BG.Children.Add(F);
RotateTransform rotate1 = new RotateTransform();
F.RenderTransform = rotate1;
F.RenderTransformOrigin = new Point(0.5, 0.9);
Storyboard story1 = new Storyboard();
DoubleAnimation daF = new DoubleAnimation(fen*6, fen*6+360, new Duration(TimeSpan.FromMinutes(60)));
Storyboard.SetTarget(daF, F);
Storyboard.SetTargetProperty(daF, new PropertyPath("RenderTransform.Angle"));
daF.RepeatBehavior = RepeatBehavior.Forever;
story1.Children.Add(daF);
story1.Begin();
#endregion
#region 秒针
Image M = new Image();
M.Width = 15;
M.Height = 70;
M.Source = new BitmapImage(new Uri("img/zheng/M.png", UriKind.Relative));
M.Stretch = Stretch.Fill;
Canvas.SetLeft(M, Canvas.GetLeft(br) + br.Width / 2 - M.Width / 2);
Canvas.SetTop(M, Canvas.GetTop(br) + br.Height / 2 + 25 - M.Height);
BG.Children.Add(M);
RotateTransform rotate2 = new RotateTransform();
M.RenderTransform = rotate2;
M.RenderTransformOrigin = new Point(0.5, 0.75);
Storyboard story2 = new Storyboard();
DoubleAnimation daM = new DoubleAnimation(miao*6, miao * 6+360, new Duration(TimeSpan.FromSeconds(60)));
Storyboard.SetTarget(daM, M);
Storyboard.SetTargetProperty(daM, new PropertyPath("RenderTransform.Angle"));
daM.RepeatBehavior = RepeatBehavior.Forever;
story2.Children.Add(daM);
story2.Begin();
Border br02 = new Border();
br02.Background = Brushes.Black;
br02.Width = br02.Height = 8;
br02.CornerRadius = new CornerRadius(4);
BG.Children.Add(br02);
Canvas.SetLeft(br02,Canvas.GetLeft(br)+br.Width/2-3);
Canvas.SetTop(br02, Canvas.GetTop(br) + br.Height / 2+3);
#endregion
}
}
}