【发布时间】:2015-02-25 08:14:15
【问题描述】:
我已经做了几个星期了......归结为最简单的组件,我想以平滑的动作将一个圆圈从屏幕左侧移动到右侧,没有任何闪烁。
这是我的代码:我实例化了一个计时器 (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
}
}
}
【问题讨论】:
-
可以使用双缓冲吗?
-
那不应该闪烁那么多。您可以将
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;添加到您的绘画方法中,以使圆圈看起来更平滑一些。不知道你的投诉是什么。如果您正在寻找游戏质量的流畅度,那么您使用了错误的技术。 -
闪烁消失 this=doublebuffered;但是运动总是会跳动,因为没有与显示器同步。也许使用this 会有所帮助,但我还没有尝试过..
-
考虑使用Stopwatch 来跟踪实际经过的时间,然后根据Paint 中经过的时间计算_x,而不是在计时器中增加它。然后你可以让你的定时器调用无效。这仍然不会完全顺利,但它比 Timer 回调口吃要好得多。