Original Post
I am attempting (keyword) to write the message router in the windows wrapper tutorial. The code compiles correctly but the messages are not being routed correctly... when alt-F4 or use the system menu to close the program it still runs.
Here''s the code:
// Window''s main loop
LRESULT CWND::MainLoop()
{
bool done = false;
// Main message loop
while(!done)
{
PeekMessage(&m_msg, m_hwnd, 0, 0, PM_REMOVE);
// If WM_QUIT
if(m_msg.message == WM_QUIT)
{
done = true;
}
else
{
// Translate & dispatch to even quene
TranslateMessage(&m_msg);
DispatchMessage(&m_msg);
// Main game logic cycle
m_GameCycle();
}
}
return m_msg.wParam;
}
// Class message router: this function is static and will rout messages
// to the correct class instance
LRESULT CALLBACK CWND::MessageRouter(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
CWND *p_wnd = 0;
if(msg == WM_NCCREATE)
{
// retrieve Window instance from window creation data and associate
LPVOID p_temp = ((LPCREATESTRUCT)lParam)->lpCreateParams;
p_wnd = reinterpret_cast(p_temp);
SetWindowLong(hwnd, GWL_USERDATA, reinterpret_cast(p_wnd));
// save window handle
if(p_wnd)
p_wnd->SetHWND(hwnd);
}
else
{
// retrieve associated Window instance
p_wnd = reinterpret_cast(GetWindowLong(hwnd, GWL_USERDATA));
}
if(p_wnd)
{
t_messageIterator i;
i = p_wnd->GetMessageHandler(msg);
if(i == NULL)
{ MessageBox(NULL, "Message not recognized!", "ERROR", MB_OK); }
else
{ return (i->second)(p_wnd, hwnd, wParam, lParam); }
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Any suggestions?