需要在 Windows C#/.Net 中禁用屏幕保护程序/屏幕锁定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/241222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need to disable the screen saver / screen locking in Windows C#/.Net
提问by Clinton Pierce
For a particular application, I need the screen saver to be disabled while it's running. The operator COULD manually turn it off, and then back on later, but the easiest thing to do would be to just keep the screen saver at bay while the application is running.
对于特定的应用程序,我需要在它运行时禁用屏幕保护程序。操作员可以手动关闭它,然后稍后再打开,但最简单的方法是在应用程序运行时保持屏幕保护程序处于关闭状态。
How do I do this? I've found code for actually turning off the screen saver with SPI_SETSCREENSAVEACTIVE, but I don't think that's what I want.
我该怎么做呢?我找到了使用 SPI_SETSCREENSAVEACTIVE 实际关闭屏幕保护程序的代码,但我认为这不是我想要的。
采纳答案by Serafeim
theoldnewthinghas your answer: Use SetThreadExecutionState(ES_DISPLAY_REQUIRED)
.
theoldnewthing有你的答案:使用SetThreadExecutionState(ES_DISPLAY_REQUIRED)
.
This is used by video players and PowerPoint.
这是由视频播放器和 PowerPoint 使用的。
回答by Jasper Bekkers
SystemParametersInfo with SPI_SETSCREENSAVEACTIVE is the normal way to do this. However, it doesn't disable screen locking.
带有 SPI_SETSCREENSAVEACTIVE 的 SystemParametersInfo 是执行此操作的正常方法。但是,它不会禁用屏幕锁定。
回答by deepu
try making your form topmost value trueit works for me screen-saver never came even after the idle time...
尝试使您的表单最高值成为真的它对我有用 - 即使在空闲时间之后屏幕保护程序也从未出现......
回答by Ohad Schneider
[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
ES_SYSTEM_REQUIRED = 0x00000001,
ES_DISPLAY_REQUIRED = 0x00000002,
// Legacy flag, should not be used.
// ES_USER_PRESENT = 0x00000004,
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
}
public static class SleepUtil
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
}
public void PreventSleep()
{
if (SleepUtil.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
| EXECUTION_STATE.ES_DISPLAY_REQUIRED
| EXECUTION_STATE.ES_SYSTEM_REQUIRED
| EXECUTION_STATE.ES_AWAYMODE_REQUIRED) == 0) //Away mode for Windows >= Vista
SleepUtil.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
| EXECUTION_STATE.ES_DISPLAY_REQUIRED
| EXECUTION_STATE.ES_SYSTEM_REQUIRED); //Windows < Vista, forget away mode
}
回答by AVladislav
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
| EXECUTION_STATE.ES_DISPLAY_REQUIRED
| EXECUTION_STATE.ES_SYSTEM_REQUIRED);
This is not helpful on XP.
这对 XP 没有帮助。
In fact, this function is not cross operable between diferent Windows versions (although it works pretty well in Windows Vista or higher)... In Windows XP / 2003 this function shall be called with ES_USER_PRESENT | ES_CONTINUOUS (both should be called)... This will reset periodically both system and display idle timers... In other Windows versions, it's recommended that you use ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS | ES_AWAYMODE_REQUIRED...
事实上,这个函数不能在不同的 Windows 版本之间交叉操作(虽然它在 Windows Vista 或更高版本中运行良好)...在 Windows XP / 2003 中,这个函数应该用 ES_USER_PRESENT | 调用。ES_CONTINUOUS(都应该被调用)...这将定期重置系统和显示空闲计时器...在其他Windows版本中,建议您使用ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS | ES_AWAYMODE_REQUIRED...