WPF桌面下雪效果
Posted X3
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF桌面下雪效果相关的知识,希望对你有一定的参考价值。
寄存在canvas面板,目前缺点就是开始的时候效果有点别扭。
直接贴代码:
1 void StartSnowing(Canvas panel) 2 { 3 Random random = new Random(); 4 Task.Factory.StartNew(new Action(() => 5 { 6 for (int j = 0; j < 10; j++) 7 { 8 Thread.Sleep(j * 100); 9 Dispatcher.Invoke(new Action(() => 10 { 11 int snowCount = random.Next(0, 20); 12 for (int i = 0; i < snowCount; i++) 13 { 14 int width = random.Next(10, 20); 15 PackIconFontAwesome pack = new PackIconFontAwesome() 16 { 17 Kind = PackIconFontAwesomeKind.SnowflakeRegular, 18 Width = width, 19 Height = width, 20 Foreground = Brushes.White, 21 BorderThickness = new Thickness(0), 22 RenderTransform = new RotateTransform(), 23 }; 24 int left = random.Next(0, (int)panel.ActualWidth); 25 Canvas.SetLeft(pack, left); 26 panel.Children.Add(pack); 27 int seconds = random.Next(10, 20); 28 DoubleAnimationUsingPath doubleAnimation = new DoubleAnimationUsingPath() 29 { 30 Duration = new Duration(new TimeSpan(0, 0, seconds)), 31 RepeatBehavior = RepeatBehavior.Forever, 32 PathGeometry = new PathGeometry(new List<PathFigure>() { new PathFigure(new Point(left, 0), new List<PathSegment>() { new LineSegment(new Point(left, panel.ActualHeight), false) }, false) }), 33 Source = PathAnimationSource.Y 34 }; 35 pack.BeginAnimation(Canvas.TopProperty, doubleAnimation); 36 DoubleAnimation doubleAnimation1 = new DoubleAnimation(360, new Duration(new TimeSpan(0, 0, 10))) 37 { 38 RepeatBehavior = RepeatBehavior.Forever, 39 40 }; 41 pack.RenderTransform.BeginAnimation(RotateTransform.AngleProperty, doubleAnimation1); 42 } 43 })); 44 } 45 })); 46 }
以上是关于WPF桌面下雪效果的主要内容,如果未能解决你的问题,请参考以下文章