对于特定的应用程序,我需要在屏幕保护程序运行时禁用它.操作员可以手动将其关闭,然后再打开,但最简单的方法是在应用程序运行时保持屏幕保护程序不受影响.
我该怎么做呢?我找到了用SPI_SETSCREENSAVEACTIVE实际关闭屏幕保护程序的代码,但我不认为这就是我想要的.
[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 }
信用:P/Invoke,死点
theoldnewthing有你的答案:使用SetThreadExecutionState(ES_DISPLAY_REQUIRED)
.
这被视频播放器和PowerPoint使用.