c# panel背景图片切换闪烁的解决办法

代码如下:重绘panel,并在新panel的构造函数中,设置双缓冲即可;

 public partial class BackGroundPanel : Panel 
    {
        public BackGroundPanel()
        {
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint
                | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw 
                | ControlStyles.SupportsTransparentBackColor, true);

            //SetStyle(ControlStyles.
        }
        SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
        //protected override void OnPaintBackground(PaintEventArgs e)
        //{
        //    // 
        //    // 重载基类的背景擦除函数,
        //    // 解决窗口刷新,放大,图像闪烁
        //    // do nothing here: doesn't paint background => no flickering
        //    return;

        //}
        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    // render-code goes here
        //    this.DoubleBuffered = true;

        //    if (this.BackgroundImage != null)
        //    {
        //        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //        // e.ClipRectangle
        //        e.Graphics.DrawImage(this.BackgroundImage, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
        //        0, 0, this.BackgroundImage.Width, this.BackgroundImage.Height,
        //        System.Drawing.GraphicsUnit.Pixel);

        //    }

        //    base.OnPaint(e);
        //}
    }

同时,为了能够在测试容器中测试:

需要对designer.cs中的代码改动如下:

  #region 组件设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        /// 
        private void InitializeComponent()
        {
            this.panel_wholeFrameTrend = new BackGroundPanel();
        }
        BackGroundPanel panel_wholeFrameTrend;
        //private void InitializeComponent()
        //{
        //    components = new System.ComponentModel.Container();
        //    //this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        //}

        #endregion
    }


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值