Avtem
Avtem

Reputation: 105

Why call InvalidateRect() in WM_PAINT message?

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.

Upvotes: 1

Views: 1331

Answers (3)

Avtem
Avtem

Reputation: 105

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!

Upvotes: 1

Zeus
Zeus

Reputation: 3890

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.

Upvotes: 2

SoronelHaetir
SoronelHaetir

Reputation: 15182

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.

Upvotes: 3

Related Questions