您好,登錄后才能下訂單哦!
最近做個功能,根據(jù)表數(shù)據(jù)配置,在窗體上自動生成控件,自動布局,這個時候是沒有問題的;當(dāng)窗體大小改變時,控件的位置也要自動調(diào)整,這個時候窗體就會出現(xiàn)閃爍,看著很不爽,嚴(yán)重影響程序的使用,于是在在網(wǎng)上搜集解決方案,皇天不負(fù)有心人,終于把問題解決了,現(xiàn)講方法共享出來。
1、使用雙緩存
SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. SetStyle(ControlStyles.DoubleBuffer, true); //雙緩沖
這是我在網(wǎng)上搜集資料的時候,找到最多的回答,這個有一點(diǎn)用,但是效果確實(shí)不太明顯,于是繼續(xù)搜集,終于找到了另外一個能解決實(shí)際問題的方案。
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、為所有控件設(shè)置雙緩存
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; } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。