why not to send WM_PAINT manually

后端 未结 4 1776
一生所求
一生所求 2020-12-18 09:01

I have read that I should never send WM_PAINT manually and should call InvalidateRect instead but didn\'t found anything about why not, however. So

4条回答
  •  抹茶落季
    2020-12-18 09:56

    Because WM_PAINT is not a real message.

    Think of it like each window has a structure storing an "invalid region", that is, "this part of the window on the screen is not up to date anymore and need to be repainted".

    That invalid region is modified by the window manager itself (window is resized, uncovered, etc ), or by calls to InvalidateRect, ValidateRect, EndPaint and such.

    Now, here is a crude mock-up of how GetMessage handles this :

    ... GetMessage(MSG* msg, ...)
    {
      while(true) {
        if(ThereIsAnyMessageInTheMessageQueue()) {
          *msg = GetFirstMessageOfTheMessageQueue();
          return ...;
        } else if(TheInvalidRegionIsNotEmpty()) {
          *msg = CreateWMPaintMessage();
          return ...;
        } else {
          WaitUntilSomethingHappend();
        }
      }
    }
    

    tl;dr: WM_PAINT is meant to be received, not sent.

提交回复
热议问题