FillRect Wont绘制到屏幕

时间:2020-10-01 08:08:36

标签: c++ winapi

我正在做俄罗斯方块克隆,以了解WinApi的工作原理:

const int sm = 30, //screen multiplier
fWidth = 12, fHeight = 20;

char field[fHeight][fWidth];

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    {

        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        for (int y = 0; y < fHeight; y++)
            for (int x = 0; x < fWidth; x++)
            {
                bool i;
                HBRUSH brush;
                RECT rect = { (x + 1) * sm, y * sm, (x + 2) * sm, (y + 1) * sm };
                switch (field[y][x])
                {
                case 'x':
                    brush = CreateSolidBrush(RGB(255, 255, 255));
                    break;
                default:
                    brush = CreateSolidBrush(RGB(0, 255, 0));
                    break;
                }
                FillRect(hdc, &rect, brush);
                DeleteObject(brush);
            }

        
        EndPaint(hwnd, &ps);
        return 0;
    }
    }
}

DWORD WINAPI GameLoop(LPVOID lpParam)
{
    HWND hwnd = *reinterpret_cast<HWND*>(lpParam);
    MSG msg;
    msg.hwnd = hwnd;
    msg.message = WM_PAINT;
    while (!quit)
    {
        for (int y = 0; y < 4; y++)
            for (int x = 0; x < 4; x++)
                if (tetriminous[tetrimino][x + 4 * y] == 'x')
                    field[pieceY + y][pieceX + x] = ' ';

        pieceY += 1;

        for (int y = 0; y < 4; y++)
            for (int x = 0; x < 4; x++)
                if (tetriminous[tetrimino][x + 4 * y] == 'x')
                    field[pieceY + y][pieceX + x] = 'x';

        DispatchMessage(&msg);
        Sleep(300);
    }
    return 0;
}

我将gameloop用作线程,并在更新后调度WM_PAINT。 问题是在VM_PAINT部分中,fillRect不会绘制到屏幕上。我进行了装袋,并且rect在屏幕上。并且fillrect返回成功代码。有什么问题吗?

1 个答案:

答案 0 :(得分:0)

好的,将DispatchMessage更改为InvalidateRect后,问题已解决。