码迷,mamicode.com
首页 > Windows程序 > 详细

WPF桌面下雪效果

时间:2018-05-18 16:20:28      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:render   nes   point   cto   star   add   tle   actor   ever   

寄存在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         }
View Code

 

WPF桌面下雪效果

标签:render   nes   point   cto   star   add   tle   actor   ever   

原文地址:https://www.cnblogs.com/RedSky/p/9056648.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!