내가 지금하고있는 것을보고, Lucid라는 애플리케이션 프레임 워크에서이 작업을 수행했습니다 (아직 진행중인 작업입니다). 대답을 위해 창 클래스는 APP
대신 Window
이라고합니다.
작성하는 모든 창에 글로벌 프로 시저를 전달하면됩니다. 모든 창은이 동일한 절차를 공유합니다. 모든 창에 메시지가 도착할 때마다 해당 메시지가 전역 프로 시저로 전송되고 전역 프로 시저에서는 HWND
이 사용자가 만든 Window
에 속하는지 확인한 다음 Window
의 절차에 메시지를 보냅니다. 이것이 어떻게 작동하는지에 대한 개요가 있습니다.
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 개의 창을 만드는 Lucid를 사용하는 테스트 프로젝트가 있습니다.
#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.
}
코드에 정의되지 않은 동작이 있습니다. 'App.push_back (& APP (nCmdShow))'하면 즉시 파기되는 임시 객체에 대한 포인터를 저장하고 있습니다. – juanchopanza
글쎄, 창문을 볼 수 있고, 메시지에 반응하고 있기 때문에 파괴되지 않는다. 부적절하게 코드화 된 경우 가능한 경우 알려 주시기 바랍니다. – bercik
@bercik "정의되지 않은 동작"은 작동하고있는 것처럼 보일 수 있으며 나중에 어떤 이유로 그 정의되지 않은 상태가 노출 될 때 몇 주가 지나야 만날 수 있습니다. 나는'& APP (nCmdShow)'가 컴파일되는 것에 놀랐다. 거기에'&'가 아닌'new'를 사용해야합니다. – molbdnilo