OnPaint automatically call e.Graphics.Clear()?

G

gilbert@gmail

Hello.

I have a question about OnPaint override.

For example, if i have the following code in my inherited form class,

private Brush _brush = Brushes.Black;
private RectangleF _rect = new RectangleF(0,0,100,100);

private int counter =0;
protected override void OnPaint(PaintEventArgs e){
if (counter==0){
counter++;
e.Graphics.Clear(Color.White);
e.Graphics.DrawString("Test", this.Font, _brush, _rect);
}
}

I expect the OnPaint method will do nothing after the first call. That
means, the string "Test" should stay on screen if the form is not
resized/moved/covered by other windows. However, in
reality, the OnPaint do the e.Graphics.Clear() automatically.

I'd like to know if that is possible for me to stop that behavior. In
my application, my form contains many individual text strings. The
strings are updated periodically individually. I would like to render
the updated strings only, not rendering the whole thing for every
string update. Thank you very much for your help!

Gilbert
 
J

Jesse Houwing

* gilbert@gmail wrote, On 23-4-2007 18:39:
Hello.

I have a question about OnPaint override.

For example, if i have the following code in my inherited form class,

private Brush _brush = Brushes.Black;
private RectangleF _rect = new RectangleF(0,0,100,100);

private int counter =0;
protected override void OnPaint(PaintEventArgs e){
if (counter==0){
counter++;
e.Graphics.Clear(Color.White);
e.Graphics.DrawString("Test", this.Font, _brush, _rect);
}
}

I expect the OnPaint method will do nothing after the first call. That
means, the string "Test" should stay on screen if the form is not
resized/moved/covered by other windows. However, in
reality, the OnPaint do the e.Graphics.Clear() automatically.

I'd like to know if that is possible for me to stop that behavior. In
my application, my form contains many individual text strings. The
strings are updated periodically individually. I would like to render
the updated strings only, not rendering the whole thing for every
string update. Thank you very much for your help!

Gilbert

You can Dubblebuffer the original graphics in a bitmap image and draw
that on the graphics surface at the beginning of your paint method.

Jesse
 
G

gilbert@gmail

thank you very much for your suggestion. But, would that be slow to
draw a image every time onPaint is called? i tried the method but the
screen flickers..

what I have done is creating a Bitmap and draw graphic to the bitmap.
OnPaint simply calls DrawImage. However, the screen flickers while
updates..

Thanks.

Gilbert
 
M

Michael C

gilbert@gmail said:
thank you very much for your suggestion. But, would that be slow to
draw a image every time onPaint is called? i tried the method but the
screen flickers..

what I have done is creating a Bitmap and draw graphic to the bitmap.
OnPaint simply calls DrawImage. However, the screen flickers while
updates..

In answer to your original question, OnPaint does NOT call graphics.clear. I
have done many times exactly what you are trying to do, which is update only
what has changed. Something else must be causing you form to be cleared. I
think there is a param for this.Invalidate that causes the surface to be
cleared though, maybe this is set?

With regards to painting to a bitmap and then the form, this will be more
than fast enough if the bitmap is 32bitPARGB. However, I would consider this
a last resort. At first I always try to paint direct to the form without
double buffering. If that isn't possible (which is increasingly the case
these days with anti-aliasing etc) then I will use double buffering. If that
still doesn't work then in rare cases I will use a bitmap. The bitmap should
only be used in cases where drawing is complicated and takes a long time and
isn't updated that often.

Michael
 
M

Mehdi

I expect the OnPaint method will do nothing after the first call. That
means, the string "Test" should stay on screen if the form is not
resized/moved/covered by other windows. However, in
reality, the OnPaint do the e.Graphics.Clear() automatically.

Nope. OnPaint doesn't do any drawing by default (except in particular
cases). You can verify that by looking at the code for the Form.OnPaint()
method using Reflector. You code does not even call the base OnPaint()
method so OnPaint couldn't possibly do anything else than what you are
doing in your code. I would by the way recommend that you call
base.OnPaint() in your override to ensure that the Paint event is raised
appropriately.

What does clear your Form's content is the OnPaintBackground() method which
is called before OnPaint() whenever the Form needs to be redrawn.
I'd like to know if that is possible for me to stop that behavior. In
my application, my form contains many individual text strings. The
strings are updated periodically individually. I would like to render
the updated strings only, not rendering the whole thing for every
string update. Thank you very much for your help!

Either override OnPaintBackground() and do nothing in it or set the
AllPaintingInWmPaint flag on your Form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top