I have a Visual Studio 2008 C++ project for Windows 7 where I would like to be notified of power state transitions (e.g. suspend, hibernate, resume, etc…). I have created a message-only window that watches for WM_POWERBROADCAST messages. Once the window is created, I suspend the PC.
For example (error checking omitted for brevity):
const TCHAR class_name[] = _T( "Power State Monitor" );
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof( WNDCLASSEX );
wc.lpfnWndProc = PowerStateMonitor;
wc.lpszClassName = class_name;
::RegisterClassEx( &wc );
::CreateWindowEx( 0, class_name, class_name, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, wc.hInstance, NULL );
::SetSuspendState( FALSE, FALSE, FALSE );
The WindowProc looks like this:
LRESULT CALLBACK PowerStateMonitor( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if( uMsg == WM_POWERBROADCAST )
{
ATLTRACE( L"WM_POWERBROADCAST:\r\n");
return TRUE;
}
ATLTRACE( L"Default Handler: %#08x\r\n", uMsg );
return ::DefWindowProc( hwnd, uMsg, wParam, lParam );
}
I would expect to see WM_POWERBROADCAST: logged, but instead all I see are the typical window creation messages:
Default Handler: WM_GETMINMAXINFO
Default Handler: WM_NCCREATE
Default Handler: WM_NCCALCSIZE
Default Handler: WM_CREATE
Can anybody suggest what I can change to have my window correctly receive power change notifications?
Message only windows do not receive broadcast messages. You will have to use a hidden, normal window instead. From MSDN, emphasis mine: