CreateGraphics() 方法和绘制事件参数
我在某处读到 CreateGraphics()
将为我们执行以下步骤:
- BeginPaint
- Drawing
- 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 :
- BeginPaint
- Drawing
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两件事:
Dispose()
的图形对象。您应该将语句放在 using 块内。Two things:
Dispose()
prior to exiting. You should put your statement inside of a using block.您的表单加载调用正在绘制表单,但随后第一个常规表单绘制事件会覆盖它,因此您永远看不到它。 (因为这发生在您提交表格之前)
我相当确定它们是等效的,您需要的是更好地理解 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)
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
您正在创建一个新的图形对象,它很可能由内存缓冲区支持。从
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.