none
c# winform 界面显示加载性能调优! RRS feed

  • 问题

  • 有一个Form,贴了一张背景图,托了一个FlowLayoutPanel控件(设置其背景色透明),和一个按钮,点击按钮的时候,在FlowLayoutPanel里动态添加了一些PictureBox控件(也是背景色透明),这时明显感觉到加载速度很慢,当把背景色透明属性去掉后,显示速度很快,请问各位老师,有没有解决的方法!


    www.shshunfu.com
    • 已编辑 小猪飞飞 2011年10月8日 2:33 字输入有误
    2011年10月8日 2:32

答案

  • Hi 小猪飞飞,
    欢迎来到MSDN 论坛。

    谢谢您给我发来演示项目。

    在运行程序后,我注意到了下面两个问题:
    1.  BackGroundPanelTrend 控件中,在paint事件中不断地重绘控件。      

            protected override void OnPaintBackground(PaintEventArgs e)
            {
                // 重载基类的背景擦除函数,
                // 解决窗口刷新,放大,图像闪烁
                // do nothing here: doesn't paint background => no flickering
                return;
            }
     
            protected override void OnPaint(PaintEventArgs e)
            {
                this.DoubleBuffered = true;
                // if (this.BackgroundImage != null)
                // {
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                e.Graphics.DrawImage(Resources.bg, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
                0, 0, Resources.bg4.Width, Resources.bg.Height,
                System.Drawing.GraphicsUnit.Pixel);
                //}
                base.OnPaint(e);
            }
     
    

     

     可以在把它们注释掉以后,选择在初始化该控件时进行背景设置:        

            protected override void InitLayout()
            {
                System.Drawing.Graphics g = this.CreateGraphics();
                this.DoubleBuffered = true;
                this.BackgroundImage = Resources.bg;           
                g.DrawImage(Resources.bg, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
                0, 0, Resources.bg4.Width, Resources.bg.Height,
                System.Drawing.GraphicsUnit.Pixel);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                base.InitLayout();
            }
    

     

    2.尝试把30个控件分别添加到 backGroundPanelTrend中:          

                for (int i = 0; i < 30; i++)
                {
                    UserControl1 c = new UserControl1();
                     c.BackColor = Color.Transparent;
                    c.Size = userControl11.Size;
                    c.BackgroundImage = userControl11.BackgroundImage;
                    c.Name = i.ToString();
                    this.backGroundPanelTrend1.Controls.Add(c);
                }
    

     

       可以把上述代码进行了以下替换:         

     

                Control[] controls = new Control[30];
                for (int i = 0; i < 30; i++)
                {
                    UserControl1 c = new UserControl1();
                    c.BackColor = Color.Transparent;
                    c.Size = userControl11.Size;              
                    c.BackgroundImage = userControl11.BackgroundImage;
                    c.Name = i.ToString();
                    controls[i] = c;
                   // this.backGroundPanelTrend1.Controls.Add(c);
                }
                this.backGroundPanelTrend1.Controls.AddRange(controls);
    

    祝您,
    一切顺利! 


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年10月12日 6:58
    版主

全部回复

  • Hi 小猪飞飞,
    欢迎来到MSDN论坛!

    方便的话,你可以把代码贴上来吗?不知道您是如何实现的,我们无法得知潜在的性能问题。

    您也可以把整个项目发送到 v-yoyj at microsoft dot com.

    谢谢!
    Yoyo.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年10月11日 5:59
    版主
  • thanks answer! 我已经将项目发至您的邮箱,请查收!
    www.shshunfu.com
    2011年10月12日 1:31
  • Hi 小猪飞飞,
    欢迎来到MSDN 论坛。

    谢谢您给我发来演示项目。

    在运行程序后,我注意到了下面两个问题:
    1.  BackGroundPanelTrend 控件中,在paint事件中不断地重绘控件。      

            protected override void OnPaintBackground(PaintEventArgs e)
            {
                // 重载基类的背景擦除函数,
                // 解决窗口刷新,放大,图像闪烁
                // do nothing here: doesn't paint background => no flickering
                return;
            }
     
            protected override void OnPaint(PaintEventArgs e)
            {
                this.DoubleBuffered = true;
                // if (this.BackgroundImage != null)
                // {
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                e.Graphics.DrawImage(Resources.bg, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
                0, 0, Resources.bg4.Width, Resources.bg.Height,
                System.Drawing.GraphicsUnit.Pixel);
                //}
                base.OnPaint(e);
            }
     
    

     

     可以在把它们注释掉以后,选择在初始化该控件时进行背景设置:        

            protected override void InitLayout()
            {
                System.Drawing.Graphics g = this.CreateGraphics();
                this.DoubleBuffered = true;
                this.BackgroundImage = Resources.bg;           
                g.DrawImage(Resources.bg, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
                0, 0, Resources.bg4.Width, Resources.bg.Height,
                System.Drawing.GraphicsUnit.Pixel);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                base.InitLayout();
            }
    

     

    2.尝试把30个控件分别添加到 backGroundPanelTrend中:          

                for (int i = 0; i < 30; i++)
                {
                    UserControl1 c = new UserControl1();
                     c.BackColor = Color.Transparent;
                    c.Size = userControl11.Size;
                    c.BackgroundImage = userControl11.BackgroundImage;
                    c.Name = i.ToString();
                    this.backGroundPanelTrend1.Controls.Add(c);
                }
    

     

       可以把上述代码进行了以下替换:         

     

                Control[] controls = new Control[30];
                for (int i = 0; i < 30; i++)
                {
                    UserControl1 c = new UserControl1();
                    c.BackColor = Color.Transparent;
                    c.Size = userControl11.Size;              
                    c.BackgroundImage = userControl11.BackgroundImage;
                    c.Name = i.ToString();
                    controls[i] = c;
                   // this.backGroundPanelTrend1.Controls.Add(c);
                }
                this.backGroundPanelTrend1.Controls.AddRange(controls);
    

    祝您,
    一切顺利! 


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年10月12日 6:58
    版主
  • 感谢Yoyo的热心帮助!
    www.shshunfu.com
    2011年10月17日 2:26
  • 不客气!

    祝您,今天愉快!


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    2011年10月17日 2:28
    版主