0

我正在尝试win32 api所以我决定尝试在windows上画一个棋盘,只是为了测试

碰巧我可以绘制棋盘,并尝试在其上实现一些交互性。为此,我将鼠标点击存储在一个静态对象上,并在 WM_PAINT 上使用它在点击的房子周围绘制一个红色边框。

问题是,点击几下窗口后,屏幕变白了,没有刷子填充,我可以找到窗口标题,一团糟。

这是我的代码

#include <windows.h>
#include "WindowsX.h"
#include "stdafx.h"
#include <vector>
#include <stdlib.h>

//const char g_szClassName[] = "myWindowClass";
POINT p;
void TabuleiroCasas(std::vector<RECT>* rc, RECT rcClient)
{
        for(int i = 0; i < 8; i++)
        {
                for(int j = 0; j < 8; j++)
                {
                        int width = (rcClient.right-200 - rcClient.left) / 8;
                        int height = (rcClient.bottom - rcClient.top) / 8;

                        RECT r;
                        r.left = width*j;
                        r.top = rcClient.bottom - (height * i);
                        r.right = width*(j+1);
                        r.bottom = rcClient.bottom - height * (i+1);

                        rc->push_back(r);
                }
        }
}

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
        HPEN hPenSel = CreatePen(PS_SOLID, 3, RGB(255,0,0));
        HBRUSH hBrushgrey =CreateSolidBrush(RGB(200,200,200));
        HBRUSH hBrushblack =CreateSolidBrush(RGB(255,255,255));
        HBRUSH hBrushwhite = CreateSolidBrush(RGB(0,0,0));
        PAINTSTRUCT Ps;
        std::vector<RECT> casas;
        int xPos;
        int yPos;
    switch(msg)
    {
                case WM_LBUTTONDOWN:
                GetCursorPos(&p);
                InvalidateRect(hwnd, NULL, true);

                break;
                case WM_PAINT:
                        try
                        {
                        RECT rcClient;
                        GetClientRect(hwnd, &rcClient);
                        HDC hdc;


                        //DESENHA TABULEIRO
                        hdc = BeginPaint(hwnd, &Ps);
                        SelectObject(hdc, hBrushgrey);
                        Rectangle(hdc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
                        Rectangle(hdc, rcClient.left, rcClient.top, rcClient.right-200, rcClient.bottom);
                        TabuleiroCasas(&casas, rcClient);

                        for(int i = 0; i < casas.size(); i++)
                        {
                                if ( ((i + (i / 8)) % 2) == 1)
                                {

                                                SelectObject(hdc, hBrushwhite);
                                                Rectangle(hdc, casas[i].left ,casas[i].bottom, casas[i].right, casas[i].top);
                                }
                                else
                                {
                                                SelectObject(hdc, hBrushblack);
                                                Rectangle(hdc, casas[i].left ,casas[i].bottom, casas[i].right, casas[i].top);
                                }

                        }
                        for(int i = 0; i < casas.size(); i++)
                        {
                                if((p.x > casas[i].left) && (p.x < casas[i].right) && (p.y < casas[i].top) && (p.y > casas[i].bottom))
                                {

                                        SelectObject(hdc, hPenSel);
                                        Rectangle(hdc, casas[i].left ,casas[i].bottom, casas[i].right, casas[i].top);
                                }
                        }
                        EndPaint(hwnd, &Ps);
                        }
                        catch(int e)
                        {
                        }
                break;
                case WM_SIZE:
                        //InvalidateRect(hwnd, NULL, false);
                        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = L"Damas";
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, L"Window Registration Failed!", L"Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        3,
        L"Damas",
        L"The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, L"Window Creation Failed!", L"Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
4

1 回答 1

5

问题可以在窗口过程的顶部找到。每次调用您时,您都在创建一堆 GDI 对象WndProc

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
    HPEN hPenSel = CreatePen(PS_SOLID, 3, RGB(255,0,0));
    HBRUSH hBrushgrey =CreateSolidBrush(RGB(200,200,200));
    HBRUSH hBrushblack =CreateSolidBrush(RGB(255,255,255));
    HBRUSH hBrushwhite = CreateSolidBrush(RGB(0,0,0));
    .....
}

您永远不会将这些返回给系统,因此最终您会耗尽可用的 GDI 资源。仅在应用程序初始化时创建一次这些对象。

对我来说,这个电话DeleteDC看起来很可疑。你当然不需要它。去掉它。

您遇到的另一个问题是检索鼠标位置的代码。您永远不会使用GetCursorPos它,因为自从发布消息以来鼠标可能已经移动。你可以从lParam. 像这样:

p.x = GET_X_LPARAM(lParam);
p.y = GET_Y_LPARAM(lParam);

您需要正确包含 Windowsx 标头。它是这样完成的:

#include <WindowsX.h>

请注意问题中与您的代码的区别。

于 2013-04-15T15:04:17.710 回答