使用按钮时闪烁形式
问题描述:
我正在尝试画一个电路板并让用户在按下按钮时执行动作。使用按钮时闪烁形式
似乎悬停按钮调用窗体的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()
答
使用的一种形式与双缓冲(http://fssnip.net/rA)
/// Double-buffered form
type CompositedForm() =
inherit Form()
override this.CreateParams =
let cp = base.CreateParams
cp.ExStyle <- cp.ExStyle ||| 0x02000000
cp
标志值coressponds到WS_CLIPCHILDREN:不包括绘制时子窗口所占用的面积父窗口内发生。创建父窗口时使用此样式。 (MSDN) –