小墨の博客

梦想需要付诸行动,否则只能是梦

解决 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

张小弟之家

本文链接:
文章标题:

本站文章除注明转载/出处外,均为原创,若要转载请务必注明出处。转载后请将转载链接通过邮件告知我站,谢谢合作。本站邮箱:admin@only4.work

尊重他人劳动成果,共创和谐网络环境。点击版权声明查看本站相关条款。

    发表评论:

    搜索
    本文二维码
    标签列表
    站点信息
    • 文章总数:507
    • 页面总数:20
    • 分类总数:92
    • 标签总数:207
    • 评论总数:60
    • 浏览总数:219202

    | | |
    | |  Z-Blog PHP