这就是我想要做的。我正在制作一个仅使用 S.W.F 和 S.D 命名空间的游戏。当我使用间隔为 1000/30(30 帧)的计时器时,在它的滴答事件中我有一个电话InvokeGraphics()。一切都或多或少呈现得很好,除了椭圆是用 flickred 绘制的。我尝试使用双缓冲和 this.SetStyle(),但都失败了。这是代码:
public partial class MainForm : Form
{
int x = 0;
public MainForm()
{
InitializeComponent();
var sz = SystemInformation.PrimaryMonitorSize;
this.FormBorderStyle = FormBorderStyle.None;
this.Size = sz;
this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
Timer tmr = new Timer();
tmr.Enabled = true;
tmr.Interval = 1000/30;
tmr.Tick += delegate(object sender, EventArgs e)
{
x++;
this.InvokePaint(this,new PaintEventArgs(this.CreateGraphics(),this.Bounds));
};
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
if((int)e.KeyChar == 27) Application.Exit();
base.OnKeyPress(e);
}
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
g.Clear(Color.Firebrick);
// this ellipse flickrs
g.FillEllipse(Brushes.Green,x,64,64,64);
base.OnPaint(e);
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
}
}
最佳答案
使用 this.CreateGraphics() 不会创建双缓冲绘画上下文。将窗体的 DoubleBuffered 属性设置为 true。并使用间隔为 32 毫秒的计时器强制刷新,仅在其 Tick 事件处理程序中调用 Invalidate()。
关于c# - VC# 窗体闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726126/