Re: How to use InvalidateRect
Mkennedy1102 wrote:
"Mkennedy1102" wrote:
I want to redraw only a portion of my View, for example I have a CRect in the
lower right corner where my app draws some text, I want to redraw that rect
when the info changes, so if that CRect is named rect, I call
InvalidateRect(&rect) (the berase parameter is moot in this case). However
this does nothing. If I call Invalidate() to repaint the screen, it works
fine, but I want to reduce my drawing overhead. Where am I going wrong with
InvalidateRect? According to MSDN :
"The invalidated areas accumulate in the update region until the region is
processed when the next WM_PAINT message occurs or until the region is
validated by using the ValidateRect or ValidateRgn function."
How do I make that happen? I have tried PostMessage(WM_PAINT), I have tried
calling OnPaint from within my code. I must be missing something here. Much
thanks for any help.
Update:
After some testing, I realize that whats happening is this;
OnPaint draws text into the CRect;
text changes;
InvalidateRect is called;
text is redrawn into rect, however the old text is not erased entirely, only
what is covered by the new text, mind you, its not being drawn on top of the
old text its more like it's repacing it. I need some way to completely erase
what was there before I redraw.
The default OnEraseBkgnd should erase the invalid area. If you have
overridden it then you have to erase things yourself (by drawing a
rectangle of the desired background color before painting the content).
When you use InvalidateRect to optimize painting, the trick you seem to
be missing is that your OnDraw function can call pDC->GetClipBox to find
out what needs repainting. Then paint only what is required by the
GetClipBox rect.
Absolutely, positively, do not call OnDraw, OnPaint or post WM_PAINT
yourself. Calling Invalidate or InvalidateRect is the only correct way
to initiate a repaint.
--
Scott McPhillips [MVP VC++]