WPF 实现雪花效果
雪花控件类: class MM:Control { DispatcherTimer dispatcherTimer = new DispatcherTimer(); public MM() { dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100); ; dispatcherTimer.Tick += DispatcherTimer_Tick; dispatcherTimer.Start(); } private void DispatcherTimer_Tick(object sender, EventArgs e) { this.InvalidateVisual(); } protected override void OnRender(DrawingContext drawingContext) { var currentcolor = Colors.White; Brush brush = new RadialGradientBrush(currentcolor, Color.FromArgb(0, currentcolor.R, currentcolor.G, currentcolor.B)); Random r = new Random(); for (int i = 0; i < 530; i++) { var w = 35 * r.NextDouble(); var rect = new RectangleGeometry( new Rect(new Point(r.Next(10,(int)this.Width), r.Next(10, (int)this.Height)), new Size(w, w))); drawingContext.DrawGeometry(brush, null, rect); } } }
前台代码: <Window x:Class="WpfAppTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfAppTest" mc:Ignorable="d" WindowStyle="None" AllowsTransparency="True" Top="100" Left="100" Title="MainWindow" Height="450" Width="800"> <Window.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> <GradientStop Color="#05438C" Offset="0"/> <GradientStop Color="White" Offset="0.9"/> <GradientStop Color="White" Offset="1"/> </LinearGradientBrush> </Window.Background> <local:MM x:Name="cc" HorizontalAlignment="Left" Height="417.114" VerticalAlignment="Top" Width="791.112" RenderTransformOrigin="0.5,0.5" Margin="0,0.007,0,0"> <local:MM.RenderTransform> <TransformGroup> <ScaleTransform/> <SkewTransform/> <RotateTransform Angle="0.387"/> <TranslateTransform/> </TransformGroup> </local:MM.RenderTransform> </local:MM> </Window>
实现效果: