简体   繁体   中英

Unable to create a window

The RegisterClassEx function is returning a non zero value, however the CreateWindowEx function return a null. I have read multiple answers and I seem to be following everything correctly. Could someone please review my code and tell me what is going wrong? My guess is that there is something wrong with my WndProc, but I am unable to figure out what is going wrong. I am trying to create a class that can paint windows and hence the WndProc function is a class member.

void windowPainter::registerWindow()
{
    WNDCLASSEX wcex = { 0 };
    wcex.cbSize = sizeof(wcex);
    wcex.lpfnWndProc = windowPainter::WndProc;
    wcex.hInstance = m_hInstance;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.lpszClassName = windowClassName;

    ATOM returnValue = RegisterClassEx(&wcex);
    int lastErrorValue = GetLastError();
}

void windowPainter::createWindow()
{
    DWORD dwStyle1 = WS_EX_TOPMOST | WS_EX_LAYERED; 
    if (m_isClickThrough)
        dwStyle1 = dwStyle1 | WS_EX_TRANSPARENT;

    DWORD dwStyle2 = WS_POPUP;
    RECT rc = { m_windowOriginX, m_windowOriginY, m_windowSizeX, m_windowSizeY };

    AdjustWindowRect(&rc, dwStyle1, FALSE);
    BYTE alpha_value = 255; 

    m_hWnd = CreateWindowEx(dwStyle1,windowClassName, windowTitle, dwStyle2, rc.left,
        rc.top, rc.right,
        rc.bottom,
        NULL, NULL, m_hInstance, NULL);
}

LRESULT CALLBACK windowPainter::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    UNREFERENCED_PARAMETER(lParam);
    windowPainter* pThis = NULL;
    if (WM_CREATE == message)
    {
        pThis = (windowPainter*)((LPCREATESTRUCT)lParam)->lpCreateParams;
        SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pThis);
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    else
    {
        pThis = (windowPainter*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
    }

    if (pThis)
        switch (message)
        {
        case WM_PAINT:
            if (pThis)
            {
                //do painting using members of pThis
            }break;

        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        case WM_LBUTTONDOWN:
            pThis->onLeftButtonClick();
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

Look at these statements from your code:

m_hWnd = CreateWindowEx(..., NULL);

Here you pass NULL for the lpParam argument.

pThis = (windowPainter*)((LPCREATESTRUCT)lParam)->lpCreateParams;

This line of code is where you read that same value. Because you passed NULL to lpParam when calling CreateWindowEx , you will be setting pThis to NULL .

Instead of passing NULL to lpParam when calling CreateWindowEx you must pass the address of the instance.

m_hWnd = CreateWindowEx(..., (LPVOID)this);

Note that these changes should not influence whether or not the call to CreateWindowEx succeeds or fails. For the code in the question, pThis being NULL just means that you always call the default window procedure. Which will not lead to the window creation failing. You appear to have some other problem in your code, presumably in the code that we cannot see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM