Re: Control Painting
Chris Anderson <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.winforms |
|---|---|
| Message-ID | <[email protected]> |
> 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
> }
Yup that's pretty much it, thanks Fabian
> // this will explicitly cause the control to refresh itself and your
> figure to be drawn
> public void CauseFigureToBeDrawnOnThisControl()
> {
> this.Invalidate();
> }
Personally I would call that method public void Refresh() to fit into
the way controls work, or public void DrawFigure() so you don't need to
refactor any client code, but that's just semantics and personal
preference
> 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);
> }
LOL - spot on
I really should read entire threads before I reply with the same info :D
Chris