我发现了一个有趣的程序,它可以让你在桌面和窗口上覆盖雪花。作为一项编程挑战,我有兴趣自己尝试弄清楚如何做到这一点。更不用说这个程序有点占用内存(如果它没有内存泄漏的话)。下面是我的开始。我正在尝试用一张图片来了解基础知识,然后再进行扩展。
我真正需要帮助的是使图像移动更流畅自然。
编辑:
我在答案部分的下方发布了一个解决方案,但它比我想要的 CPU 密集度更高,有什么想法吗?
WPF XAML 代码:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AllowsTransparency="True"
WindowStyle="None"
Title="MainWindow" Height="350" Width="525" Background="Transparent" Topmost="True" WindowState="Maximized" ResizeMode="NoResize">
<Grid Name="grid1">
<Image Height="26" HorizontalAlignment="Left" Margin="{Binding flakeMargin}" Name="Image1" Stretch="Fill" VerticalAlignment="Top" Width="28" Source="/snowTest;component/Images/blue-pin-md.png" />
</Grid>
</Window>
VB代码:
Imports System.ComponentModel
Class MainWindow
Dim bw As BackgroundWorker = New BackgroundWorker
Dim flake0 As New flake
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
grid1.DataContext = flake0
AddHandler bw.DoWork, AddressOf backgroundMover
bw.RunWorkerAsync()
End Sub
Private Sub backgroundMover(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
While (True)
flake0.move()
System.Threading.Thread.Sleep(100)
End While
End Sub
End Class
薄片类:
Imports System.ComponentModel
Public Class flake
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Private Property startLeft As Integer = 300
Private Property left As Integer = left
Private Property top As Integer = 100
Private Property speed As Integer = 1
Public ReadOnly Property flakeMargin As Thickness
Get
Return New Thickness(left, top, 0, 0)
End Get
End Property
Public Sub move()
top += speed
left = (Math.Cos(top - 100)) * 6 + startLeft
NotifyPropertyChanged("flakeMargin")
End Sub
End Class