【问题标题】:Window doesn't appear窗口不出现
【发布时间】:2011-09-18 19:26:22
【问题描述】:

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

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

【问题讨论】:

  • 运行它时究竟会发生什么?

标签: c++ windows window


【解决方案1】:

比较类名:

wc.lpszClassName = "WindowClass1";

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

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

【讨论】:

  • 为什么不只是#define MY_CLASS_NAME _T("WindowClass1") ?
  • @Ajay:由于OP使用的是C++,所以const char* className = "WindowClass1";更合适。
  • 为什么不const TCHAR* className = _T("WindowClass1")? :)
猜你喜欢
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多