Monday, November 23, 2009

Monitor Power Saver


Nearly all of us employ some power saving scheme while working with our PCs' like, switching off the monitors (physically); setting the screensavers to kick in after a preset duration, putting the system to stand-by or totally powering down the system.

Consider this scenario - you want to take a 5 minute break and your screensaver is set to kick in around 3 minutes. You don't want to put the system to stand-by or totally power down the system as the lag required to get the system up and working is bit annoying. (You may have set up an antivirus scan, update or download which you don't want to be bothered with while you power down). So, your best bet is to power down the monitor for the 5 min and switch it on when you are back or let the screensaver kick in (which would give you only (5-3) 2 minute of power saving).

Switching on and off (physically) the power switch of your monitor isn't a great idea, because on a long run it can damage the switch (Monitor switches aren't meant to take heavy duty switch on and off cycles. Present day monitors are so designed to be left on for hours, while the power saving strategy has to be implemented by the installed softwares).

Well, this was one question that has been bothering me quiet recently. Thankfully a quick google search took me to the right direction. Below I've provided a small program (in C++), that could help solve a situation like the one described above.

// PROGRAM TO TURN OFF MONITOR IN C++

#include
using namespace std;

int main()
{
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
return 0;
}

//EOF

The source code and executable can be downloaded here (6.40 KB).
(Open the link in new tab/window and save file as PowerSaver.rar)

A little light on the SendMessage function.

LRESULT SendMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to send
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

The SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message.

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 - Specifies additional message-specific information.
1 - the display is going to low power.
2 - the display is being shut off.
-1 - the display is being turned on (undocumented value).

This program should compile with almost any C compilers without any errors, to give you an efficient monitor friendly power management application.

When the program is executed it forces the display to go into 'power off' state and (program) is terminated. Upon mouse movement or keyboard activity, the display is restored.

Try linking the program with any key on your keyboard (multimedia keyboard) or put a shortcut to the program on your desktop or assign a keyboard shortcut for the program.

Thanks to all people out there who helped me with this program.