C#.NET ..在Paint事件处理程序外部绘制是一种不好的做法吗? [英] C#.NET .. is it a bad practice to draw outside the Paint event handler?

查看:63
本文介绍了C#.NET ..在Paint事件处理程序外部绘制是一种不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

清除我的问题的意思..当我使用.NET Framework在C#中制作游戏时,习惯了实现自己的DrawScene()方法并在我想重绘游戏图形时调用它(基本上是在游戏中的任何实例都已移动或更改了其形状/状态),如下所示:

To clearup what I meant by my question.. I got used when making a game in C# using the .NET framework, to implement my own DrawScene() method and call it whenever I want to redraw the game's graphics (basically after any instance in the game has moved or changed its shape/state), like the following:

private void DrawScene()
{
    Graphics g = this.CreateGraphics();
    g.Clear(Color.Black);

    g.DrawImage(myBitmap, 0, 0);

    g.Dispose();
}

这样做,在我的Paint事件处理程序中,我要做的就是以下事情:

And by doing so, in my Paint event handler, all I do is the following:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    DrawScene();
}

例如,同样,当我想在播放器进行一些移动后重画时,我只需以相同的方式调用DrawScene()即可:

and for example, also when I want to redraw after the player makes some move, I simply call DrawScene() the same way:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Keycode == Keys.Up)
    {
        hero.y -= 5;
    }

    DrawScene();
}

这样做或这样做之间有什么严重的区别(我注意到大多数人都遵循):

Is there any serious difference between doing that or doing it this way (Which I notice most people follow):

private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.Clear(Color.Black);

    e.Graphics.DrawImage(myBitmap, 0, 0);
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Keycode == Keys.Up)
    {
        hero.y -= 5;
    }

    this.Invalidate();
}

很抱歉,如果说得太多,但是我只是想弄清楚我的问题.

Sorry if this was much talking, but I just wanted to clear things up about my question..

因此,如果上述两种情况的绘制方式完全相同(从Graphics的角度来看),并且与第一种情况一样,对我的常规实现没有危害.那么什么时候最好使用invalidate()方法?

So, if the two cases above does the drawing exactly the same way (From the Graphics viewpoint), and there's no harm keeping on my usual implementation as in the first case.. Then when is it best to use the invalidate() method?

推荐答案

这两种方法存在严重差异(首选第二种方法).

This two approaches differs seriously (second one is preferred).

首先-在您的方法中,每次需要绘画时,您都会创建一个新的 Graphics 对象,而在 Paint 事件处理程序中,您使用的是相同的图形每次都反对.

First of all - in your approach you are creating new Graphics object each time you need to paint something, while in Paint event handler you're using the same graphics object each time.

这会增加内存使用量(因为重绘可能会非常频繁地发生),并且由于您不处理正在创建的图形(基本上,您应该对不再使用实现 IDisposable 的任何对象执行此操作)需要使用)-图形所占用的系统资源没有被释放.

This increases memory usage (since redraws can occurs very frequently), and also since you're not disposing graphics you're creating (basically you should do it with any object implementing IDisposable you no longer need to use) - system resources used by graphics not being freed.

此外,可以不通过调用手动重绘表单,但在某些其他情况下(通过其他窗口重叠,显示/隐藏等)可以重绘表单.如果您的绘画将在 Paint 处理程序中完成-那么它们将自动完成,但是在您的第一种"方法中,乳清直到您手动调用绘制方法才可以完成.

Also, your form can be redrawed not manually by your call, but in some other cases (overlapping by other window, show/hide and so on). If your paintings will be done in Paint handler - then they will be done automatically, but in your "first" approach whey will not until you manually call your drawing method.

因此,基本上,最好在 Paint 事件处理程序中绘制所有内容(并调用 Invalidate 甚至是 Refresh 强制重绘),而不是手动使用单独的方法.

So basically it is always better to paint everything in Paint event handlers (and call Invalidate or even Refresh to force redrawing) and not manually in separate methods.

有时候(如果您的图形需要很多代码并且可以在逻辑上拆分为多个部分),则最好像这样处理它:

Sometimes (if your drawing takes much code and can be logically splitted into parts) it can be better to handle it like:

private void DrawScene(Graphics g)
{
    g.Clear(Color.Black);
    g.DrawImage(myBitmap, 0, 0);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
    DrawScene(e.Graphics);
    DrawSomething(e.Graphics);
}

因此,您仍在使用Paint事件处理程序中的grahics对象,只是将其传递到绘图方法中.

So you still using grahics object from Paint event handler, but just passing it into drawing methods.

这篇关于C#.NET ..在Paint事件处理程序外部绘制是一种不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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