我知道您现在正在尝试做什么,我已经在名为 Lucid 的应用程序框架中实现了这一点(它仍在进行中)。为了回答,您的窗口类将被调用Window
而不是APP
.
这是通过将全局过程传递给您创建的每个窗口来完成的。所有窗口共享相同的过程。每次任何窗口收到消息时,该消息都会发送到全局过程,全局过程会检查是否HWND
属于Window
您创建的 a,如果属于,则将消息发送到该Window
s 的过程。以下是其工作原理的概述。
class Window
{
public:
// The contents of this function can vary from window to window
// provided that you make a subclass and override this method.
virtual LRESULT procedure(HWND wnd, UINT msg, WPARAM wp, LPARAM lp);
// When you create a Window object, add a pointer to it in this map.
// Eg. if (this->hwnd != NULL) createdWindows[this->hwnd] = this;
static map<HWND, Window*> createdWindows;
// When you create a window, make this its procedure.
static LRESULT CALLBACK routeMessage(HWND wnd, UINT msg, WPARAM wp, LPARAM lp)
{
if (createdWindows.find(wnd) != createdWindows.end()) {
// Message belongs to one of our 'Window' objects.
// Pass the message to that window and return the result.
return createdWindows[wnd]->procedure(wnd, msg, wp, lp);
} else {
// It seems you made 'routeMessage' the procedure
// of a window that doesn't belong in the map. Go ahead
// and process the message in the default manner.
return DefWindowProc(wnd, msg, wp, lp);
}
}
};
现在您只需要一个消息循环和一个线程。我有一个使用 Lucid 的测试项目,它在单个线程上使用单个消息循环创建具有不同过程的 2 个窗口:
#include "Lucid.h"
using namespace Lucid;
void sayBye(MessageEventArgs& e)
{
MessageBox(NULL, "Goodbye!", "Form 2", MB_OK);
e.handled = true;
}
void Program::onStart()
{
Form* myForm1 = new Form("Hello World!");
myForm1->show();
Form* myForm2 = new Form("Hello World!");
myForm2->addMessageHandler(WM_CLOSE, sayBye);
myForm2->show();
// This Program::onStart() function is called
// immediately before the single message loop is entered.
}