Re: redrawing in control with Invalidate() by Mark
Mark
Wed Feb 23 23:05:49 CST 2005
revyakin@yahoo.com wrote:
> I have a CStatic which displays a plot - X, Y axes, tickmarks, etc. I
> want to change scaling of the plot by entering new ranges of X and Y. I
> call an overriden OnPaint for that:
>
> CPlot::OnPaint() {
> this->Invalidate()
> CPaintDC dc(this);
> ... draw lines and labeles
> DeleteDC(dc);
> }
>
> The problem is that it gets redrawn on top of the prevous drawing, w/o
> first erasing the prevous drawing. I also tried InvalidateRec() w/o
> success.
You are somewhat confused here. Invalidating a region of your window
directs Windows to send a WM_PAINT message to the window to cause the
invalidated area to be redrawn. Invalidating a region within the
WM_PAINT message will cause a WM_PAINT message to be sent... not very
helpful. So don't do that.
Secondly, CPaintDC wraps a BeginPaint/EndPaint call. You should not
delete the DC returned by BeginPaint -- EndPaint does this for you. As
CPaintDC wraps both BeginPaint and EndPaint calls (in the c'tor and
d'tor respectively), then you should call neither function -- just
construct the paint DC.
Finally, as to your desired problem -- your window class does not have a
background brush set. If you do have one set, BeginPaint will erase the
contents of the invalidated area using the background brush. If not,
you are responsible for performing this action, if desired consider:
- the case of blatting a (cached) bitmap to the screen -- there you
don't need to erase the background).
- the case where your background colour is user selectable on a per
object basis. There, you want to erase the background yourself to the
correct colour, which will depend on per window and not per class state.
It isn't lame to erase the background yourself under the second
circumstance. In fact, it's not likely to be much more efficient to
have BeginPaint do it for you -- under the covers, this will likely just
call FillRect or similar anyway.
Finally note that the PAINTSTRUCT has an fErase flag which tells you if
you are responsible for erasing the background, and an rcPaint with the
afflicted region, if you want to know what to erase and whether to erase it.