Home > Software engineering >  What means by 'clear the update region' when painting the client area on Windows?
What means by 'clear the update region' when painting the client area on Windows?

Time:11-17

Here the Painting the Window mentioned that:

After you finish painting the client area, you clear the update region, which tells the operating system that it does not need to send another WM_PAINT message until something changes.

One may write these code:

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);

            RECT rect;
            GetClientRect(hWnd, &rect);
            HBRUSH brush = CreateSolidBrush(RGB(127, 127, 127));

            FillRect(hdc, &rect, brush);

            const wchar_t * lstr = L"here is information";
            TextOut(hdc,
                5, 5,
                lstr, _tcslen(lstr));


            DrawText(hdc, TEXT("Singleline in center~"), -1, &rect,
                DT_SINGLELINE | DT_CENTER | DT_VCENTER);

            MoveToEx(hdc, 50, 100, NULL);
            LineTo(hdc, 44, 10);
            LineTo(hdc, 78, 40);

            Rectangle(hdc, 16, 36, 72, 70);
            Rectangle(hdc, 34, 50, 54, 70);

            DeleteObject(brush);

            EndPaint(hWnd, &ps);
        }

What means by clear here? We do not want what we have drawn to be cleared.

CodePudding user response:

WM_PAINT messages are generated on demand (see Paint messages will come in as fast as you let them). For each window, the system maintains an update region. Clients can mark parts or all of a window's client area as "invalid", calling InvalidateRect or InvalidateRgn. Either one adds to the update area, but doesn't immediately trigger a WM_PAINT message.

When the system determines that it's time to send a WM_PAINT message, it is the client's responsibility to empty the update region when it's done painting so that no additional WM_PAINT messages are generated until the update region is non-empty again.

The call to BeginPaint does that for you, so you don't have to worry about this so long as you use standard WM_PAINT handling. If you do have more specific requirements (e.g. when using a Direct2D render target) you would have to manually clear the update region with a call to ValidateRect or ValidateRgn.

CodePudding user response:

I used your code and noticed that your drawn be cleared was not due to 'clearing the Update region', but that your code ended up with no breaks.

  • Related