Re: Control Painting

Peter Osucha <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
Yes, that last explanation is good, Fabian.  Thanks.

-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]] On Behalf Of
Fabian Schmied
Sent: Wednesday, May 23, 2007 10:38 AM
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Control Painting

> I have this painting stuff straightened out, I think.  Things seem to
be
> working as I desire.  One final question having nothing to do with
> painting I'll ask in a separate thread.

Even though I'm not Chris and although you've already figured it out,
I'd like to provide an answer just for completeness (e.g. for people
browsing the archives):

> My OnPaint() event handler is what is calling DrawFigure();
>
>     private void OnPaint ( object sender, PaintEventArgs e )
>     {
>         // Draw the wells and well headers and cartridge border.
>         DrawFigure ( );
>     }

I think, Chris meant something like this:

private void OnPaint ( object sender, PaintEventArgs e )
{
    // Draw the wells and well headers and cartridge border.
    DrawFigure (e.Graphics);
}

private void DrawFigure (Graphics g)
{
  // do all the drawing stuff
  d.DrawSomething();
  // do NOT dispose of "g", because we got it from the outside
}

// this will explicitly cause the control to refresh itself and your
figure to be drawn
public void CauseFigureToBeDrawnOnThisControl()
{
  this.Invalidate();
}

On a related note (but not exactly an answer to your problem): there
are actually two different kinds of Paint "things" in the Control
classes - the Paint _event_ and an OnPaint _method_. I'm raising this
topic, because you called your Paint event handler "OnPaint", which is
confusing, and because it partially answers your second question:

> when does the
> hosting control (the panel or the ultraGroupBox) do the rest of its
> painting (after or before mine)?

So, what's the difference between "Paint" and "OnPaint"?

"Paint" is an event meant for code _outside_ of the control class. If
you have a control X, you can simply say "X.Paint += MyPaintHandler;"
to have a method MyPaintHandler to be invoked whenever the control X
is to be painted. In your sample, you called your event handler
"OnPaint", Visual Studio usually calls it "X_Paint" when it generates
one for your.

"OnPaint" is a virtual method meant for code within the control class.
I.e. if you are writing a custom control and need to do special
painting, you usually do not subscribe to your "Paint" event, but
instead override the "OnPaint" method.

The advantage of using the OnPaint method instead of the Paint event
is that you have greater control of when your custom drawing code is
inserted into the drawing chain. E.g. consider the following:

protected override OnPaint (PaintEventArgs e)
{
  e.Graphics.DrawSomething();
  base.OnPaint(e);
}

In this case, your drawing code will be executed before that of the
base class, i.e. the base class will draw on top of what you've drawn.
If you want it the other way around, just have the base call at the
beginning of the method.

You don't have that degree of control with the "Paint" event, for
which the drawing order is an implementation detail which might differ
for every control. On the other hand, you can only use the "OnPaint"
method from inside a control, not from the outside.

Regards,
Fabian
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.