解决WinForm界面闪烁问题

发布时间:2020-09-14 13:04:02 作者:Misy
来源:网络 阅读:1897

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


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;
            }
        }


推荐阅读:
  1. Winform刷新时界面假死、闪烁怎么办
  2. 如何解决vue渲染时闪烁{{}}的问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

winform 闪烁

上一篇:解决mui框架中switch开关通过js控制开或者关状态时小圆点不动的问题

下一篇:解决webpack dev-server不能匹配post请求的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》