1. 程式人生 > >C#窗體加載和控件加載不同步導致控件閃爍

C#窗體加載和控件加載不同步導致控件閃爍

orm header rop nts int new 引用 加載 use

窗體加載和控件加載不同步導致的控件閃爍現象:
// 代碼塊加在父窗體中的任意位置,解決窗體加載和控件加載不同步導致的控件閃爍問題
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}

原理很簡單,引用以下原話:
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.

C#窗體加載和控件加載不同步導致控件閃爍