溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

解決WinForm界面閃爍問題

發(fā)布時間:2020-09-14 13:04:02 來源:網(wǎng)絡(luò) 閱讀:1908 作者:Misy 欄目:編程語言

最近做個功能,根據(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;
            }
        }


向AI問一下細(xì)節(jié)

免責(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)容。

AI