Skip to content

Commit

Permalink
Turn off screensavers. Note that this won't work on Vista+ when there…
Browse files Browse the repository at this point in the history
…'s a password.
  • Loading branch information
hrydgard committed May 2, 2013
1 parent 51493ab commit 0ccebac
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Windows/WndMainWindow.cpp
Expand Up @@ -746,6 +746,21 @@ namespace MainWindow
UpdateMenus();
break;

// Turn off the screensaver.
// Note that if there's a screensaver password, this simple method
// doesn't work on Vista or higher.
case WM_SYSCOMMAND:
{
switch (wParam)
{
case SC_SCREENSAVE:
return 0;
case SC_MONITORPOWER:
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
Expand Down

2 comments on commit 0ccebac

@PeterTh
Copy link
Contributor

@PeterTh PeterTh commented on 0ccebac May 2, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have code somewhere to disable the screen saver and sleep (also on Vista and higher when there is a password) ...

Right, here it is:

class KeepDisplayOn {
    UINT prevScreenSaver;
    EXECUTION_STATE prevExecState;
public:
    KeepDisplayOn() {
        prevExecState = SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
        SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &prevScreenSaver, 0);
        SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, FALSE, NULL, 0);
    }

    ~KeepDisplayOn() {
        SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, prevScreenSaver, NULL, 0);
        SetThreadExecutionState(prevExecState);
    }
};

You just put one instance of that on the stack in main -- at least that's what I do in another project. The SystemParametersInfo stuff is somewhat bad practice, but I don't know of a reliable method which is good practice -- you'd expect SetThreadExecutionState(ES_DISPLAY_REQUIRED ...) to disable the screensaver, but it doesn't, it only disables turning off the screen for power saving.

@shinra358
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bogus mouse event option in this program keeps the screensaver off, laptop screen dimming off, and hibernation off when you have a password on. Maybe you can check the source code to see how to implement.

http://code.google.com/p/magical-tools/downloads/detail?name=Insomnia%20r116.zip&can=2&q=

It is also implemented in this plugin when prevent screensaver is checked:

http://code.google.com/p/magical-tools/downloads/detail?name=Pokopom%20r127.zip&can=2&q=

Please sign in to comment.