.NET C#Winforms应用程序中的慢速屏幕绘制 [英] Slow screen drawing in .Net C# winforms application

查看:103
本文介绍了.NET C#Winforms应用程序中的慢速屏幕绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a very large C# .net 2.0 winforms application which has some drawing issues.

当使用不同的表单时,您可以看到控件被绘制,甚至标题

When going into different forms you can see controls being drawn and even the title bar of the form being resized and then disappearing.

所有其他表单继承的基本表单在其构造函数中具有以下代码。

The base form that all other forms inherit from has the following code in its constructor. Including setting DoubleBuffering to true.

this.SetStyle(ControlStyles.UserPaint, true);

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

this.SetStyle(ControlStyles.ResizeRedraw, true);

this.UpdateStyles();

表单也具有backgroundgradient,但删除此设置不会影响速度。

The forms also have a backgroundgradient, but removing this makes no difference to the speed.

所有控件和表单均从基本版本继承。

All controls and forms inherit from base versions.

我可以添加或检查哪些内容以帮助提高绘图速度?

What can i add or check to help with the drawing speed?

OnPaint中的代码

Code within the 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中绘制 的所有内容看上去绘制起来都更快,但是除此之外,这实际上会使您的应用程序变慢。尤其是如果您打开了许多表单,因为每个表单都会创建一个巨大的位图,该位图用于双缓冲。

I'd say that your "optimizations" is aimed att fooling the eye to believe that everthing you draw by yourself in OnPaint will appear to draw faster, but apart from that, it will really slow your application down. Especially if you have a lot of forms open, since each one of the form will have created a huge bitmap which is used for double-buffering.

所以:依次执行提高速度:删除上面的所有代码,然后仅在绝对需要的地方插入。

So: in order go gain speed: remove all of your code above and insert it only where absolutely needed.

编辑:在我看到您的OnPaint代码后:删除所有优化,然后将您的OnPaint代码移至OnPaintBackground。确保您调用base.OnPaintBackground。扔掉Susped / Resume布局。

EDIT: After I saw your OnPaint-code: remove ALL "optimizations" AND move your OnPaint code to OnPaintBackground instead. Make sure that you do not call base.OnPaintBackground. Throw away Susped/Resume layout.

所有,您真正需要做的就是使用OnPaintBackground而不是OnPaint,其余的将处理本身。

All that you really need to do is to use OnPaintBackground instead of OnPaint and the rest will take care of itself.

这篇关于.NET C#Winforms应用程序中的慢速屏幕绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆