为什么不画在hdc上立即更新窗口?

时间:2018-09-19 16:34:20

标签: c++ windows winapi gdi

根据this website,在BeginPaint返回的HDC上执行的任何绘制操作都将立即显示在屏幕上。但是,以下代码打印的数字仅在调整窗口大小时更新:

int counter = 0;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            std::string s = std::to_string(counter).c_str();
            TextOutA(hdc, 0, 0, s.c_str(), s.length());
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_KEYDOWN:
        counter++;
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

即使使用连续向窗口发送WM_PAINT

RedrawWindow(hwndMain, 0, 0, RDW_INTERNALPAINT);

在EndPaint之后,该数字仅在调整窗口大小时更新。如何在不手动调整窗口大小的情况下获取要更新的号码?

1 个答案:

答案 0 :(得分:-1)

使用不同的标志调用RedrawWindow解决了此问题。 具体在RedrawWindow(hWnd, 0, 0, RDW_FRAME | RDW_INVALIDATE);之后的counter++;