Why call InvalidateRect() in WM_PAINT message?

928 views Asked by At

So i am learning Windows API with book "Programming Windows - Charles Petzold (5th edition)". If i understand correctly, when handling WM_PAINT message calling function BeginPaint() validates given area that has to be updated. However, in this book (page 145 for PDF version or page 128 in printed version) you can see this code:

case WM_PAINT:
          InvalidateRect (hwnd, NULL, TRUE) ;   // what does this line do?
          hdc = BeginPaint (hwnd, &ps) ;
          DrawBezier (hdc, apt) ; 
          EndPaint (hwnd, &ps) ;
          return 0 ;

Is this author mistake? i think InvalidateRect() should be called after using GetDC()/ReleaseDC() and not inside WM_PAINT message.

3

There are 3 answers

0
Avtem On BEST ANSWER

The answer is: no, it's not a mistake made by author. The function call InvalidateRect() is in right message and causes to repaint whole client area in all cases (as i assumed, but i wasn't 100% sure, so that's why i asked for a help). If you have this book and struggle with understanding this example, please try to understand every line in code. It took me a week to understand why there is call to InvalidateRect() function.

Thank everyone for your answers, i highly appreciate it!

0
SoronelHaetir On

The one thing this does is force the entire window to be invalid and not just whatever happens to be invalid (for instance due to an overlapped window being moved out of the way).

I'm not sure that it matters in this case but that is what the call accomplishes.

0
Zeus On

According to the document:

The system is not the only source of WM_PAINT messages. The InvalidateRect or InvalidateRgn function can indirectly generate WM_PAINT messages for your windows. These functions mark all or part of a client area as invalid (that must be redrawn).

So maybe the author wants to mark all of a client area as invalid, but it doesn’t make sense to do so in this example, because the WM_PAINT message is triggered after the form has been marked invalid, so adding or deleting this line of code will not influential.