Win32不一致的CreateWindowEx调用

时间:2017-04-24 21:10:04

标签: c++ windows winapi

我正试图围绕Win32 API,并在创建窗口时得到不一致的结果。对CreateWindowEx的调用将失败,GetLastError()的结果将产生ERROR_NOT_ENOUGH_MEMORY错误代码。但有时应用程序成功创建窗口并运行正常。这让我很困惑。

// Standard Includes
#include <windows.h>
#include <iostream>

#ifndef UNICODE
    #define UNICODE
#endif
#ifndef _UNICODE
    #define _UNICODE
#endif

#define UNUSED_PARAM(x) (void)(x)

const char szWindowClass[] = "Program";
const char szTitle[] = "Program Desc";
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine, int nCmdShow) {

    UNUSED_PARAM(hPrevInstance);
    UNUSED_PARAM(lpCmdLine);

    WNDCLASSEX wcex;
    HWND hWnd;

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

    if(!RegisterClassEx(&wcex)) {
        MessageBox(NULL, "Call to RegisterClassEx failed!", szWindowClass, MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }

    hWnd = CreateWindowEx( WS_EX_CLIENTEDGE,             // dwExStyle
                           szWindowClass,                // lpClassName
                           szTitle,                      // lpWindowName
                           WS_OVERLAPPEDWINDOW,          // dwStyle
                           CW_USEDEFAULT, CW_USEDEFAULT, // x, y
                           240, 120,                     // width, height
                           NULL, NULL,                   // hWndParent, hMenu
                           hInstance, NULL);             // hInstance, lpParam
    std::cout << GetLastError();
    if(hWnd == NULL) {
        MessageBox(NULL, "Call to CreateWindow failed!", szWindowClass, MB_ICONEXCLAMATION | MB_OK);
        return 1;
    }

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

    // Main message loop
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

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

1 个答案:

答案 0 :(得分:7)

您没有初始化WNDCLASSEX.cbWndExtra,它定义了您的窗口类需要多少额外的存储空间。

你应该添加:

wcex.cbWndExtra = 0;

致电RegisterClassEx之前。或者你可以从这开始:

WNDCLASSEX wcex = { sizeof(wcex) };

cbSize成员初始化为结构大小,将所有其他成员初始化为0(或NULL)。