我已经在这里待了几个星期......归结为它最简单的组件,我希望以一个平滑的动作从屏幕左侧移动一个圆圈到右侧,没有任何闪烁。
这是我的代码:我实例化了一个计时器(timer1)并在表单的属性窗口中启用并尝试了1-100的值。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
private int _y;
private int _x;
public Form1()
{
InitializeComponent();
_x = 0;
_y = 200;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Green, _x, _y, 30, 30);
}
private void timer1_Tick(object sender, EventArgs e)
{
_x += 1;
Invalidate(); //ie: redraw
}
}
}
答案 0 :(得分:2)
定时器是离散的,所以总会有一些滞后或口吃。
当WPF内置动画(或一般的对象转换)时,我不会使用WinForms进行此类动画。以下XAML在5秒内平滑地将(0,200)到(350,200)的红点设置为平滑动画动画。
<Canvas>
<Canvas.Resources>
<PathGeometry x:Key="AnimationPath">
<PathGeometry.Figures>
<PathFigure>
<LineSegment Point="0,200" />
<LineSegment Point="350,200" />
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Canvas.Resources>
<Ellipse Canvas.Top="200" Width="30" Height="30" Fill="Red">
<Ellipse.RenderTransform>
<TranslateTransform x:Name="AnimatedTranslation" />
</Ellipse.RenderTransform>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Path.Loaded">
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<DoubleAnimationUsingPath
Storyboard.TargetName="AnimatedTranslation"
Storyboard.TargetProperty="X"
PathGeometry="{StaticResource AnimationPath}"
Source="X"
Duration="0:0:5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</Canvas>