I want to turn off all monitors programmatically, as if the screen off time in Windows Settings had elapsed. This means, that any user input (e.g., keypress or mouse move) would turn the monitors back on.
I am looking for a solution that
- is not “somewhat of a hack”, like this answer;
- is as simple as possible: the mentioned answer links to Raymond Chen’s article, which suggests creating a dummy window, thus, let’s create the simplest window;
- is in C++.
Note, that the following program puts the computer to sleep instead of turning all monitors off, even though the documentation of SC_MONITORPOWER
says, (LPARAM) 2
has to turn them off.
#include <windows.h>
int main() {
SendNotifyMessageW(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
}
I found the above code by googling. The Google results contained SendMessage
instead of SendNotifyMessageW
. I replaced the function because I found that sometimes the call lasts long (see this question). Also note, that Raymond Chen discouraged broadcasting this message in his article.
1