Re: Control Painting

Chris Anderson <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
> 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 ( );
>         }

The problem there is that the PaintEventArgs argument of the OnPaint
method holds a reference to the Graphics object you need to paint to,
but instead you are creating a new one in DrawFigure.

Basically you need to move all the code in DrawFigure into the OnPaint
event, and to change the 

        Graphics g = Graphics.FromHwnd ( _canvas.Handle );
To
        Graphics g = e.Graphics;

Also, you should remove the call to g.Dispose (it's been passed to you
so you are not responsible for clear up)

Then the public DrawFigure should just contain the invalidation code.



> So I can't just invalidate the canvas in the DrawFigure() method -
> nothing will be drawn. However, you're note to Steve Savage on VISBASL
> might be useful for my understanding.
> 
> The 'OnPaint()' event fires whenever the form (control, etc.) needs to
> have its graphic area refreshed.  So I need to do all my drawing of
> persistent graphics in this event handler, correct?


Yup !!
And I wish I read all your email before I wrote the above :)


> Also, If my class is hooked into and handling this event, when does
the
> hosting control (the panel or the ultraGroupBox) do the rest of its
> painting (after or before mine)?  It seems that on the initial panel

If the order is important, I would subclass the control instead (if you
can)
Then you have full control over when you call the base method (i.e.
before, after or even in the middle of your painting code)

In the case of events, it depends on when the control decides to call
the paint event handlers
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.