1. 程式人生 > >WPF桌面下雪效果

WPF桌面下雪效果

panel onu IT art onf paths rotate poi 下雪效果

寄存在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桌面下雪效果