CreateWindowEx returns null : unable to create a w

2019-09-22 05:10发布

问题:

CreateWindowEx method always returns null.

I don't know whats wrong, but I am not able to even create a window.

My window procedure is a static method, BaseWndApplication::WndProc, defined in another class, I am not sure if that should cause any problems, As I can register my WNDCLASSEX structure successfully.

Please heeeelp!!

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

const char * WINDOW_CLASS_NAME = "D2DAppClass";
HRESULT hr;
{
    // Register the window class.
    WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = BaseWndApplication::WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hbrBackground = NULL;
    wcex.lpszMenuName = NULL;
    wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
    wcex.lpszClassName = WINDOW_CLASS_NAME;

    if (!RegisterClassEx(&wcex))
        return E_FAIL;

    HWND m_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,
        WINDOW_CLASS_NAME,
        "Direct2D Demo App",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        static_cast<UINT>(ceil(640.f)),
        static_cast<UINT>(ceil(480.f)),
        NULL,
        NULL,
        hInstance,
        NULL
        );

    hr = m_hWnd ? S_OK : E_FAIL;
    if (SUCCEEDED(hr))
    {
        ShowWindow(m_hWnd, SW_SHOWNORMAL);
        UpdateWindow(m_hWnd);
    }
}

return 0;

}

回答1:

It was a problem with my callback method. It was set to return 0;

I changed it as follows, and then it worked:

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc(hWnd, message, wParam, lParam);
}