2012-10-17 30 views
2

我有这样的代码,是我创造,将有多个窗口添加代码在WNDCLASSEX WndProc中回调打破代码

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返回一个无效句柄,任何帮助将不胜感激,因为这真是令我困惑,我试过了调用DefWindowProc之前,我做任何其他代码在UISystem :: WndProc但我所有的尝试失败

这是一个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); 
    } 
} 
+3

作为一个附注,与其维护所有窗口的全局列表,您可以通过使用GWL_USERDATA调用SetWindowLong将用户数据存储在窗口中:http://msdn.microsoft.com/zh-cn/library/windows/桌面/ ms633591(v = vs.85).aspx – avakar

回答

1

不知道这是否是问题的根源,但有一件事你应该修改是取消:

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,但是由于它的构造函数永远不会结束,所以它可能是NULL或者处于未定义状态。

+0

luskan,那么如何在不关闭整个应用程序的情况下为每个窗口保留消息泵? – user1754209

+1

请在此处查看:http://msdn.microsoft.com/en-us/library/windows/desktop/ms644936%28v=vs.85%29.aspx,每个线程应该有一个消息循环,只需将其放入你main()函数,它会好的。初始化在此循环之前执行。 – marcinj

+0

我想我可能会意外删除您的评论luskan,对不起! 这是怎么回事? http://pastebin.com/K9sFnJ3E – user1754209