井字游戏:Winapi 绘画

Tic Tac Toe: Winapi painting

每当我将 bool 设为真时,我在我的客户区绘画时遇到问题,试图制作井字棋,一旦我正确地完成了初始绘画,我就可以继续进行。

主要问题是它没有正确绘制下行,或者在单击 3 或 4 后绘制它们,目前我不关心圆圈,只想知道我错了什么.

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        static BOOL fState[DIVISIONS][DIVISIONS];
        bool  XorO = false;
        static int  cxBlock, cyBlock;`

        HDC         hdc;
        int         x, y, iPosX , iPosY;
        PAINTSTRUCT ps;
        RECT        rect;

        static const int grid = 1000;
        static const int block = grid / DIVISIONS;
        static const int width = 1200;
        static const int height =1200;

        switch (message)
        {
        case WM_SIZE:

                cxBlock = LOWORD (lParam) / DIVISIONS ;
                cyBlock = HIWORD (lParam) / DIVISIONS ;

            return 0;

        case WM_LBUTTONDOWN:
            x = LOWORD(lParam)/ cxBlock;
            y = HIWORD(lParam)/cyBlock;

            if (x < DIVISIONS && y < DIVISIONS)
            {
                //Click in the first cuadrant x = 0 and y = 0 , so both false
                //second x = 1, y = 0
                //third x 2 , y = 0 and so on.
                fState[x][y] ^= 1;
                rect.left = x * block;
                rect.top = y * block;
                rect.right = (x + 1) * block;
                rect.bottom = (y + 1) * block;

                InvalidateRect(hwnd, &rect, FALSE);

            }
            else
                MessageBeep(0); 

            return 0;

        case WM_PAINT:

            hdc = BeginPaint(hwnd, &ps);


            //paint X or ellipses here
            for (x = 0; x < DIVISIONS; x++)
                for (y = 0; y < DIVISIONS; y++)
                {

                    if (fState[x][y] )
                    {
                        MoveToEx(hdc, x     * block, y       *  block, NULL);
                        LineTo(hdc,  (x + 1) * block, (y + 1) *  block);
                        MoveToEx(hdc, x     * block, (y + 1) *  block, NULL);
                        LineTo(hdc,  (x + 1) * block, y       *  block);
                    }
                }


            //main grid
            for (int i = block; i < grid - 1; i += block) {
                MoveToEx(hdc, i, 0, NULL); 
                LineTo(hdc, i, grid);
                MoveToEx(hdc, 0, i, NULL);
                LineTo(hdc, grid, i);


            }

            EndPaint(hwnd, &ps);
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }

我什至试图通过为每个场景制作一个 if 语句来使绘画更直接和原始,但是,一旦程序运行,它就无法在第二行和第三行正确绘制。或者如果我单击 space 2,2(网格上的最后一个)它不会绘制直到我单击其他地方...

您需要调用 SelectObject 以 select 将合适的笔插入 DC,否则 LineTo 将随机使用剩下的任何笔(可能是不可见的或其他东西)。