Tuesday, May 4, 2010

Turn off monitor in Windows

You can set up your computer to automatically turn off the monitor after idling for x minutes in power settings.
However, If you want to turn it off right now, what can you do?
-If the monitor has a power switch, just use it.
-Turn off your computer.
-My laptop does not have a power switch, and I want it to be running at night. Here is a simple solution.

Use SendMessage API to send WM_SYSCOMMAND message to HWND_BROADCAST

WM_SYSCOMMAND reference

LPARAM value
-1 - the display is powering on
1 - the display is going to low power
2 - the display is being shut off

#include <windows.h>
int main()
{
   Sleep(3000); // Wait for you to get rid of the mouse.
                //If you move the mouse, the monitor will turn on again.
   SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
   return 0;
}

It's time to go to bed.