2014-01-29 29 views
0

嗨,我有面板异步绘图的问题。如果我没有清除它的作品,但清晰,它一直闪烁。我在主循环中创建窗体和面板,但我想绘制游戏循环。我的问题是如何在没有闪烁的情况下在图形面板上绘图F#闪烁createGraphics

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),它工作得更好,但仍然没有完全是我想要的

回答

2

do this.DoubleBUffered<-true 

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

这里是真正地道的F#风格的一个完整的答案(使用class ... end不common`)

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

它不工作 – Luffy

+0

@Luffy - 你看过我雁 - 你需要把'这一点。 doublebuffered < - true'在构造函数中,而不是成员。 –

+0

我无法在构造函数中设置它。我得到了无效的表达。 new()= {inherit Panel(); this.DoubleBUffered < - true} – Luffy