我有這樣的代碼,是我創造,將有多個窗口添加代碼在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);
}
}
作爲一個附註,與其維護所有窗口的全局列表,您可以通過使用GWL_USERDATA調用SetWindowLong將用戶數據存儲在窗口中:http://msdn.microsoft.com/zh-cn/library/windows/桌面/ ms633591(v = vs.85).aspx – avakar