0

我在 MSDN 上学习了这个例子,但是当我编码时,我在 CreateWindowEx() 上遇到了很大的麻烦。它返回NULL,所以它不能创建一个窗口。我不明白为什么它会返回。此代码遵循 MSDN 示例。这是我的代码:

#include <Windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hIns, HINSTANCE hPreIns, PWSTR pCmdLine, int nCmdShow)
{
    UNREFERENCED_PARAMETER(hPreIns);
    UNREFERENCED_PARAMETER(pCmdLine);
    HWND hWnd;
    TCHAR ClassName[] = L"Learn";
    TCHAR Title[] = L"My Window";
    WNDCLASSEX wcx;
    wcx.cbSize = sizeof(WNDCLASSEX);
    wcx.style = CS_HREDRAW | CS_VREDRAW;
    wcx.hInstance = hIns;
    wcx.lpszClassName = ClassName;
    wcx.lpfnWndProc = WndProc;
    wcx.cbClsExtra = 0;
    wcx.cbWndExtra = 0;
    wcx.hbrBackground = CreateSolidBrush(RGB(238,201,0));
    wcx.hIcon = LoadIcon(hIns, IDI_APPLICATION);
    wcx.lpszMenuName = NULL;
    wcx.hCursor = LoadCursor(hIns, IDC_WAIT);
    wcx.hIconSm = LoadIcon(hIns, IDI_INFORMATION);
    // Register
    RegisterClassEx(&wcx);

    hWnd = CreateWindowEx(0, ClassName, Title, WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,
    NULL, NULL, hIns, NULL);
    if(hWnd == NULL)
    {
        MessageBox(hWnd, L"Fail!", L"Warning!", MB_OK);
        return 1;
    }
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_PAINT:
        {
            HDC hdc;
            PAINTSTRUCT ps;
            hdc = BeginPaint(hWnd, &ps);
            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
    default: DefWindowProc(hWnd, msg, wParam, lParam);
    }
    return 0;
}

非常感谢。

4

1 回答 1

4
  default: DefWindowProc(hWnd, msg, wParam, lParam);

这是错误的,返回 DefWindowProc() 返回的值非常重要。如果您不这样做,那么您的消息处理程序将为WM_NCCREATE 消息返回 FALSE 。记录强制 CreateWindowEx() 返回 NULL,Windows 放弃了创建窗口的尝试。使固定:

  default: return DefWindowProc(hWnd, msg, wParam, lParam);
于 2014-11-30T13:43:45.100 回答