In a c++ program run on Win7, is there a way to fake a mouse movement or something like that, just to keep the screen saver from starting and the system from going to sleep? I'm looking for the minimal approach and I prefer not to use .NET. Thanks, -nuun
-
2why don't you change screen saver setting or your system power setting ? – Anil Vishnoi Sep 08 '10 at 07:06
-
1As a user of your program, _I_ want to decide myself whether I want a program's executing to be slowed down by some screen saver. If you are your own user, you can always change the system's settings yourself. – sbi Sep 08 '10 at 07:10
-
4Actually this is valid question. I think Windows Media Player does this when playing video and then it is justified (you don't want to stand up every five minutes and move your mouse just to enjoy your movie, don't you? also disabling screen saver just to watch one movie is overkill). – Tomek Sep 08 '10 at 07:13
-
2Same thing in Powerpoint. You don't want your laptop to fall asleep in the middle of a presentation just because you're taking the time to answer a question from the audience. I'll bet this alone was sufficient reason to add the SetThreadExecutionState() function. – MSalters Sep 08 '10 at 08:22
3 Answers
Don't mess with the screensaver settings, use SetThreadExecutionState. This is the API for informing windows on the fact that your application is active:
Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.
, and
Multimedia applications, such as video players and presentation applications, must use ES_DISPLAY_REQUIRED when they display video for long periods of time without user input

- 234,701
- 27
- 340
- 448
-
SetThreadExecutionState() is great for controlling system and display sleep, but it will *not* prevent the screen saver from launching. At least the function doc says it won't. – naor Sep 08 '10 at 19:04
-
5Correction: although the MSDN page for this functions states that "This function does not stop the screen saver from executing." calling with ES_DISPLAY_REQUIRED does in fact prevent the screen saver from triggering. – naor Sep 08 '10 at 19:41
-
3this is also the way to go (via p/invoke) if you are using .NET - http://pinvoke.net/default.aspx/kernel32.SetThreadExecutionState – Patrick Klug Mar 11 '11 at 00:08
That's not a bad idea, any decent media player does it... Look for SystemParametersInfo(SPI_SETSCREENSAVEACTIVE ...)
function in Win32 api, it should do the trick.

- 9,897
- 4
- 26
- 41
Before Windows starts screen-saver, it sends SC_SCREENSAVE notification in WM_SYSCOMMAND message to applications. If application wants to prevent screen-saver from starting, it should set "handled" flag to true and return zero during message processing. There is also SC_MONITORPOWER to prevent display from going to low power state.
https://learn.microsoft.com/en-us/windows/desktop/menurc/wm-syscommand

- 1
- 1