C# tableLayoutPanel动态加载控件闪烁的解决方案

本文转载自k_set原创内容点击打开链接

WinForm加载多个自定义控件时,会出现很严重的闪烁问题,很卡,一块一块的加载(像打开网页时,网络很卡的那种感觉)简直没法忍受。

在网上搜索了好久,网上大部分的方法是一下4种,但是都不能有效的解决问题。

  1.将DoubleBuffered 设置 true,用双缓存处理Form界面内容加载,可以提高页面显示质量。或者

SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
this.UpdateStyles();

2.把构造方法里的内容尽量移动到Form_load事件里处理。

3.把控件绘制的Pint()写到一起绘制。

4.在控件使用后有变化时可以采用先 挂起 再显示的方法,提高显示质量。

this.tableLayoutPanel2.SuspendLayout();
                this.SuspendLayout();
                tableLayoutPanel2.Controls.Clear();
                
                tableLayoutPanel2.ColumnCount = value.ColumnCount;
                tableLayoutPanel2.ColumnStyles.Clear();
                for (int i = 0; i < this.tableLayoutPanel2.ColumnCount; i++) {
                    this.tableLayoutPanel2.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F/this.tableLayoutPanel2.ColumnCount));
                }
 
                this.tableLayoutPanel2.RowCount = value.ReservedCount / value.ColumnCount;
                tableLayoutPanel2.RowStyles.Clear();
                for (int i = 0; i < tableLayoutPanel2.RowCount; i++) {
                    this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Percent, 100F/tableLayoutPanel2.RowCount));
                }
 
                //  动态添加控件    
                //  在这儿添加你的代码
                ...
 
 
                tableLayoutPanel2.ResumeLayout(true);
                this.ResumeLayout(true);

你只有试了你就会发现,以上这几种办法并不能解决问题。 

  

  解决办法:

在调用自定义控件的窗体内添加的:

protected override CreateParams CreateParams
{
    get
    {
            CreateParams cp = base.CreateParams; 
            cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED 
            return cp; 
    }
}

在自定义控件中添加的:

protected override CreateParams CreateParams
{
       get
      {
              var parms = base.CreateParams; 
              parms.Style &= ~0x02000000; // Turn off WS_CLIPCHILDREN 
              return parms;
       }
}


本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

会员中心
搜索
«    2024年4月    »
1234567
891011121314
15161718192021
22232425262728
2930
网站分类
标签列表
最新留言
    热门文章 | 热评文章 | 随机文章
文章归档
友情链接
  • 订阅本站的 RSS 2.0 新闻聚合
  • 扫描加本站机器视觉QQ群,验证答案为:halcon勇哥的机器视觉
  • 点击查阅微信群二维码
  • 扫描加勇哥的非标自动化群,验证答案:C#/C++/VB勇哥的非标自动化群
  • 扫描加站长微信:站长微信:abc496103864
  • 扫描加站长QQ:
  • 扫描赞赏本站:
  • 留言板:

Powered By Z-BlogPHP 1.7.2

Copyright Your skcircle.com Rights Reserved.

鄂ICP备18008319号


站长QQ:496103864 微信:abc496103864