F#闪烁的createGraphics

时间:2014-01-29 23:01:25

标签: f#

嗨,我在Panel上的异步绘图有问题。如果我不清楚它的工作原理,但很明显它会一直闪烁。我在主循环中创建了Form和Panel,但我想在游戏循环中绘制。我的问题是如何在没有闪烁的情况下在图形面板上绘制

let rec gameLoop (gamePanel:Panel) (lastTime:int64) (ball:Ball) =  async {
    lock gamePanel ( fun() ->
        if gamePanel.IsDisposed || close then
            ()
        else
            let rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
            use graphics = gamePanel.CreateGraphics();
            graphics.Clear(Color.White) (* when i use this it flickers *)
            graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
            graphics.FillEllipse(ball.brush(), ball.rectangle());
    )
    return! gameLoop gamePanel (lastTime) (ball.move())
}

编辑x2。闪烁仍然存在,但也许我的方式很好

type PanelF() as this=
        inherit Panel()
        do this.DoubleBuffered <- true
        member this.setStyle(a,b) = this.SetStyle(a, b)

编辑x3。我添加了System.Threading.Thread.Sleep(30)它运行得更好但仍然不是我想要的

1 个答案:

答案 0 :(得分:3)

do this.DoubleBUffered<-true

在表单的构造函数中。这将启用双缓冲,通过渲染到隐藏缓冲区来停止闪烁,并在渲染完成后交换缓冲区。

以下是惯用F#风格的完整答案(使用class ... end并不常见。)

type PanelF() as this=
        inherit Panel()
        do this.DoubleBuffered <- true
        member this.setStyle(a,b) = this.SetStyle(a, b)