Win32 API窗口仍在处理,无法关闭

时间:2017-11-04 11:22:40

标签: c++ windows winapi

我编码在VS 2017中使用win32 api创建一个窗口,代码似乎是正确的但是在执行时窗口显示并继续处理,当关闭时,它将被关闭但在后台继续处理我必须手动关闭它,但VS中的[停止调试]按钮。

CODE ::

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    } break;
    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = L"WindowClass1";
if (!RegisterClassEx(&wc))
    return 1;

CREATESTRUCT ws;
ZeroMemory(&ws, sizeof(CREATESTRUCT));
ws.style = WS_SYSMENU | WS_MINIMIZEBOX;
RECT wr{ 0,0,800,600 };
AdjustWindowRectEx(&wr, ws.style, FALSE, 0);
ws.x = 0;   ws.y = 0;
ws.cx = wr.right - wr.left; ws.cy = wr.bottom - wr.top;
ws.hInstance = hInstance;
ws.lpszClass = wc.lpszClassName;
ws.lpszName = L"Window1";
HWND hWnd = CreateWindowEx(ws.dwExStyle, ws.lpszClass, ws.lpszName, ws.style, ws.x, ws.y, ws.cx, ws.cy, ws.hwndParent, ws.hMenu, ws.hInstance, ws.lpCreateParams);
if (!hWnd)
{
    MessageBox(NULL, L"Window could not be created!", L"ERROR!", MB_OK | MB_ICONERROR);
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

MSG msg;
while (true)
{
    if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        if (msg.message == WM_QUIT)
            break;
    }
}

UnregisterClass(wc.lpszClassName, wc.hInstance);
return (int)msg.wParam;
}

0 个答案:

没有答案