Home Music Photos Software Writings

MonitorSleep

MonitorSleep is a very simple Windows utility (the entire program code is contained in the box below). Its purpose is to make it easy to put your monitor into “sleep” mode — and get it back out again.

MonitorSleep.exe (Win32 executable; 24KB)

To use MonitorSleep, download the executable and save it somewhere out of the way, then create one or more shortcuts to it. To set the monitor to sleep after 5 seconds of no activity and display a dialog box (when you move the mouse or press a key) that will let you restore normal operation, just use a shortcut as is.

For other functions, you’ll need to modify a shortcut by right-clicking it, selecting Properties, and adding a parameter to the Target field on the Shortcut tab. Leave a space after the file name that’s already there and add:

0to set the monitor to “always on” (never times out)
a numberto set the monitor to time out after the specified number of seconds
the letter “L” followed by a number; e.g., L15to set the monitor to time out after the specified number of seconds and immediately log off

Example: you might place a shortcut named “Logoff Dark” on your Start menu that specifies MonitorSleep L15, and another shortcut that specifies MonitorSleep 0 in your Startup folder.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>

#pragma comment(lib, "powrprof.lib")
extern "C" {
#include <powrprof.h>
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow) {

   unsigned int ps;
   unsigned int timeout;
   POWER_POLICY pp;

   GetActivePwrScheme(&ps);
   ReadPwrScheme(ps, &pp);

   if (*lpCmdLine == 'l' || *lpCmdLine == 'L') {
      if (*(lpCmdLine+1)) {
         timeout = atoi(lpCmdLine+1);
         if (timeout && timeout < 5) timeout = 5;
         }
       else timeout = 5;
      pp.user.VideoTimeoutAc = timeout;
      SetActivePwrScheme(ps, 0, &pp);
      ExitWindows(0, 0);
      return 0;
      }
    else if (*lpCmdLine) {
      timeout = atoi(lpCmdLine);
      if (timeout && timeout < 5) timeout = 5;
      }
    else {
      timeout = pp.user.VideoTimeoutAc;
      pp.user.VideoTimeoutAc = 5;
      SetActivePwrScheme(ps, 0, &pp);
      MessageBox(0, "Monitor on 5 second time-out; \
click OK to restore normal operation.",
                 "Monitor Timeout", MB_OK);
      }

   pp.user.VideoTimeoutAc = timeout;
   SetActivePwrScheme(ps, 0, &pp);
   return 0;
   }