WPF钟表+闹钟(WPF的动画做的)

您所在的位置:网站首页 闹钟的动画 WPF钟表+闹钟(WPF的动画做的)

WPF钟表+闹钟(WPF的动画做的)

2024-07-15 23:35| 来源: 网络整理| 查看: 265

一、xaml文件

1、xaml的设计界面 在这里插入图片描述 2、代码xaml.cs文件里面写: 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 动画基础和动画时钟 { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } //创建动画 Storyboard story = new Storyboard(); //时针 Image shizhen = new Image(); //分针 Image fenzhen = new Image(); //秒针 Image miaozhen = new Image(); private void Window_Loaded(object sender, RoutedEventArgs e) { //获取时间 //DateTime time = new DateTime(); //int shi = time.Hour; //int fen = time.Minute; //int miao = time.Second; double shi = (DateTime.Now.Hour * 30) + (DateTime.Now.Minute * 0.5); double fen = DateTime.Now.Minute * 6; double miao = DateTime.Now.Second*6; this.Left = 270; this.Top=30; this.Width = 800; this.Height = 600; //时钟 Image shizhong = new Image(); shizhong.Width = 300; shizhong.Height = 300; Canvas.SetLeft(shizhong, this.Width / 2 - shizhong.Width / 2); Canvas.SetTop(shizhong, this.Height / 2 - shizhong.Height / 2); shizhong.Source = new BitmapImage(new Uri("../../img/时钟.png", UriKind.Relative)); GM.Children.Add(shizhong); //时针 //Image shizhen = new Image(); shizhen.Width = 50; shizhen.Height = 110; shizhen.Stretch = Stretch.Fill;//图片拉伸 shizhen.Source = new BitmapImage(new Uri("../../img/时针.png", UriKind.Relative)); Canvas.SetLeft(shizhen, this.Width / 2 - shizhen.Width / 2); Canvas.SetTop(shizhen, this.Height / 2 - shizhen.Height / 2 - 20); GM.Children.Add(shizhen); //分针 //Image fenzhen = new Image(); fenzhen.Width = 30; fenzhen.Height = 150; fenzhen.Stretch = Stretch.Fill; fenzhen.Source = new BitmapImage(new Uri("../../img/时针.png", UriKind.Relative)); Canvas.SetLeft(fenzhen, this.Width / 2 - fenzhen.Width / 2); Canvas.SetTop(fenzhen, this.Height / 2 - fenzhen.Height / 2 - 20); GM.Children.Add(fenzhen); //秒针 //Image miaozhen = new Image(); miaozhen.Width = 30; miaozhen.Height = 180; miaozhen.Stretch = Stretch.Fill; miaozhen.Source = new BitmapImage(new Uri("../../img/时针.png", UriKind.Relative)); Canvas.SetLeft(miaozhen, this.Width / 2 - miaozhen.Width / 2); Canvas.SetTop(miaozhen, this.Height / 2 - miaozhen.Height / 2 - 20); GM.Children.Add(miaozhen); //秒针旋转对象 RotateTransform rotateTransform = new RotateTransform(); //给秒针添加旋转位置,两个信息互相转换了 miaozhen.RenderTransform = rotateTransform; miaozhen.RenderTransformOrigin = new Point(0.5,0.5);//旋转位置 //添加旋转动画 DoubleAnimation xuanzhuan = new DoubleAnimation(miao, 360 + miao, new Duration(TimeSpan.FromSeconds(60))); //永动 xuanzhuan.RepeatBehavior = RepeatBehavior.Forever; Storyboard.SetTarget(xuanzhuan,miaozhen);//故事的演员 Storyboard.SetTargetProperty(xuanzhuan,new PropertyPath("RenderTransform.Angle"));//故事要执行的动作 story.Children.Add(xuanzhuan);//把这个动作添加到故事版里 story.Begin(); //分针:围绕指定指定点随时针方向旋转更换的对象 RotateTransform fenobj = new RotateTransform(); fenzhen.RenderTransform = fenobj; fenzhen.RenderTransformOrigin = new Point(0.5,0.5); //DoubleAnimation fendushu = new DoubleAnimation(0,360,new Duration(TimeSpan.FromSeconds(6))); DoubleAnimation fendushu = new DoubleAnimation(fen,fen+360, new Duration(TimeSpan.FromMinutes(60))); Storyboard.SetTarget(fendushu,fenzhen); Storyboard.SetTargetProperty(fendushu,new PropertyPath("RenderTransform.Angle")); fendushu.RepeatBehavior = RepeatBehavior.Forever; story.Children.Add(fendushu); story.Begin(); //时针,先添加旋转对象 RotateTransform miaoObj = new RotateTransform(); shizhen.RenderTransform = miaoObj; shizhen.RenderTransformOrigin = new Point(0.5,0.5); //获取旋转对象中RenderTransform.Angle的值就可以获取旋转位置 //DoubleAnimation shiXuan = new DoubleAnimation(0,360,new Duration(TimeSpan.FromSeconds(10))); DoubleAnimation shiXuan = new DoubleAnimation(shi, 360+shi, new Duration(TimeSpan.FromHours(12))); Storyboard.SetTarget(shiXuan, shizhen); Storyboard.SetTargetProperty(shiXuan, new PropertyPath("RenderTransform.Angle")); shiXuan.RepeatBehavior = RepeatBehavior.Forever; story.Children.Add(shiXuan); story.Begin(); //设置闹钟,弹出菜单 ContextMenu menu = new ContextMenu(); //弹出菜单的子集,并给子集设置,文本 MenuItem menu1 = new MenuItem(); menu1.Header = "设置闹钟"; menu1.Click += Menu1_Click; menu.Items.Add(menu1);//把子集菜单添加给弹出菜单 GM.ContextMenu = menu;//然后把弹出菜单添加给你想要右键的东西 } private void Menu1_Click(object sender, RoutedEventArgs e) { Window1 w = new Window1(); w.Show(); } } } 二、添加设置闹钟页面

1、xaml的设计界面 在这里插入图片描述

代码(设置定时器页面)xaml:

2、代码(设置闹钟的显示)xaml.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 动画基础和动画时钟 { /// /// Window1.xaml 的交互逻辑 /// public partial class Window1 : Window { public Window1() { InitializeComponent(); } //计时器 DispatcherTimer tim = new DispatcherTimer(); //音频 SoundPlayer die = new SoundPlayer("../../music/nao.wav"); private void Window_Loaded(object sender, RoutedEventArgs e) { //计时器 tim.Interval = new TimeSpan(1000); tim.Tick += Tim_Tick; } bool b = true; //接收剩余时间 TimeSpan result;//表示一个时间间隔 private void Tim_Tick(object sender, EventArgs e) { if (b) { //刷新时间 DateTime time = DateTime.Now; //获取小时 int shi1 = Convert.ToInt32(shi.Text); //获取分钟 int fen1 = Convert.ToInt32(fen.Text); //获取设置的时间 DateTime da = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, shi1, fen1, 0); //减去剩余的时间 result = da - time; //显示剩余的时间 te.Content = "剩余时间:" + result.ToString();//te添加空白Label,并给添加剩余时间 if (result.TotalSeconds


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3