Why does GetMessage not process WM_POWERBROADCAST messages?

493 views Asked by At

I'm creating a hidden window for the purpose of handling messages. I'm experiencing that I do not receive WM_POWERBROADCAST messages in it's GetMessage loop. I do, however, receive it via my WNDPROC. I have confirmed that I do receive other messages in both locations.

Why is GetMessage not receiving WM_POWERBROADCAST?

WNDCLASSEX classInfo = {0};
classInfo.cbSize = sizeof(classInfo);
classInfo.style = WS_DISABLED;
// CustomWndProc just outputs the message and chains to DefaultWndProc
classInfo.lpfnWndProc = CustomWndProc; 
classInfo.hInstance = GetModuleHandle(NULL);
classInfo.hCursor = NULL;
classInfo.hbrBackground = NULL;
classInfo.lpszMenuName = NULL;
classInfo.lpszClassName = L"MyMessageWindow";
ATOM windowClass = RegisterClassEx(&classInfo);

HWND messageWindow = CreateWindowEx(WS_EX_NOACTIVATE, L"MyMessageWindow", 
    L"Message Handling Window", WS_DISABLED, 0, 0, 0, 0, 0, NULL, 
    GetModuleHandle(NULL), NULL);

MSG message;
while (GetMessage(&message, NULL, 0, 0))
{
    // This condition is never true.
    if (message.message == WM_POWERBROADCAST)
        std::cout << "Got WM_POWERBROADCAST" << std::endl;
}
1

There are 1 answers

4
David Heffernan On BEST ANSWER

That's because WM_POWERBROADCAST is a dispatched synchronously and so is not placed on the message queue.

In order for you to process it you need to handle it in your window procedure.