2016-01-29 179 views
0

我正在嘗試畫一個電路板並讓用戶在按下按鈕時執行動作。使用按鈕時閃爍形式

似乎懸停按鈕調用窗體的Paint重繪板和觸發窗體的閃爍。

雖然在Paint上重新繪製板子是正常的(使用動作將決定板子上的變化),有什麼辦法可以避免閃爍?也許在某些事件上致電表格Invalidate?也許找到替代CreateGraphics

open System.Windows.Forms 
open System.Drawing 

type BoardForm() = 
    let button = new Button() 

    let initializeButton (button:Button) left top caption sizeX sizeY enabled callback = 
     button.Text<-caption 
     button.Top<-top 
     button.Left<-left 
     button.Size<-new Size(sizeX,sizeY) 
     button.Click.Add(callback) 

    let drawBoard (form:Form) (x:int) (y:int) (width:int) (height:int) =   
     let brushBoard = new SolidBrush(Color.Beige) 
     let g = form.CreateGraphics() 
     g.FillRectangle(brushBoard, x, y, width, height) 

     let cellSize = 10 
     let cellsX = 10 
     let cellsY = 10 
     use pen = new Pen(Brushes.Black) 
     for i in [0..cellsY] do 
      g.DrawLine(pen, x, y+i*cellSize, x+cellsX*cellSize, y+i*cellSize) 

    let drawButtons (form:Form) = 
     let left = 10 
     let top = 300 
     let buttonWidth = 50 
     let buttonHeight = 30 

     initializeButton button left top "Ping" 50 buttonHeight true (fun _ -> printfn "I was pushed") 
     [button] |> Seq.cast<Control> |> Array.ofSeq |> form.Controls.AddRange 

    let initializeForm() =   
     let formWidth = 240 
     let formHeight = 400 
     let x = 10 
     let y = 10 
     let width = 200 
     let height = 200 
     let form = new Form(Width=formWidth, Height=formHeight, Visible=true, Text="Some form", TopMost=true)   

     form.Paint.Add(fun e -> drawBoard form x y width height) 

     drawButtons form  

    member this.Start() = 
     initializeForm() 

let boardForm = new BoardForm() 
boardForm.Start() 

回答

2

使用的一種形式與雙緩衝(http://fssnip.net/rA

/// Double-buffered form 
type CompositedForm() = 
    inherit Form() 
    override this.CreateParams = 
     let cp = base.CreateParams 
     cp.ExStyle <- cp.ExStyle ||| 0x02000000 
     cp 
+0

標誌值coressponds到WS_CLIPCHILDREN:不包括繪製時子窗口所佔用的面積父窗口內發生。創建父窗口時使用此樣式。 (MSDN) –