user1754209
user1754209

Reputation: 21

Adding code to the WndProc callback in WNDCLASSEX breaks code

I have this code which is part of my user interface system that I am creating that will have multiple windows

bool UISystem::HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    UIWindow* target = NULL;
    for(vector<UIWindow*>::iterator it = windowList.begin(); it < windowList.end(); it++)
    {
        if((*it)->windowHandle == hwnd)
        {
            target = *it;
            break;
        }
    }

    if(target == NULL)
    { return false; }

    switch(msg)
    {
    case WM_DESTROY:

        return true;

    case WM_PAINT:  

        return true;

    default:
        return false;
    }
}

LRESULT WINAPI UISystem::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    /*if(UISYSTEM->HandleMessage(hwnd, msg, wParam, lParam))
    {
    }*/
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

When this code is implemented as it is, including the comment block in UISystem::WndProc, a window is displayed correctly however, if I uncomment this block in UISystem::WndProc then an invalid handle is returned from CreateWindow any help would be appreciated because this is really confusing me, I have tried to call DefWindowProc before I do any other code in UISystem::WndProc but all my attemps have failed

This is the Constructor for UIWindow:

UIWindow::UIWindow(int x, int y, int width, int height, string & text)
{

    int frameWidth   = GetSystemMetrics(SM_CXSIZEFRAME);
    int frameHeight  = GetSystemMetrics(SM_CYSIZEFRAME);
    int menuHeight   = GetSystemMetrics(SM_CYMENU);
    int windowXPos   = (GetSystemMetrics(SM_CXSCREEN) - width) / 2;
    int windowYPos   = (GetSystemMetrics(SM_CYSCREEN) - height) / 2;
    int windowWidth  = width + frameWidth * 2;
    int windowHeight = height + frameHeight * 2 + menuHeight;

    bounds.X = x;
    bounds.Y = y;
    bounds.Width = width;
    bounds.Height = height;
    title = text;

    MSG msg;

    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, 
        &UISystem::WndProc, 0, 0, hInstance, NULL, NULL, (HBRUSH)(COLOR_WINDOW + 1), 
        NULL, "AurousWindow", NULL};

    RegisterClassEx(&wc);


    windowHandle = CreateWindow("AurousWindow", title.c_str(), WS_OVERLAPPEDWINDOW, windowXPos, windowYPos, windowWidth, windowHeight, NULL, NULL, hInstance, NULL);
    SetWindowRgn(windowHandle, CreateRectRgn(0, 0, width, height), TRUE);
    ShowWindow(windowHandle, nShow);
    UpdateWindow(windowHandle);

    //RECT rec = {0, 0, width, height};

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

Upvotes: 2

Views: 348

Answers (1)

marcinj
marcinj

Reputation: 49986

Not sure if this is the root problem but one thing you should fix is to remove:

while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

from:

UIWindow::UIWindow(int x, int y, int width, int height, string & text)

this while will loop as long as your window exists and will prevent UIWindow from being properly constructed. This object (UIWindow) is actually being accessed inside: UISystem::HandleMessage, but since its constructor never ends then it is probably NULL or in undefined state.

Upvotes: 1

Related Questions