我有这段代码,它是我正在创建的具有多个窗口的用户界面系统的一部分
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 中的注释 block ,窗口会正确显示,但是,如果我在 UISystem::WndProc 中取消注释此 block ,则从 CreateWindow 返回无效句柄任何帮助都会不胜感激,因为这真的让我感到困惑,我曾尝试在 UISystem::WndProc 中执行任何其他代码之前调用 DefWindowProc,但我所有的尝试都失败了
这是 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)
只要您的窗口存在,这个 while 就会循环,并且会阻止正确构造 UIWindow。该对象 (UIWindow) 实际上在内部被访问:UISystem::HandleMessage,但由于其构造函数永远不会结束,因此它可能为 NULL 或处于未定义状态。
关于c++ - 将代码添加到 WNDCLASSEX 中的 WndProc 回调会破坏代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12941309/