我找到了a fun program,它允许你overlay snowflakes over your desktop and windows。作为编程挑战,我有兴趣试图弄清楚如何自己做到这一点。更何况这个程序是一个内存猪(如果它没有内存泄漏)。以下是我的开始。我试图让一个图像的基础知识,然后将扩大。移动元素像飘落的雪花
我真正想要的帮助是让图像更流畅自然地移动。
编辑:
我张贴在回答部分楼下的解决方案,但它是更多的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
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
不知道为什么,这让收,好像一个真正的问题给我。我的anser会:因为你试图在WPF中做每帧动画,你应该使用CompositionTarget.Redering事件而不是你自己的时钟。在渲染事件中,你可以调用你的“绘制”代码,这将是移动图像。您可能想要使用画布而不是网格作为主机元素 –