转载:https://www.cnblogs.com/1175429393wljblog/p/8391902.html
参考:https://social.msdn.microsoft.com/Forums/windows/en-US/aaed00ce-4bc9-424e-8c05-c30213171c2c/flickerfree-painting?forum=winforms
原因:
-
Windows sends a control two messages when a control needs to be painted. The first one (WM_ERASEBKGND) causes the background to be painted (OnPaintBackground), the second causes the foreground to be painted (WM_PAINT, firing OnPaint). Seeing the background drawn first, then the foreground is noticeable when the drawing is slow. Windows Forms has a ready solution for this kind of flicker with ControlStyles.OptimizedDoubleBuffer.
-
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. This effectively solves the 2nd cause of flicker.
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}
本文探讨了Windows窗体中常见的闪烁问题,详细解释了其产生的原因,并提供了一种有效解决方案。通过启用WS_EX_COMPOSITED样式,Windows XP可以对窗体及其所有子控件进行双缓冲,从而解决闪烁问题。
1万+

被折叠的 条评论
为什么被折叠?



