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

来源:互联网 发布:淘宝网禁止出售的药品 编辑:程序博客网 时间:2024/05/21 22:27

代码如下:重绘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    }


0 0
原创粉丝点击