C# WinForms - Paint 方法问题
我不确定使用图形的最佳方法是什么 - 我应该将我的类附加到主窗体 Paint 事件然后进行绘图,还是最好像这样从覆盖的 OnPaint void 中调用它?我的意思是,这样做可以吗:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e) //what is this good for? My app works without it as well
Graphics g=e.Graphics;
DrawEnemies(g);
UpdateHUD(g);
DrawSelectedUnit(g);
}
I am not sure what is the best way of using graphics - should I attach my classes to main form Paint event and then do the drawing, or it is better to call it from overidden OnPaint void like this? I mean, is it OK to do that like this:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e) //what is this good for? My app works without it as well
Graphics g=e.Graphics;
DrawEnemies(g);
UpdateHUD(g);
DrawSelectedUnit(g);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
建议控件重写
On...
方法,而不是订阅自己的事件。您应该调用
base.OnPaint
以确保正确触发Paint
方法。来自 MSDN:
It is recommended that controls override the
On...
methods rather than subscribe to their own events.You should call
base.OnPaint
to ensure thePaint
method is fired properly.From MSDN:
这其实并不重要;两者都有效。理论上,重写
OnPaint
可能会稍微快一点,但这并不是任何人都会注意到的差异。微软建议重写OnPaint
,但并没有真正推动这一点。您需要调用
base.OnPaint
,因为此方法将调用附加到Paint
事件的处理程序。It doesn't really matter; both work. Overriding
OnPaint
might be ever so slightly faster in theory, but it's not a difference that anyone will notice. Microsoft recommends overridingOnPaint
but doesn't really motivate this.You need to call
base.OnPaint
because this method will invoke handlers attached to thePaint
event.