使用按钮

时间:2016-01-29 16:45:01

标签: f# system.drawing

我正在尝试绘制一块电路板并让用户在按下按钮时执行操作。

似乎悬停按钮会调用表单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()

1 个答案:

答案 0 :(得分:2)

使用具有双缓冲的表单(http://fssnip.net/rA

if __name__ == '__main__':
    import import_test
    import_test.run()