创建窗口不起作用,但不提供错误消息

时间:2015-01-24 07:52:05

标签: c++ window

我正在尝试创建一个窗口,没有出现但我没有收到任何错误消息,除了我测试hWnd的那个。

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#include "entity.h"

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

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wcex;

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

    if(!RegisterClassEx(&wcex))
    {
        MessageBox(NULL, "FAILED TO REGISTER WINDOW CLASS", "ERROR", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    HWND hWnd = CreateWindow("WinD", "Chair", WS_OVERLAPPEDWINDOW, 480, 480, 480, 480, NULL, NULL, hInstance, NULL);

    if (hWnd == NULL)
    {
        MessageBox(NULL, "FAILED TO CREATE WINDOW", "ERROR", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

我只是不知道该怎么做。

我做了一切,就像那个人说的那样,我已经多次阅读并重新阅读了这个页面,并且无法理解为什么这样做不起作用。

1 个答案:

答案 0 :(得分:2)

您的classname参数不匹配。如果您使用过GetLastError(),则会返回1407,即:

ERROR_CANNOT_FIND_WND_CLASS
    1407 (0x57F)
    Cannot find window class.

wcex.lpszClassName = "Extt";更改为wcex.lpszClassName = "WinD";或将第一个参数更改为CreateWindow"Extt"