【问题标题】:Slow screen drawing in .Net C# winforms application.Net C# winforms 应用程序中的慢速屏幕绘制
【发布时间】:2010-12-17 00:48:24
【问题描述】:

我有一个非常大的 C# .net 2.0 winforms 应用程序,它有一些绘图问题。

当进入不同的表单时,您可以看到控件被绘制,甚至表单的标题栏被调整大小然后消失。

所有其他表单继承自的基本表单在其构造函数中具有以下代码。包括将 DoubleBuffering 设置为 true。

this.SetStyle(ControlStyles.UserPaint, true);

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

this.SetStyle(ControlStyles.ResizeRedraw, true);

this.UpdateStyles();

表单也有一个背景渐变,但删除它对速度没有影响。

所有控件和表单都继承自基本版本。

我可以添加或检查什么来帮助提高绘图速度?

OnPaint 中的代码

if (this.b_UseBackgroundGradient)
        {
            Graphics graphics = e.Graphics;

            Rectangle backgroundRectangle = this.ClientRectangle;

            this.SuspendLayout();
            if (backgroundRectangle.Width != 0 && backgroundRectangle.Height != 0) 
            {

                using (Brush backgroundBrush = new LinearGradientBrush(backgroundRectangle, base.BackColor, this.BackGradiantColour, LinearGradientMode.ForwardDiagonal))
                {
                    graphics.FillRectangle(backgroundBrush, backgroundRectangle);
                }
            }
            this.ResumeLayout();
        }
        else
        {
            base.OnPaint(e);
        }

【问题讨论】:

  • 你在 OnPaint 方法内部做些什么吗?
  • 在此更改后尝试在远程桌面上使用该表单。

标签: c# winforms drawing performance


【解决方案1】:

我会说你的“优化”旨在欺骗眼睛相信你在 OnPaint 中自己绘制的所有东西看起来会更快,但除此之外,它会真的很慢你的应用程序下来。特别是如果您打开了很多表单,因为每个表单都会创建一个巨大的位图,用于双缓冲。

所以:为了加快速度:删除上面的所有代码,只在绝对需要的地方插入。

编辑:在我看到您的 OnPaint 代码后:删除所有“优化”并将您的 OnPaint 代码移至 OnPaintBackground。确保您调用 base.OnPaintBackground。丢弃 Susped/Resume 布局。

所有您真正需要做的就是使用 OnPaintBackground 而不是 OnPaint,其余的会自行处理。

【讨论】:

    【解决方案2】:

    Google 搜索 SuspendLayout/ResumeLayout。我还想看看在构造函数中包含 stylign 信息是否正确,或者是否有更好的方法在构造函数之前调用。

    【讨论】:

      猜你喜欢
      • 2011-05-12
      • 1970-01-01
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 2014-09-09
      • 2020-12-05
      相关资源
      最近更新 更多