2012-05-18 150 views
0

我在我的節目此消息循環:無限的窗口消息循環

while (true) { 
    if (PeekMessage(&msg, window, 0, 0, PM_REMOVE)) { 
     if (msg.message == WM_QUIT) { 
      MessageBox(NULL, L"Quit", L"", 0); 
      break; 
     } 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } else { 
     Render(); 
    } 
} 

這個循環永遠不會結束。即使主窗口消失,它也不會顯示消息框。 這裏是WndProc的代碼:

switch (msg) { 

    case WM_CLOSE : 
     DestroyWindow(hwnd); 
     break; 

    case WM_DESTROY : 
     PostQuitMessage(0); 
     break; 

    default : 
     return DefWindowProc(hwnd, msg, wParam, lParam); 
     break; 
} 

return 0; 

有人能幫助我嗎?我從字面上拉我的頭髮。

+0

您是否試過使用[Spy ++](http://msdn.microsoft.com/zh-cn/library/aa264396(v = vs.60).aspx)或類似工具跟蹤郵件? – kichik

回答

9

你在致電PeekMessage(&msg, window, ...)。如果window不是NULL,則永遠不會得到WM_QUIT,因爲WM_QUIT未與窗口關聯。

取而代之,只需撥打PeekMessage/GetMessageNULLHWND。如有必要,DispatchMessage會將其發送到右邊的WndProc。 (通常,making GetMessage/PeekMessage filter by HWND is a bad idea.