Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Invalidate() vs InvalidateRect()

476 views
Skip to first unread message

Abhijit Patait

unread,
Mar 11, 2004, 7:40:55 PM3/11/04
to
What is the difference and advantages/disadvantages of using
Invalidate() or InvalidateRect()?

In my view, I have a few text lines (drawn via CDC::DrawText) which
need to be updated in response to some continuous action. I tried the
following two approaches.

1. Call Invalidate(). This forces the view to be repainted and the
text updates as desired. But the display flickers (may be excessive
drawing).

2. Call InvalidateRect(rect) with rect containing the coordinates of
the rectanble encompassing the text. This pretty much does the same
thing as #1 above (including flicker).

In short, I don't see reason why one would use InvalidateRect(). Is my
method (calling Invalidate()) the right way to update text in a view?

Thanks in advance.
Abhijit

Joseph M. Newcomer

unread,
Mar 11, 2004, 8:13:54 PM3/11/04
to
Invalidate() == InvalidateRect(NULL);

InvalidateRect() invalidates only the rectangle which needs to be redrawn. If you are
getting overall flicker, you are inadvertently invalidating more than you need to.

Invalidate() works fine except when the flicker becomes noticeable. Which is the problem
you're having.

To double-check this, get the clipping rectangle. If it is the entire client rectangle,
you know you somehow managed to invalidate too much.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Scott McPhillips [MVP]

unread,
Mar 11, 2004, 11:58:09 PM3/11/04
to

Invalidate() causes the entire view to be erased and redrawn. The erase
is often visible as a white flash and this is the major cause of flicker.

InvalidateRect() can reduce the area that erases and then redraws, which
might help, but if the erase is visible you still see some flicker. The
primary advantage of using InvalidateRect() is that your OnDraw routine
can omit the drawing of things outside the clipping area (call
GetClipBox to determine where to draw), thus speeding up the drawing.
This is not significant if you are not drawing much, but is a big help
with complex drawing or with small scrolls of a big image.

The real solution to visible flicker is to override OnEraseBkgnd() and
not call the base class (suppressing the visible white flash), then in
OnDraw draw into an offscreen bitmap and blit the image to the view.
This accomplishes both the erase and the paint in a single (and fast)
step. There is a great example of this flicker-elimination technique in
the MFC DrawCLI sample code.

--
Scott McPhillips [VC++ MVP]

0 new messages