资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

解决WinForm界面闪烁问题

最近做个功能,根据表数据配置,在窗体上自动生成控件,自动布局,这个时候是没有问题的;当窗体大小改变时,控件的位置也要自动调整,这个时候窗体就会出现闪烁,看着很不爽,严重影响程序的使用,于是在在网上搜集解决方案,皇天不负有心人,终于把问题解决了,现讲方法共享出来。

创新互联主营滦州网站建设的网络公司,主营网站建设方案,app软件开发,滦州h5小程序制作搭建,滦州网站营销推广欢迎滦州等地区企业咨询


1、使用双缓存

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
SetStyle(ControlStyles.DoubleBuffer, true); //双缓冲


这是我在网上搜集资料的时候,找到最多的回答,这个有一点用,但是效果确实不太明显,于是继续搜集,终于找到了另外一个能解决实际问题的方案。


2、在主窗体的任意位置重写CreateParams

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;////用双缓冲从下到上绘制窗口的所有子孙
        return cp;
    }
}


参考资料:http://www.dotblogs.com.tw/rainmaker/archive/2012/02/22/69811.aspx

How to fix the flickering in User controls


http://blog.csdn.net/onejune2013/article/details/7664323

Flicker-free painting

3、为所有控件设置双缓存

        private PropertyInfo _PropertyInfo = null;
        
        public IPNWidget()
        {
            InitializeComponent();

            this.DoubleBuffered = true;
            this.SetStyle(
                        ControlStyles.UserPaint |
                        ControlStyles.AllPaintingInWmPaint |
                        ControlStyles.DoubleBuffer, true);

            this._PropertyInfo = this.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            foreach (Control rootCtrl in this.Controls)
            {
                this._PropertyInfo.SetValue(rootCtrl, true, null);

                if (rootCtrl.HasChildren)
                    SearchControl(rootCtrl);
            }
        }
        
        void SearchControl(Control Ctrl)
        {
            foreach (Control rootCtrl in Ctrl.Controls)
            {
                //Debug.WriteLine(rootCtrl.Name + " 建立DoubleBuffer");
                this._PropertyInfo.SetValue(rootCtrl, true, null);
                if (rootCtrl.HasChildren)
                    SearchControl(rootCtrl);
                else
                    break;
            }
        }

新闻标题:解决WinForm界面闪烁问题
文章来源:http://cdkjz.cn/article/giidis.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220