Winapi WM_油漆经过一些重画后,它会发疯的

Winapi WM_油漆经过一些重画后,它会发疯的,winapi,visual-c++,Winapi,Visual C++,我正在试用win32 api,所以我决定尝试在windows上绘制一个棋盘,只是为了测试 碰巧我可以画棋盘,我试着在上面实现一些交互性。为了做到这一点,我将鼠标点击存储在一个静态文件上,并在WM_画图上使用它在点击的房子周围绘制一个红色边框 问题是,在点击几下窗口后,屏幕变得全白,没有笔刷填充,我可以找到窗口标题,这是一个完全混乱的局面 这是我的密码 #include <windows.h> #include "WindowsX.h" #include "stdafx.h" #inc

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

碰巧我可以画棋盘,我试着在上面实现一些交互性。为了做到这一点,我将鼠标点击存储在一个静态文件上,并在WM_画图上使用它在点击的房子周围绘制一个红色边框

问题是,在点击几下窗口后,屏幕变得全白,没有笔刷填充,我可以找到窗口标题,这是一个完全混乱的局面

这是我的密码

#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;
}
#包括
#包括“WindowsX.h”
#包括“stdafx.h”
#包括
#包括
//const char g_szClassName[]=“myWindowClass”;
p点;
void TabuleiroCasas(std::vector*rc,RECT-rcClient)
{
对于(int i=0;i<8;i++)
{
对于(int j=0;j<8;j++)
{
int width=(rcClient.right-200-rcClient.left)/8;
int height=(rcClient.bottom-rcClient.top)/8;
矩形r;
r、 左=宽度*j;
r、 顶部=rcClient.bottom-(高度*i);
r、 右=宽度*(j+1);
r、 底部=rcClient.bottom-高度*(i+1);
rc->推回(r);
}
}
}
//步骤4:窗口过程
LRESULT回调WndProc(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM)
{
HPEN HPEN=CreatePen(PS_SOLID,1,RGB(255255));
HPEN hPenSel=CreatePen(PS_SOLID,3,RGB(255,0,0));
HBRUSH hBrushgrey=CreateSolidBrush(RGB(200200));
HBRUSH HBRUSHBLICK=CreateSolidBrush(RGB(255255));
HBRUSH hBrushwhite=CreateSolidBrush(RGB(0,0,0));
PAINTSTRUCT-Ps;
std::载体casas;
int XPO;
int yPos;
开关(msg)
{
案例WM_LBUTTONDOWN:
GetCursorPos&p;
无效(hwnd,NULL,true);
打破
案例WM_油漆:
尝试
{
RECT-rcClient;
GetClientRect(hwnd和rcClient);
HDC-HDC;
//德森哈塔布列罗
hdc=开始喷漆(hwnd和Ps);
选择对象(hdc、HBRUSH格雷);
矩形(hdc、rcClient.left、rcClient.top、rcClient.right、rcClient.bottom);
矩形(hdc,rcClient.left,rcClient.top,rcClient.right-200,rcClient.bottom);
TabuleiroCasas(和casas,rcClient);
对于(int i=0;icasas[i].左)和&(p.xcasas[i].下))
{
选择对象(hdc、hPenSel);
矩形(hdc,casas[i]。左侧,casas[i]。底部,casas[i]。右侧,casas[i]。顶部);
}
}
端漆(hwnd和Ps);
}
捕获(INTE)
{
}
打破
案例WM_大小:
//无效(hwnd、NULL、false);
打破
案例WM_结束:
窗口(hwnd);
打破
案例WM_销毁:
PostQuitMessage(0);
打破
违约:
返回DefWindowProc(hwnd、msg、wParam、lParam);
}
返回0;
}
int WINAPI WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、,
LPSTR lpCmdLine,int nCmdShow)
{
WNDCLASSEX wc;
HWND-HWND;
味精;
//步骤1:注册窗口类
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=0;
wc.lpfnWndProc=WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(空,IDI_应用程序);
wc.hCursor=LoadCursor(空,IDC_箭头);
wc.hbrBackground=(HBRUSH)(彩色窗口+1);
wc.lpszMenuName=NULL;
wc.lpszClassName=L“Damas”;
wc.hIconSm=LoadIcon(空,IDI_应用程序);
如果(!RegisterClassEx(&wc))
{
MessageBox(NULL,L“窗口注册失败!”,L“错误!”,
MB|U图标连接| MB|U OK);
返回0;
}
//步骤2:创建窗口
hwnd=CreateWindowEx(
3.
L“Damas”,
L“我窗口的标题”,
WS_重叠窗口,
CW_USEDEFAULT,CW_USEDEFAULT,800600,
NULL,NULL,hInstance,NULL);
if(hwnd==NULL)
{
MessageBox(NULL,L“窗口创建失败!”,L“错误!”,
MB|U图标连接| MB|U OK);
返回0;
}
显示窗口(hwnd、nCmdShow);
更新窗口(hwnd);
//步骤3:消息循环
while(GetMessage(&Msg,NULL,0,0)>0)
{
翻译信息(&Msg);
发送消息(&Msg);
}
返回Msg.wParam;
}

问题可以在窗口的顶部找到p
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));
    .....
}
p.x = GET_X_LPARAM(lParam);
p.y = GET_Y_LPARAM(lParam);
#include <WindowsX.h>