PDA

Click to See Complete Forum and Search --> : Repaint without flickering


vbcode1980
May 31st, 2007, 01:55 AM
Hi,

I have a form with a background image, and I use System.Drawing.Graphics.DrawString() to draw text on the form.

That works fine.
Now all I need is a way to refresh the screen when the text has been changed.
I've tried this.Refresh() and this.Invalidate() but they produce a flicker, because the entire screen is repainted.

is there a way to clear and repaint only the text, without having to repaint the background image as well?

jmcilhinney
May 31st, 2007, 02:20 AM
In actual fact, Invalidate does NOT force the form to repaint. All it does is invalidate part of the form so that it will be repainted next time a repaint is done. To ensure that any repainting is done immediately after calling Invalidate you must call Update. Here's what the Refresh method looks like:public virtual void Refresh()
{
this.Invalidate(true);
this.Update();
}

You have two choices to speed things up and both involve calling Invalidate followed by Update.

When you call Refresh it calls Invalidate and passes True, which forces all child controls to repaint too. You can call Invalidate explicitly and pass False, so only the form and not any child controls will be repainted.

Even better would be if you calculated the smallest Rectangle or Region that contained your text and then passed that as a parameter to Invalidate. That way only the area containing the text will be invalidated so only that area will be repainted when you call Update.

vbcode1980
May 31st, 2007, 02:25 AM
Cheers! :)
I'll try that when I get home.