CreateGraphics() 方法和绘制事件参数

发布于 2024-10-22 23:03:39 字数 611 浏览 2 评论 0原文

我在某处读到 CreateGraphics() 将为我们执行以下步骤:

  1. BeginPaint
  2. Drawing
  3. EndPaint

我的代码如下:

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

没有红色矩形...但是,当我在 中复制下面的行时>Form1_paint,一切运行正常。

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

所以问题在这里: Form1_paint 中的 e.Graphics 是什么?

CreateGraphics 还是e.Graphics

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

  1. BeginPaint
  2. Drawing
  3. EndPaint

I have my code like this :

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

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

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

So Question is Here:
What is the e.Graphics in Form1_paint?

CreateGraphics or e.Graphics?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

骄傲 2024-10-29 23:03:39

有两件事:

  1. CreateGraphics 为您提供退出之前应始终使用 Dispose() 的图形对象。您应该将语句放在 using 块内。
  2. 您绘制的图形仅在表单重新绘制之前有效。在您的情况下,通过在 Form_Load 中调用此函数,它会在第一次渲染之前发生,并被“丢弃”。您应该始终将其放在 OnPaint() 中,以便使其“持久”在屏幕上,因为这将导致在重绘控件时重绘它。

Two things:

  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.
不打扰别人 2024-10-29 23:03:39

您的表单加载调用正在绘制表单,但随后第一个常规表单绘制事件会覆盖它,因此您永远看不到它。 (因为这发生在您提交表格之前)

所以问题来了:什么是

e.form1_paint 中的图形?

CreateGraphics 还是 e.Graphics?

我相当确定它们是等效的,您需要的是更好地理解 Windows 窗体事件生命周期。

这个答案有相关链接:
WinForms 事件生命周期

Your form load call is drawing to the form, but then the first regular form paint event writes over it, so you never see it. (As this happens before your presented the form at all)

So Question is Here : What is the

e.Graphics in form1_paint ?

CreateGraphics or e.Graphics ?

I'm fairly sure the are equivilent, what you need is a better understanding of the windows forms event lifecycle.

This answer has relevant links:
WinForms event life cycle

风透绣罗衣 2024-10-29 23:03:39

您正在创建一个新的图形对象,它很可能由内存缓冲区支持。从 e.Graphics 获得的 Graphics 对象由一个缓冲区支持,该缓冲区代表当前窗口的屏幕区域(窗口句柄中的窗口,而不是带有标题栏的窗口等)。

您始终可以将创建的图形对象中的数据位块传输到 e.Graphics 中的数据。

我相信有人会比我详细阐述更多。

You are creating a new graphics object, which is most likely backed by a memory buffer. The Graphics objects you get from e.Graphics is backed by a buffer which represents the screen area by the current window (window as in Window Handle, not a window with title bar, etc).

You can always bit blit the data from a created graphics object onto the one from e.Graphics.

I am sure someone will elaborate much more than I have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文