I don't understand the OnPaint method


T

Tony Johansson

Hi!

I have some problems to fully understand the OnPaint that I override in my
Form class.
I do understand when I have an event handler for the Paint event.

I mean when I override the OnPaint method this OnPaint is not an event
handler I just override an existing method that exist in a more general
class higher up and say now we use my implementation of OnPaint so windows
will call my OnPaint instead of the other method OnPaint.

I know that my OnPaint work as following when I have a windows with graphics
on and hide it by using for example the notepad and then move the notepad
away so the graphics window is not hidden my OnPaint is called.

From the functionallity of the OnPaint it work as if it was an event handler
for the Paint event but as I mentioned it's not an event handler.

If I don't override the OnPaint method but instead use an event handler for
the Paint event I think it works as this. In the OnPaint that exist above
the Form class in the control class perhaps this method OnPaint raise the
Paint event and every subscriber that subscribe to the Paint event will be
notified and the respective event handler will be called.

So it might be in this way Windows call OnPaint whatever this method is
implemented if I have overridden it Windows call my OnPaint that's it. It's
important to call base.OnPaint in my overrideen OnPaint because if there are
some subscriber to the paint event these must be notified and perhaps
Windows need to do some processing in the base method OnPaint.

So as a summary is this correct understood about the OnPaint method.

//Tony
 
Ad

Advertisements

P

Peter Duniho

Tony said:
[...]
I mean when I override the OnPaint method this OnPaint is not an event
handler I just override an existing method that exist in a more general
class higher up and say now we use my implementation of OnPaint so windows
will call my OnPaint instead of the other method OnPaint.

That is correct. Your class overrides the base class OnPaint() method.
It's just like overriding any other virtual method.
I know that my OnPaint work as following when I have a windows with graphics
on and hide it by using for example the notepad and then move the notepad
away so the graphics window is not hidden my OnPaint is called.

That is also correct. That's how Windows (and practically every other
mainstream GUI API) works.
From the functionallity of the OnPaint it work as if it was an event handler
for the Paint event but as I mentioned it's not an event handler.

What makes you say it works as if it was an event handler? Are you
trying to say that _everything_ that is called as a response to
something happening elsewhere _must_ be an event handler? Because
that's very far from the truth.

An "event handler" is a very specific method. It is one that has been
referenced by a delegate instance that has been added to an _event_ and
is called when the event is raised.

But there are lots of examples of methods that are called for reasons
other than an event being raised. Just because your OnPaint() method is
called to do something when something else happens, that doesn't make it
an "event handler". It can only be an "event handler" if you use it as
the target method for a delegate instance that has been added to an event.
If I don't override the OnPaint method but instead use an event handler for
the Paint event I think it works as this. In the OnPaint that exist above
the Form class in the control class perhaps this method OnPaint raise the
Paint event and every subscriber that subscribe to the Paint event will be
notified and the respective event handler will be called.

Not only does it work like that, you've been told multiple times it
works like that.
So it might be in this way Windows call OnPaint whatever this method is
implemented if I have overridden it Windows call my OnPaint that's it.

Technically, it's not Windows that calls your OnPaint() method. Rather,
Windows sends a WM_PAINT message to your window, which results it in
being dispatched by the message loop to the window's "window procedure".
That window procedure, for a System.Windows.Forms.Form object, happens
to be a method that looks at the window message ID, and if it's
WM_PAINT, then calls the OnPaint() method. Through the magic of
polymorphism, if you've overridden that method, your method gets called
instead.

So, yes…Windows is the part that starts the ball rolling, by sending the
appropriate window message. But it's .NET that calls your method.
It's
important to call base.OnPaint in my overrideen OnPaint because if there are
some subscriber to the paint event these must be notified and perhaps
Windows need to do some processing in the base method OnPaint.

Not Windows, but .NET. And yes, you'll want to call the base method so
that it can do what it normally does, including raising the Paint
method. (Depending on what class you've inherited, the base method may
also do some actual drawing; if you don't call it, then you don't get
that drawing).
So as a summary is this correct understood about the OnPaint method.

It seems to me you understand the basic concept reasonably well. I
don't know why your Subject: for this message thread says that you don't
understand the OnPaint() method. It seems to me that you do.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top