You are doing wrong thing. The graphics rendered this way is not preserved anywhere.
You need to render graphics every time the Windows message
WM_PAINT
is sent. This is done in the handler of the event
Paint
or, better yet, in the overridden virtual method
OnPaint
of any
Control
you want to render your graphics in, including
Form
. You should not create the instance of the class
Graphic
, instead, you should get one passed to you through the event arguments parameter.
When some part of your control is invalidated, your rendering method will be called again. It will happen if a part of the view if covered by some other window and later gets exposed again. You should keep all the data needed for rendering. The dynamic behavior of the graphics is achieved through change in this data followed by invalidation of the control or a part of it using one of the
Invalidate
methods.
Please see also my past answers to some related questions:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[
^],
Drawing Lines between mdi child forms[
^],
capture the drawing on a panel[
^].
—SA