WM_PAINT经过一些重绘之后就吓坏了

时间:2013-04-15 14:57:32

标签: winapi visual-c++

我正在尝试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;
}

1 个答案:

答案 0 :(得分:4)

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

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>

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