6

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);
}
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
Thomas
  • 2,575
  • 9
  • 31
  • 41
  • 4
    `OnPain` is not going to compile, unless this is actually a subclass of a class that supports being physically tortured ;) – Thomas Jul 07 '10 at 14:00
  • 2
    Aw @Thomas, you could have just edited that. – Lazarus Jul 07 '10 at 14:01
  • 1
    Nah. I'm far too witty for that. (Also, I was recently reprimanded for editing an obvious copy/paste mistake in someone's code, but that wasn't the reason I refrained from editing here.) – Thomas Jul 07 '10 at 14:13
  • It just left me wondering... OnPain of what? OnPain of execution! – Lazarus Jul 07 '10 at 14:14
  • overriding OnPaint you can comment base.OnPaint for performace reasons. But take care cause you will not recieve OnPaint events anymore. – serhio Jul 07 '10 at 14:50

2 Answers2

5

It is recommended that controls override the On... methods rather than subscribe to their own events.

You should call base.OnPaint to ensure the Paint method is fired properly.

From MSDN:

The OnPaint method also enables derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors
When overriding OnPaint in a derived class, be sure to call the base class's OnPaint method so that registered delegates receive the event.

Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
  • usually, overriding OnPaint you don't need to fire Paint events anymore, so base.OnPaint is not mandatory. MSDN tells, but not everything is to follow exactly. In a similar way you override OnBackgroundPaint and comment base.OnBackgroundPaint do NOT base repaint the background - against flickering. – serhio Jul 07 '10 at 14:59
  • @serhio: The way to prevent background painting isn't to override OnBackgroundPaint (that's the old school brute force method carried over from Win32). Instead, you should set the Control style to include `AllPaintingInWmPaint`. – Jeff Yates Jul 07 '10 at 15:10
  • From MSDN: `When overriding OnPaintBackground in a derived class it is not necessary to call the base class's OnPaintBackground.` http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpaintbackground.aspx – serhio Jul 07 '10 at 15:24
  • @serhio: Yup, that's right, but the correct way to prevent flicker only in .NET is the control style. If you want to actually paint a new background, you would do what you describe. Of course, that is all moot for this question as the question regards OnPaint. – Jeff Yates Jul 07 '10 at 15:34
5

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 overriding OnPaint but doesn't really motivate this.

You need to call base.OnPaint because this method will invoke handlers attached to the Paint event.

Thomas
  • 174,939
  • 50
  • 355
  • 478