在WNDClasSex中断代码中将代码添加到WNDPROC回调中



我有这个代码,这是我正在创建的用户界面系统的一部分,它将具有多个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);
}

当该代码按原样实现(包括uisystem :: wndproc中的注释块)时,正确显示了一个窗口,但是,如果我在uisystem中删除此块:: wndproc中的此块,那么从createwindow返回了一个无效的句柄值得赞赏,因为这确实使我感到困惑,我试图在uisystem中执行其他任何代码:: wndproc中的任何其他代码之前,都试图致电defwindowproc,但是我所有的adtemps都失败了

这是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);
    }
}

不确定这是否是根问题,但是您应该解决的一件事是删除:

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

来自:

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

,只要您的窗户存在,并且将阻止Uiwindow的正确构造。实际上,此对象(UIWindow)正在内部访问:UISYSTEM :: handlemessage,但是由于其构造函数永远不会结束,因此它可能是无效的或处于未定义的状态。

最新更新