GDI and Events

  • Thread starter Thread starter Luke Ward
  • Start date Start date
L

Luke Ward

Hi

I'm trying to learn a bit about c# GDI and the paint event. I want to
try writing a program to move a GDI square or circle around a form with
the keyboard. I've i had a go but i run into recursion issues on the
paint event while I am trying to store/restore the background to stop
the square/circle leaving a trail.

Any help / code samples appreciated.

Thanks

Luke
 
Luke said:
Hi

I'm trying to learn a bit about c# GDI and the paint event. I want to
try writing a program to move a GDI square or circle around a form with
the keyboard. I've i had a go but i run into recursion issues on the
paint event while I am trying to store/restore the background to stop
the square/circle leaving a trail.

Any help / code samples appreciated.

Where's your code? It sounds like you are making a recursive method
call in your Paint handler, which of course would be very bad. But if
you don't post a concise-but-complete code example, it's not possible to
know for sure.

As far as general advice goes, the basic strategy for drawing in Forms
applications is:

– Handle the Paint event, either by subscribing to the event itself,
or (if the drawing code is in the Control subclass itself) overriding
the OnPaint() method.

– In the Paint event handler (actual handler or the override), draw
the _current_ state from scratch.

– When the state changes, use the Control.Invalidate() method to
indicate to Windows the area that needs to be redrawn. At some point in
time after the call to Invalidate(), the area to be redrawn will be
erased and your Paint event handler called to redraw it.

When you want user input to affect drawing, you should use the user
input to update whatever state affects what you'll draw, then call
Invalidate() and return without drawing anything. Let Windows call your
code to redraw, and do the redrawing then.

Pete
 
Back
Top