Is it necessary to invalidate update region before painting in the window

894 Views Asked by At

Is it necessary to invalidate the update region before immediate painting (in response to a user action) if I use Direct2D? Or is calling RenderTarget::BeginDraw() enough? I need to quickly repaint some part of the window outside of the WM_PAINT message. If I don't invalidate the update region, sometimes the whole client area of the window becomes black, no drawings are visible. But if I do invalidate the update region, the system sends a WM_PAINT message before I validate the update region back, which causes unnecessary drawing operations to be performed. How should I implement the immediate drawing operations outside the WM_PAINT message handler if I use Direct2D?

2

There are 2 best solutions below

0
On BEST ANSWER

Invalidating and validating are ways to get a WM_PAINT message and to handle one. If you're painting outside of a WM_PAINT handler, you shouldn't be invalidating or validating as part of that drawing.

That said, it's very uncommon to paint outside of the WM_PAINT handler. It can be very hard to get it right. My advice would be to get everything working via a traditional WM_PAINT handler first, and then decide whether it's really necessary to do some painting outside that handler.

3
On

Typically when using Direct2D in a game-like application you perform no drawing in WM_PAINT and draw using Direct2D many times per second. Another common technique, for cases where you draw something once and do not create a new drawing many times per second is storing the drawing in a back buffer of some sort; that way you can 'blit' it to the screen in response to a WM_PAINT message.

Of course, this could be different for your case, depending on the type of program you are creating.