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?

- 45,555
- 16
- 123
- 175

- 3
- 2
2 Answers
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.

- 45,555
- 16
- 123
- 175
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.

- 5,324
- 2
- 28
- 53
-
I know this question is related to many years ago, but I have a question, Does Direct2D call WM_PAINT multiple times in one second? Because without using BeginPaint and EndPaint in GDI WM_PAINT is called repeatedly. Is this what Direct2D does to animate? – Jul 11 '23 at 10:01
-
@TheParhamDev Direct2D will not call WM_PAINT, it's the regular Windows window manager that does this. If you're getting a lot of WM_PAINT messages then there's something else going on. But I get the feeling your question isn't really about Direct2D and rather regular GDI. If you're using that, you should probably use BeginPain and EndPaint. – MicroVirus Jul 12 '23 at 08:05
-
I meant why Direct2D doesn't handle BeginPaint and EndPaint for painting. I know I can paint outside WM_PAINT and the os sends the WM_PAINT. So whats the best way to paint? in a message loop that handles the game loop? – Jul 12 '23 at 09:32