C#动画实现钟表

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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值