如何让 wndproc 处理发送到多个窗口的消息
How to get wndproc to handle messages to multiple windows
我一直在尝试让 wndproc 成功从消息队列接收消息并根据我的代码处理它们,但它没有按照我想要的方式工作,我希望它做的是确定哪个窗口接收WM_DESTROY或WM_CLOSE并调用特定于哪个窗口关闭的代码, 但出于某种原因,目前它什么也没做。经过大量的实验,我已经让它以不同的方式部分运行,但似乎每个实现都不能以正确的方式运行。这是我最新的不成功代码:
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK Proc2(HWND mainwin, UINT message, WPARAM wParam, LPARAM lParam )
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
const HWND active_window = GetActiveWindow();
if (active_window == mainwin)
{
PostQuitMessage(0);
}
if (active_window == hwnd2)
{
PostQuitMessage(0);
EnableWindow (mainwin, true);
}
break;
}
switch (wParam) /* handle the messages */
{
case ID_2 :
PostQuitMessage(0);
break;
case ID_1 :
ShowWindow (hwnd2, SW_SHOW);
break;
default: /* for messages that we don't deal with */
return DefWindowProc (mainwin, message, wParam, lParam);
break;
}
return 0;
}
这是我尝试使用多个窗口过程的代码
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK Proc2(HWND mainwin, UINT message, WPARAM wParam, LPARAM lParam )
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
switch (wParam) /* handle the messages */
{
case ID_2 :
PostQuitMessage(0);
break;
case ID_1 :
ShowWindow (hwnd2, SW_SHOW);
break;
default: /* for messages that we don't deal with */
return DefWindowProc (mainwin, message, wParam, lParam);
break;
}
return 0;
}
LRESULT CALLBACK Proc3(HWND hwnd2, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
EnableWindow (mainwin, false);
break;
case WM_CLOSE:
EnableWindow (mainwin, false);
break;
}
switch (wParam)
{
default:
return DefWindowProc (hwnd2, message, wParam, lParam);
break;
}
return 0;
}
你说,
"我希望它做的是确定哪个窗口收到了WM_DESTROY或WM_CLOSE"
简单的答案是接收UINT message
的窗口是HWND mainwin
。
如果你想知道"是的,但我的哪个窗口HWND mainwin
?"那么一种方法是,当你创建一个窗口时,你记住它的窗口句柄是什么,并将其存储在某个地方(例如作为全局变量)。
除此之外,不清楚为什么要调用GetActiveWindow()
,也不清楚要实现什么功能。
相关文章:
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息
- 由于无效的 ValidateRgn() 子窗口不会收到WM_PAINT消息
- MFC 中的窗口消息管理:添加基类调用是否是强制性的?
- 输出窗口中消息的可视化工作室源
- 为什么初始化 Wintab 扩展后没有收到WT_PACKETEXT窗口消息?
- 与WM_DPICHANGED消息一起发送的建议窗口大小太大
- 如何处理窗口的消息?
- 在弹出窗口中管理CEF客户端区域的窗口消息
- 在Internet Explorer_Server(IE9)中处理窗口消息
- 使用智能指针在窗口消息中将对象作为 WPARAM 传递
- 将窗口消息发送到控制台应用程序
- 是否可以捕获翻译/调度消息循环中的窗口消息/事件
- 调整窗口大小时不断发送窗口消息WM_SIZING
- 正在发送转发窗口消息
- 对于单个实例应用程序,只允许使用单个窗口消息框
- Win32:更多"object oriented"窗口消息处理系统
- 异步代理和窗口消息
- Windows (c++)上窗口消息的问题(延迟)
- WinAPI窗口消息似乎被插件窃取-如何修复
- 在 COleControl 中未处理的 LButtonUp 窗口消息