WPF开发者QQ群: 340500857 | 微信群 -> 进入公众号主页 加入组织
由于微信群人数太多入群请添加小编微信号
(yanjinhuawechat)或(W_Feng_aiQ)邀请入群
(需备注WPF开发者)
PS:有更好的方式欢迎推荐。
01
—
代码如下
一、创建 SnowCanvas.cs 继承 Control代码如下。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;namespace WPFDevelopers.Controls
{[TemplatePart(Name = CanvasTemplateName, Type = typeof(Canvas))]public class SnowCanvas: Control{private const string CanvasTemplateName = "PART_Canvas";private Canvas _canvas;private readonly Random _random = new Random((int)DateTime.Now.Ticks);public ImageSource Icon{get { return (ImageSource)GetValue(IconProperty); }set { SetValue(IconProperty, value); }}public static readonly DependencyProperty IconProperty =DependencyProperty.Register("Icon", typeof(ImageSource), typeof(SnowCanvas), new PropertyMetadata(null));static SnowCanvas(){DefaultStyleKeyProperty.OverrideMetadata(typeof(SnowCanvas), new FrameworkPropertyMetadata(typeof(SnowCanvas)));}public override void OnApplyTemplate(){base.OnApplyTemplate();_canvas = GetTemplateChild(CanvasTemplateName) as Canvas;if (_canvas == null) return;this.Loaded += (s, e) => {var timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(300) };timer.Tick += (s1, arg) => AddSnowflake();timer.Start();};}protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e){base.OnMouseLeftButtonDown(e);}private void AddSnowflake(){var x = _random.Next(0, (int)_canvas.ActualWidth);var y = -10;var size = _random.Next(4, 12);var translateTransform = new TranslateTransform(x, y);var snowflake = new Snowflake{RenderTransform = new TransformGroup{Children = new TransformCollection { translateTransform }},HorizontalAlignment = HorizontalAlignment.Left,VerticalAlignment = VerticalAlignment.Top,Width = size,Height = size};_canvas.Children.Add(snowflake);y += (int)(_canvas.ActualHeight + 10);DoubleAnimation animation = new DoubleAnimation{To = y,Duration = TimeSpan.FromSeconds(_random.Next(3, 8))};Storyboard.SetTarget(animation, snowflake);Storyboard.SetTargetProperty(animation, new PropertyPath("RenderTransform.Children[0].Y"));Storyboard story = new Storyboard();story.Completed += (sender, e) => _canvas.Children.Remove(snowflake);story.Children.Add(animation);snowflake.Loaded += (sender, args) => story.Begin();}}
}
二、Snowflake.xaml 代码如下
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;namespace WPFDevelopers.Controls
{public class Snowflake: Control{static Snowflake(){DefaultStyleKeyProperty.OverrideMetadata(typeof(Snowflake), new FrameworkPropertyMetadata(typeof(Snowflake)));}public Snowflake(){CacheMode = new BitmapCache();}public override void OnApplyTemplate(){base.OnApplyTemplate();}}
}
三、SnowCanvas.xaml代码如下
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:controls="clr-namespace:WPFDevelopers.Controls"><ResourceDictionary.MergedDictionaries><ResourceDictionary Source="Basic/ControlBasic.xaml"/></ResourceDictionary.MergedDictionaries><Style TargetType="{x:Type controls:SnowCanvas}" BasedOn="{StaticResource ControlBasicStyle}"><Setter Property="Width" Value="672"/><Setter Property="Height" Value="376"/><Setter Property="Background" Value="#FFAD2B28"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type controls:SnowCanvas}"><Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"Background="{TemplateBinding Background}"><Canvas x:Name="PART_Canvas" Height="246" Width="246"Background="#FF0C1935"><Canvas.Clip><EllipseGeometry Center="123,123" RadiusX="120" RadiusY="120"/></Canvas.Clip><Image Source="{TemplateBinding Icon}"Canvas.Left="25" Canvas.Top="30"/></Canvas></Grid></ControlTemplate></Setter.Value></Setter>
</Style>
<Style TargetType="{x:Type controls:Snowflake}" BasedOn="{StaticResource ControlBasicStyle}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type controls:Snowflake}"><Ellipse Fill="{StaticResource WhiteSolidColorBrush}"Width="{TemplateBinding Width}"Height="{TemplateBinding Height}"/></ControlTemplate></Setter.Value></Setter>
</Style>
</ResourceDictionary>
四、SnowCanvasExample.xaml代码如下
<UserControl x:Class="WPFDevelopers.Samples.ExampleViews.SnowCanvasExample"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews"xmlns:wpfdev="https://github.com/yanjinhuagood/WPFDevelopers"mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"><Grid><wpfdev:SnowCanvas Icon="pack://application:,,,/Images/Snow/SnowTree.png"/></Grid>
</UserControl>
02
—
效果预览
鸣谢素材提供者 - Alyssa Nicoll
源码地址如下
github:https://github.com/yanjinhuagood/WPFDevelopers.git
gitee:https://gitee.com/yanjinhua/WPFDevelopers.git
WPF开发者QQ群: 340500857
blogs: https://www.cnblogs.com/yanjinhua
Github:https://github.com/yanjinhuagood
出处:https://www.cnblogs.com/yanjinhua
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
转载请著名作者 出处 https://github.com/yanjinhuagood
扫一扫关注我们,
更多知识早知道!
点击阅读原文可跳转至源代码