how do i prevent screen-savers and sleeps during my program execution?

怎甘沉沦 提交于 2020-01-19 05:40:11

问题


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


回答1:


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




回答2:


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.




回答3:


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).




回答4:


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://docs.microsoft.com/en-us/windows/desktop/menurc/wm-syscommand




回答5:


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.



来源:https://stackoverflow.com/questions/3665332/how-do-i-prevent-screen-savers-and-sleeps-during-my-program-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!