窗口没有出现

时间:2011-06-20 15:27:25

标签: c++ windows window

我写了一个小程序来创建一个窗口。我以前做过这个程序,但现在我正在努力为自己回忆所有的事情。 当我完成程序编写时,窗口将不会出现,当我将我的代码与我正在学习的书进行比较时,它就是一样的。我错过了什么/做错了什么?

#include <windows.h>
#include <WindowsX.h>


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
    HWND hWnd;

    // information for the window class
    WNDCLASSEX wc;
    ZeroMemory(&wc, sizeof(WNDCLASSEX));


    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
     wc.lpfnWndProc = WindowProc;
     wc.hInstance = hInstance;
     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
     wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
     wc.lpszClassName = "WindowClass1";

     RegisterClassEx(&wc);

     // Create Window
     hWnd = CreateWindowEx( NULL,
                            "WindowClass",
                            "My Program",
                            WS_OVERLAPPEDWINDOW,
                            100,
                            100,
                            600,
                            480,
                            NULL,
                            NULL,
                            hInstance,
                            NULL);


     ShowWindow(hWnd, SW_SHOWDEFAULT);



     MSG msg;

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


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

1 个答案:

答案 0 :(得分:3)

比较班级名称:

wc.lpszClassName =“WindowClass1”;

hWnd = CreateWindowEx(NULL,“WindowClass”,...

查找此类错误的最佳方法是检查每个API的返回代码。