版权声明:我不生产代码,我只是代码的搬运工。 https://blog.csdn.net/qq_43307934/article/details/87249998
- WPF提供了一种基于帧的动画实现方式,由CompositionTarget类来完成。
- WPF会在每次界面刷新时调用该回调函数。
- CompositionTarget的刷新率与窗体保持一致,因此很难人工控制动画的快慢。
?1、前台
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=" *" />
<RowDefinition Height=" *" />
</Grid.RowDefinitions>
<Rectangle Name="rectan"
Width=" 30"
Height=" 100"
Fill="Blue" />
<Button Grid.Row=" 1"
Width=" 100"
Height=" 30"
Click="Button_Click" />
</Grid>
2、后台cs
public partial class MainWindow : Window
{
private double maxwi = 300;
private double strart = 0;
public MainWindow()
{
InitializeComponent();
strart = rectan.Width;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
}
private void CompositionTarget_Rendering(object sender, EventArgs e)
{
rectan.Width += 1;
if (rectan.Width > maxwi)
{
rectan.Width = strart;
CompositionTarget.Rendering -= new EventHandler(CompositionTarget_Rendering);
}
}
原文地址:https://www.cnblogs.com/lonelyxmas/p/10729340.html
时间: 2024-11-13 22:55:45