C# WPF动画,时钟程序源码分享,闹钟程序源码分享

C# WPF动画,时钟程序源码分享,闹钟程序源码分享_第1张图片

钟表界面右击设置闹钟

C# WPF动画,时钟程序源码分享,闹钟程序源码分享_第2张图片
C# WPF动画,时钟程序源码分享,闹钟程序源码分享_第3张图片

源码奉上请大佬点评。

注释齐全:不做流氓。

钟表界面xaml代码
<Window x:Class="WPF动画.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:WPF动画"
        mc:Ignorable="d" Background="Bisque"
        Title="时钟" Height="500" Width="600" Loaded="Window_Loaded" Icon="/img/bitbug_favicon.ico">
    <Canvas x:Name="BG" Width="400" Height="400">
        <Border  x:Name="Miao"  Width="1" Height="199" Background="Red" Canvas.Left="200"  Canvas.Top="0">
        </Border>
        <Border  x:Name="Fen"  Width="3" Height="150" Background="Orange" Canvas.Left="200"  Canvas.Top="50">
        </Border>
        <Border  x:Name="Shi"  Width="5" Height="100" Background="Yellow"  Canvas.Left="200"  Canvas.Top="100">
        </Border>
        <Border BorderBrush="Black" CornerRadius="10" BorderThickness="1"  Background="Black" Height="20" Canvas.Left="190" Canvas.Top="190" Width="20"/>
        <Label Name="sj" Content="00:00:00"  FontSize="18" Canvas.Left="160" Canvas.Top="280" RenderTransformOrigin="-0.123,-0.579" Height="30" Width="85"/>
        <Canvas.ContextMenu >
            <ContextMenu>
                <MenuItem Name="设置闹钟"  Header="设置闹钟" Click="设置闹钟_Click"  />
                <Separator/>
                <MenuItem Name="NewTimer" IsEnabled="False" Header=""/>
            </ContextMenu>
        </Canvas.ContextMenu>
    </Canvas>
</Window>

钟表C#代码
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;
using System.Windows.Threading;

namespace WPF动画
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            sj.Content = DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
        }
        DispatcherTimer tim = new DispatcherTimer();
        // 动画
        Storyboard donghua = new Storyboard();

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            tim.Interval = new TimeSpan(0,0,1);
            tim.Tick += Tim_Tick;
            tim.Start();
            // 实际的时间
            double miao = DateTime.Now.Second * 6;   
            double fen = DateTime.Now.Minute * 6;    
            double shi = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5);
            #region 时钟图片背景
            Image bg = new Image();
            bg.Source = new BitmapImage(new Uri("../../img/BG.png", UriKind.RelativeOrAbsolute));
            ImageBrush brush = new ImageBrush();
            brush.ImageSource = bg.Source;
            BG.Background = brush;
            #endregion
            // 秒,分,时
            Shijian(miao,Miao,1);
            Shijian(fen,Fen,60);
            Shijian(shi,Shi,720);
        }

        private void Tim_Tick(object sender, EventArgs e)
        {
            NewTimer.Header = DateTime.Now.ToString();
            sj.Content = DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
        }

        /// 
        /// 时间方法
        /// 
        /// 现在的时间
        /// 时,分,秒任意指针
        /// 执行的时间
        private void Shijian(double miao,Border shijian,int x)
        {
            DoubleAnimation miaoanim = new DoubleAnimation(miao, 360 + miao, new Duration(TimeSpan.FromMinutes(x)));    
            Storyboard.SetTarget(miaoanim, shijian);
            Storyboard.SetTargetProperty(miaoanim, new PropertyPath("RenderTransform.Angle"));
            miaoanim.RepeatBehavior = RepeatBehavior.Forever;
            donghua.Children.Add(miaoanim);
            RotateTransform rotate = new RotateTransform();
            shijian.RenderTransform = rotate;
            shijian.RenderTransformOrigin = new Point(1, 1);
            donghua.Begin();
        }

        private void 设置闹钟_Click(object sender, RoutedEventArgs e)
        {
            闹钟 newwin = new 闹钟();
            newwin.Show();
        }
    }
}

闹钟界面xaml代码
<Window x:Class="WPF动画.闹钟"
        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:WPF动画"
        mc:Ignorable="d"
        Title="闹钟" Height="251" Width="578.333" Loaded="Window_Loaded" Background="SkyBlue" Icon="/img/bitbug_favicon.ico">
    <Canvas>
        <ComboBox Name="shi" Canvas.Left="148" Canvas.Top="51" Width="120">
            <ComboBoxItem Content="0" HorizontalAlignment="Left" Width="117.333333333333"/>

            <ComboBoxItem Content="23" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="22" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="21" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="20" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="19" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="18" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="17" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="16" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="15" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="14" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="13" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="12" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="11" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="10" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="9" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="8" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="7" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="6" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="5" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="4" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="3" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="2" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="1" HorizontalAlignment="Left" Width="117.333333333333"/>


        </ComboBox>
        <ComboBox x:Name="shi_Copy" Canvas.Left="303" Canvas.Top="51" Width="120">

            <ComboBoxItem Content="0" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="59" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="58" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="57" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="56" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="55" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="54" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="53" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="52" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="51" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="50" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="49" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="48" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="47" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="46" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="45" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="44" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="43" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="42" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="41" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="40" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="39" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="38" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="37" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="36" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="35" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="34" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="33" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="32" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="31" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="30" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="29" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="28" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="27" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="26" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="25" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="24" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="23" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="22" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="21" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="21" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="20" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="19" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="18" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="17" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="16" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="15" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="14" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="13" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="12" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="11" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="10" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="9" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="8" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="7" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="6" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="5" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="4" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="3" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="2" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="1" HorizontalAlignment="Left" Width="117.333333333333"/>


        </ComboBox>
        <Label Content="小时:" Canvas.Left="148" Canvas.Top="20" RenderTransformOrigin="-3.443,-0.789"/>
        <Label Content="分钟:" Canvas.Left="303" Canvas.Top="20" RenderTransformOrigin="-3.443,-0.789"/>
        <Button x:Name="queding" Background="BlanchedAlmond" Content="确定时间" Canvas.Left="446" Canvas.Top="51" Width="94" Click="Button_Click" Height="69"/>
        <ListBox Background="Transparent" x:Name="naozhong" Height="160" Canvas.Left="40" Canvas.Top="51" Width="95"/>
        <Label Content="历史闹钟时间:" Canvas.Left="40" Canvas.Top="20" RenderTransformOrigin="-3.443,-0.789"/>
        <Label Name="shengyu" Content="剩余时间:"  Canvas.Left="155" Canvas.Top="150"/>
        <ComboBox Name="音乐" Canvas.Left="303" Canvas.Top="99" Width="120">
            <ComboBoxItem Content="好好听的音乐" HorizontalAlignment="Left" Width="117.333333333333"/>
            <ComboBoxItem Content="超级好听的音乐" HorizontalAlignment="Left" Width="117.333333333333"/>
        </ComboBox>
        <Label Content="选择闹钟音乐:" Canvas.Left="213" Canvas.Top="95" RenderTransformOrigin="-3.443,-0.789"/>

    </Canvas>
</Window>

闹钟cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
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.Shapes;
using System.Windows.Threading;

namespace WPF动画
{
    /// 
    /// 闹钟.xaml 的交互逻辑
    /// 
    public partial class 闹钟 : Window
    {
        public 闹钟()
        {
            InitializeComponent();
        }
        // 计时器
        DispatcherTimer timer = new DispatcherTimer();
        // 播放音乐
        SoundPlayer mic = new SoundPlayer();
        // 倒计时的时间
        TimeSpan tes;
        // 判断是否选定时间
        bool iskaishi = false;
        // 音乐路径
        string mics;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 设置计时器
            timer.Tick += Timer_Tick;
            timer.Interval = new TimeSpan(0,0,1);
            timer.Start();
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            // 获取当前时间
            DateTime date = DateTime.Now;
            if (iskaishi)
            {
                // 拿到下拉列表框的文本
                int hours = Convert.ToInt32(shi.Text);
                int miniuts = Convert.ToInt32(shi_Copy.Text);
                DateTime da= new DateTime(DateTime.Now.Year, DateTime.Now.Month,
                DateTime.Now.Day, hours, miniuts,0);
                // 拿到倒计时的时间
                tes = da - date;
                shengyu.Content = "剩余时间:"+tes.ToString();
                if (tes.TotalSeconds<=0)
                {
                    iskaishi = false;
                    timer.Stop();
                    // 启用所有控件,可以正常继续设置时间
                    shi.IsEnabled = true;
                    shi_Copy.IsEnabled = true;
                    音乐.IsEnabled = true;
                    queding.IsEnabled = true;
                    mic.Play();
                    if (MessageBox.Show("时间到啦,是否关闭音乐","闹钟",MessageBoxButton.YesNo,MessageBoxImage.Asterisk)==MessageBoxResult.Yes)
                    {
                        mic.Stop();
                    }
                    shengyu.Content = "时间到啦!!!";
                }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            btn.IsEnabled = false;
            // 选择音乐
            switch (音乐.SelectedIndex)
            {
                case 0:
                  mics = string.Format("../../mic/BG音乐.wav");
                 break;
                case 1:
                    mics = string.Format("../../mic/死亡.wav");
                    break;
            }
            // 设置音乐路径
            mic.SoundLocation = mics;
            // 设置下拉框和按钮不可选择
            iskaishi = true;
            shi.IsEnabled = false;
            shi_Copy.IsEnabled = false;
            音乐.IsEnabled = false;
            // 开始计时器
            timer.Start();
            string shijian = shi.Text+"时" + shi_Copy.Text+"分";
            naozhong.Items.Add(shijian);
        }
    }
}

你可能感兴趣的:(WPF动画编程)