Introduction
The SendMessage
function is useful to handle monitor states - the display is going to low power, the display is being shut off and the display is turned on.
Code explanation
While using SendMessage
function, you have to set four parameters:
hWnd
Handle to the window whose window procedure will receive the message. If you don't want to bother creating a window to send the message to, you can send the message to all top level windows (HWND_BROADCAST
) or you can use GetDesktopWindow
function sending the message to the desktop window.
Msg
Specifies the message to be sent (WM_SYSCOMMAND
).
wParam
Specifies additional message-specific information (SC_MONITORPOWER
).
lParam
- 1 - the display is going to low power.
- 2 - the display is being shut off.
- -1 - the display is being turned on (undocumented value).
Sleep(500);
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);
Sleep(500);
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 1);