窗口未显示

3

我编写了一个小程序来创建窗口。我以前也做过这个程序,但现在我正在尝试为自己回忆起所有的事情。当我完成编写程序后,窗口不会出现,当我将我的代码与我学习的书籍进行比较时,它们是相同的。我错过或做错了什么?

#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);
}

当你运行它时,确切地会发生什么? - David Thornley
1个回答

4

比较类名:

wc.lpszClassName = "WindowClass1";

hWnd = CreateWindowEx(NULL, "WindowClass", ...

找到这种错误的最好方法是检查每个API的返回代码。


为什么不直接使用 #define MY_CLASS_NAME _T("WindowClass1") 呢? - Ajay
2
@Ajay:由于OP正在使用C++,因此const char* className = "WindowClass1";更为合适。 - In silico
为什么不使用 const TCHAR* className = _T("WindowClass1") 呢? :) - Ajay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接