解决 C# Winform 窗体打开时闪烁问题
这个问题属于必须解决的问题,而且界面的控件越多,闪烁也越多,试过多种解决办法效果都不理想。
解决办法:把此段代码加入到窗体代码中
protected override CreateParams CreateParams {
get {
CreateParams paras = base.CreateParams;
paras.ExStyle |= 0x02000000;
return paras;
}
}
主要原因是对于Winform来说,一个窗体中绘制多个控件是很花时间的。特别是默认的按钮控件。Form先画出背景,然后留下控件需要的“洞”。如果控件的背景是透明的,那么这些“洞”就会先以白色或黑色出现,然后每个控件的“洞”再被填充,就是我们所看到的闪烁,在WinForm中没有现成的解决方案。设置控件双缓冲并不能解决它,因为它只适用于自己,而不是复合控件集。
而原文作者发现了一种新的Windows样式,可用于WindowsXP和Vista。只要你设置该样式,WindowsXP对窗体及其所有子控件进行双重缓冲。
而我测试结果是 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.
转载于:https://www.jianshu.com/p/15e2177e3122
下一篇: 第一次上女友家