這個問題屬于必須解決的問題捅膘,而且界面的控件越多,閃爍也越多冕碟,試過多種解決辦法效果都不理想。
解決辦法:把此段代碼加入到窗體代碼中
protected override CreateParams CreateParams {
get {
CreateParams paras = base.CreateParams;
paras.ExStyle |= 0x02000000;
return paras;
}
}
主要原因是對于Winform來說匆浙,一個窗體中繪制多個控件是很花時間的安寺。特別是默認的按鈕控件。Form先畫出背景首尼,然后留下控件需要的“洞”挑庶。如果控件的背景是透明的言秸,那么這些“洞”就會先以白色或黑色出現(xiàn),然后每個控件的“洞”再被填充迎捺,就是我們所看到的閃爍举畸,在WinForm中沒有現(xiàn)成的解決方案。設(shè)置控件雙緩沖并不能解決它凳枝,因為它只適用于自己抄沮,而不是復(fù)合控件集。
而原文作者發(fā)現(xiàn)了一種新的Windows樣式岖瑰,可用于WindowsXP和Vista叛买。只要你設(shè)置該樣式,WindowsXP對窗體及其所有子控件進行雙重緩沖蹋订。
而我測試結(jié)果是 Win7 / Win10 一樣奏效
原理原文
A form that has a lot of controls takes a long time to paint. Especially the Button control in its default style is expensive. Once you get over 50 controls, it starts getting noticeable. The Form class paints its background first and leaves "holes" where the controls need to go. Those holes are usually white, black when you use the Opacity or TransparencyKey property. Then each control gets painted, filling in the holes. The visual effect is ugly and there's no ready solution for it in Windows Forms. Double-buffering can't solve it as it only works for a single control, not a composite set of controls.
I discovered a new Windows style in the SDK header files, available for Windows XP and (presumably) Vista: WS_EX_COMPOSITED. With that style turned on for your form, Windows XP does double-buffering on the form and all its child controls.