Search code examples
c++winapidirect2dwm-paint

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


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?


Solution

  • 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.