1

我一直在努力让我的窗户工作。我正在查看有关如何制作窗口的 MSDN 页面,但我看不出有什么区别。当我运行我的程序时,它会出现一个消息框,说 NO WINDOW。

代码:

#include <windows.h>

static const LPSTR CLASSNAME = "Win32Window";

LRESULT CALLBACK WndProc(HWND window, UINT msg, WPARAM wParam, LPARAM lParam)
{

    switch (msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        DefWindowProc(window, msg, wParam, lParam);
        break;
    }

    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int nCmdShow)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_ARROW));
    wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    wcex.hInstance = hInstance;
    wcex.lpfnWndProc = WndProc;
    wcex.lpszClassName = CLASSNAME;
    wcex.lpszMenuName = NULL;
    wcex.style = CS_HREDRAW | CS_VREDRAW;

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL, "Failed to register window class", "ERROR", MB_ICONERROR);
        return 1;
    }

    HWND window = CreateWindow(CLASSNAME, "title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);

    if (!window)
    {
        MessageBox(NULL, "NO WINDOW", "ERROR", MB_ICONERROR); // THIS IS BEING CALLED!!!! WHY??
        return 1;
    }

    ShowWindow(window, nCmdShow);
    UpdateWindow(window);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.lParam;
}

如果有人能告诉我这里出了什么问题,那就太好了。

4

1 回答 1

5

您的窗口过程 ( WndProc) 需要返回由DefWindowProc. 目前,您只是为所有未处理的消息返回 0,这会产生副作用,例如(响应于WM_NCCREATE)导致您的窗口创建失败。

于 2014-12-22T10:39:29.570 回答