1

运行此代码会导致标题问题:

如果您调整窗口大小,您将看不到任何闪烁(系统发送的重绘)

如果你在窗口内移动鼠标,会出现严重的闪烁(我发送的重绘)

如何重现系统驱动的 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;
}
4

1 回答 1

0

如果鼠标只在窗口上方移动,则不应使窗口无效,因为这最终会导致 WM_PAINT 消息。这会导致闪烁(与睡眠相结合)。

于 2021-03-11T08:19:20.587 回答