VC#表格闪烁

1 c# windows windows-7 winforms

所以这就是我想要做的事情.我正在使用SWF和SD命名空间进行游戏.当我使用1000/30间隔(30帧)的计时器时,在它的tick事件中我调用了InvokeGraphics() .所有的东西都或多或少地呈现,除了椭圆被绘制的事实.我尝试使用双缓冲和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);
    }

}
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 5

使用this.CreateGraphics()并没有建立一个双缓冲绘画方面.将窗体的DoubleBuffered属性设置为true.并使用间隔为32毫秒的Timer来强制刷新,只在其Tick事件处理程序中调用Invalidate().