The WinAPI documentation for the PeekMessageW function says:

Your messages are retrieved. The window thread must belong to the current thread.

Well, I tried setting the single-thread mode:

 int main(int argc, char* argv[]) { FIX_USE(argc) FIX_USE(argv) const HRESULT result = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); if(!SUCCEEDED(result)) { std::cout << "Failed to initialize single thread apartment..." << std::endl; return 0x1; } try { window window(L"Window"); window.show(); window.set_client_size(size(1280, 720)); MSG msg = {}; while (WM_QUIT != msg.message) { if(PeekMessageW(&msg, window.get_handle(), 0u, 0u, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessageW(&msg); } // TODO: update and render methods call here } CoUninitialize(); return 0x0; } catch (const std::runtime_error& e) { std::cout << e.what() << std::endl; return 0x2; } } 

Also in the note section it says:

Note that you will receive messages for you and not for wMsgFilterMax.

But it is not so! The WM_QUIT message WM_QUIT never be received.

While in the window procedure:

 LRESULT window::window_procedure(HWND h_wnd, UINT msg, WPARAM w_param, LPARAM l_param) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); break; case WM_SIZE: on_size_changed(size(LOWORD(l_param), HIWORD(l_param))); break; default: return DefWindowProcW(h_wnd, msg, w_param, l_param); } return LRESULT(FALSE); } 

And the last message retrieved is: WM_NCLBUTTONDOWN .

What could be the problem of receiving this message, or did I set the single-thread mode incorrectly? Or is the window not in this stream?

    1 answer 1

    PeekMessageW works only with windows created in this thread, CoInitializeEx has nothing to do with it. PostQuitMessage sends a message to the message queue of the thread, not to a specific window. And by specifying window.get_handle() only messages addressed to this window and its child windows will be window.get_handle() .