Tho*_*mas 5 c# onpaint winforms
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);
}
Run Code Online (Sandbox Code Playgroud)
这并不重要;两者都工作。OnPaint
理论上,覆盖可能会稍微快一点,但这并不是任何人都会注意到的区别。Microsoft 建议覆盖,OnPaint
但并没有真正推动这一点。
您需要调用,base.OnPaint
因为此方法将调用附加到Paint
事件的处理程序。