的createGraphics()方法和Paint事件参数 [英] CreateGraphics() Method and Paint Event Args

查看:245
本文介绍了的createGraphics()方法和Paint事件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过的地方,的createGraphics()将做到这一点步骤我们:

I have read somewhere that CreateGraphics() will do this steps for us :


  1. BeginPaint的

  2. 绘图

  3. 调用EndPaint

我有这样的代码:

private void Form1_Load(object sender, EventArgs e)
{
    grFrom = this.CreateGraphics();
    grFrom.FillRectangle(Brushes.Red, this.ClientRectangle);
}

有没有红色长方形......可是,当我复制下面的行 Form1_paint ,每一件事情正常运行。

There is no red rectangle...but, When I copy line below in Form1_paint, every thing runs correctly.

grFrom.FillRectangle(Brushes.Red, this.ClientRectangle);



所以,问题就在这里:
是什么 e.Graphics Form1_paint

的createGraphics e.Graphics

推荐答案

两件事情:


  1. 的createGraphics 为您提供了一个图形对象,你总是应该的Dispose()退出之前。你应该把你的语句使用块内。

  2. 您绘制图形才有效,直到被形式重新绘制。在你的情况,通过的Form_Load调用这个,这是发生在第一渲染之前,并获得扔掉。你应该总是为了把这个OnPaint()方法将它老大难在屏幕上,因为这将导致其获得重绘时重绘控件。

  1. CreateGraphics gives you a graphics object that you should always Dispose() prior to exiting. You should put your statement inside of a using block.
  2. The graphics you draw are only valid until the form gets repainted. In your case, by calling this in Form_Load, it's happening prior to the first render, and getting "thrown away". You should always put this in OnPaint() in order to have it "persistent" on the screen, as that will cause it to get redrawn when the control is redrawn.

这篇关于的createGraphics()方法和Paint事件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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