简体   繁体   English

VC# 表单闪烁

[英]VC# Form Flickers

So here is what I am trying to do.I am making a game using only SWF and SD namespaces.When I use a timer with the 1000/30 interval(30 frames),in it's tick event i have a call to InvokeGraphics().Everything renders more or less fine,except the fact that the ellipse is drawn flickred.I tried using double buffering,and this.SetStyle(),but both failed.Here is the code:所以这就是我想要做的。我只使用 SWF 和 SD 命名空间制作游戏。当我使用间隔为 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);
    }

}

Using this.CreateGraphics() does not create a double-buffered painting context.使用 this.CreateGraphics()不会创建双缓冲的绘画上下文。 Set the form's DoubleBuffered property to true.将窗体的 DoubleBuffered 属性设置为 true。 And use a Timer with an Interval of 32 msec to force a refresh, only call Invalidate() in its Tick event handler.并使用间隔为 32 毫秒的计时器强制刷新,仅在其 Tick 事件处理程序中调用 Invalidate()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM