Is win32-gdi system-driven WM_PAINT flicker free?

1

running this code leads to the title question:

if you resize the window you will not see any flicker (repaint sended by the system)

if you move mouse inside the window, severe flicker will occurr (repaint sended by me)

how to reproduce the system-driven WM_PAINT?

#include <windows.h>
#include <wingdi.h>

LRESULT CALLBACK proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch(msg)
    {
        case WM_ERASEBKGND: return true;break;
        case WM_MOUSEMOVE: InvalidateRect(hwnd, 0, 0); break;
        case WM_PAINT:
        {
            InvalidateRect(hwnd,0,0);
            HBRUSH b= CreateSolidBrush(0x000000ff);
            HBRUSH c= CreateSolidBrush(0x0000ff00);
            HBRUSH d= CreateSolidBrush(0x00ff0000);
            RECT r;
            GetClientRect(hwnd,&r);
            PAINTSTRUCT ps;
            HDC hdc=BeginPaint(hwnd,&ps);
            FillRect(hdc,&r, b); 
            Sleep(10);
            FillRect(hdc,&r, c);
`           Sleep(10);
            FillRect(hdc,&r,d);
            EndPaint(hwnd,&ps);
            DeleteObject(b);
            DeleteObject(c);
            DeleteObject(d);
        }
        break;
        default:
            return DefWindowProc(hwnd, msg, wparam, lparam);
    }
    return 0;
}
int main()
{
    HWND hwnd=CreateWindow(WC_DIALOG,0,WS_OVERLAPPEDWINDOW|WS_VISIBLE,0,0,500,500,0,0,0,0);
    SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)proc);
    
    MSG msg;
    
    while (true)
    {
        if (GetMessage(&msg, 0, 0, 0) != WM_CLOSE)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return 1;
}
c++
winapi
gdi
wm-paint
asked on Stack Overflow Mar 10, 2021 by freesoft • edited Mar 10, 2021 by freesoft

1 Answer

0

You should not invalidate the window if the mouse only moves over it because that will lead to a WM_PAINT message ultimately. That causes the flicker (in combination with the sleep).

answered on Stack Overflow Mar 11, 2021 by guest

User contributions licensed under CC BY-SA 3.0