views:

55

answers:

4

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

A: 

I am not too sure why you must resort to this. Simple disabling these options in the power setting will do.

What are you trying to achieve by doing that. Is your application a service. In case it is then you dont need to be bothering about this.

In case your application is a UI application also i cant think of a valid use case.

Eitherway we need more information.

ckv
+2  A: 

This is usually a particularly bad idea. The desktop belongs to the user of your application, not your application.

If I was running an application that disabled my screen saver (or moved around my desktop icons or added itself to my various quick-access toolbars) without my permission, it would be tossed out on its ear pretty quickly.

And, if you are the user, don't do it in your application. Change it manually like the rest of us :-)

If you must do it (and I urge you not to, but you may feel free to ignore that), I don't think the method has changed since NT. You use SystemParametersInfo with SPI_SETSCREENSAVEACTIVE to change the behaviour. These are in user32.dll from memory.

But I have a vague recollection of seeing problems reported with using that method under Win7 and I think the solution was a registry change, setting ScreenSaveActive under HKEY_CURRENT_USER\Control Panel\Desktop to 0 (and back to 1 when you're done).

paxdiablo
+1  A: 

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.

alxx
That's not what media players do. See my answer.
Damien_The_Unbeliever
+8  A: 

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

Damien_The_Unbeliever
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
Correction: 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