WinForm 加載自定義控件閃爍問題


WinForm加載多個自定義控件時,會出現很嚴重的閃爍問題,很卡,一塊一塊的加載(像打開網頁時,網絡很卡的那種感覺)簡直沒法忍受。

在網上搜索了好久,網上大部分的方法是一下4種,但是都不能有效的解決問題。

  1.將DoubleBuffered 設置 true,用雙緩存處理Form界面內容加載,可以提高頁面顯示質量。或者

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

this.UpdateStyles();

  2.把構造方法里的內容盡量移動到Form_load事件里處理。

  3.把控件繪制的Pint()寫到一起繪制。

  4.在控件使用后有變化時可以采用先 掛起 再顯示的方法,提高顯示質量。

 1                 this.tableLayoutPanel2.SuspendLayout();
 2                 this.SuspendLayout();
 3                 tableLayoutPanel2.Controls.Clear();
 4                 
 5                 tableLayoutPanel2.ColumnCount = value.ColumnCount;
 6                 tableLayoutPanel2.ColumnStyles.Clear();
 7                 for (int i = 0; i < this.tableLayoutPanel2.ColumnCount; i++) {
 8                     this.tableLayoutPanel2.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F/this.tableLayoutPanel2.ColumnCount));
 9                 }
10 
11                 this.tableLayoutPanel2.RowCount = value.ReservedCount / value.ColumnCount;
12                 tableLayoutPanel2.RowStyles.Clear();
13                 for (int i = 0; i < tableLayoutPanel2.RowCount; i++) {
14                     this.tableLayoutPanel2.RowStyles.Add(new RowStyle(SizeType.Percent, 100F/tableLayoutPanel2.RowCount));
15                 }
16 
17                 //  動態添加控件    
18                 //  在這兒添加你的代碼
19                 ...
20 
21 
22                 tableLayoutPanel2.ResumeLayout(true);
23                 this.ResumeLayout(true);
24     


 

你只有試了你就會發現,以上這幾種辦法並不能解決問題。

  

  

  解決辦法:

在調用自定義控件的窗體內添加的:

1 protected override CreateParams CreateParams
2 {
3     get
4     {
5             CreateParams cp = base.CreateParams; 
6             cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED 
7             return cp; 
8     }
9 }

 

在自定義控件中添加的:

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

 

 裝模作樣的聲明一下:本博文章若非特殊注明皆為原創,若需轉載請保留原文鏈接(http://www.cnblogs.com/kest/p/4683012.html)及作者信息k_est

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM