我可以在Paint事件以外的任何其他事件中使用Graphics对象吗? [英] Can I use Graphics object in any other event than Paint event?
问题描述
我有一个带有固定面板的WinForm.我否决了Panel的Paint事件.我有一行可以设置_graphics对象:
I have a WinForm with a docked Panel. I overrode the Panel’s Paint event. I have a line that sets up _graphics object:
private Graphics _graphics;
在覆盖中,我初始化_graphics对象:
In the override I initialize _graphics object:
private void GridPanel_Paint(object sender, PaintEventArgs e)
{
_graphics = e.Graphics;
<snip>
…
</snip>
}
这是愚蠢的部分,我可以在其他任何事件(例如MouseMove)中使用此_graphics对象吗?
Here comes the stupid part, Can I use this _graphics object in any other event like MouseMove?
推荐答案
这取决于您使用"的意思.
It depends what do you mean by "use".
图形
是一次性的.重新绘制后,控件处置 Graphics
实例,该实例已传递到 Paint
事件处理程序中.从这一点来看,被丢弃的物体是没有用的.但是缓存对该实例的引用是绝对合法的.
Graphics
is disposable. After repainting, control disposes Graphics
instance, that was passed into Paint
event handler. From that point, disposed object is useless. But caching a reference to that instance is absolutely legal.
这篇关于我可以在Paint事件以外的任何其他事件中使用Graphics对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!