diff --git a/SunnyUI/Common/USystem.cs b/SunnyUI/Common/USystem.cs
index 671f20e6..fa03326f 100644
--- a/SunnyUI/Common/USystem.cs
+++ b/SunnyUI/Common/USystem.cs
@@ -37,6 +37,36 @@ namespace Sunny.UI
///
public static class SystemEx
{
+ ///
+ /// 阻止系统电源选项里的系统睡眠,屏幕关闭。
+ ///
+ ///
+ public static void StartKeepDisplayOn(bool keepDisplayOn = true)
+ {
+ Kernel.SetThreadExecutionState(keepDisplayOn
+ ? ExecutionState.Continuous | ExecutionState.SystemRequired | ExecutionState.DisplayRequired
+ : ExecutionState.Continuous | ExecutionState.SystemRequired);
+ }
+
+ ///
+ /// 恢复系统电源选项里的系统睡眠,屏幕关闭。
+ ///
+ public static void RestoreDisplayState()
+ {
+ Kernel.SetThreadExecutionState(ExecutionState.Continuous);
+ }
+
+ ///
+ /// 重置系统计时器,临时性阻止系统睡眠和屏幕关闭。此效果类似于手动使用鼠标或键盘控制了一下电脑。
+ ///
+ ///
+ public static void ResetDisplayState(bool keepDisplayOn = true)
+ {
+ Kernel.SetThreadExecutionState(keepDisplayOn
+ ? ExecutionState.SystemRequired | ExecutionState.DisplayRequired
+ : ExecutionState.SystemRequired);
+ }
+
///
/// 获取程序当前窗口的大小和位置
///
diff --git a/SunnyUI/Win32/Win32.Added.cs b/SunnyUI/Win32/Win32.Added.cs
index 8ca383d4..8f159535 100644
--- a/SunnyUI/Win32/Win32.Added.cs
+++ b/SunnyUI/Win32/Win32.Added.cs
@@ -178,8 +178,43 @@ namespace Sunny.UI.Win32
uint BufferLength, IntPtr PreviousState, uint ReturnLength);
}
+ [Flags]
+ public enum ExecutionState : uint
+ {
+ ///
+ /// Forces the system to be in the working state by resetting the system idle timer.
+ ///
+ SystemRequired = 0x01,
+
+ ///
+ /// Forces the display to be on by resetting the display idle timer.
+ ///
+ DisplayRequired = 0x02,
+
+ ///
+ /// This value is not supported. If is combined with other esFlags values, the call will fail and none of the specified states will be set.
+ ///
+ [Obsolete("This value is not supported.")]
+ UserPresent = 0x04,
+
+ ///
+ /// Enables away mode. This value must be specified with .
+ ///
+ /// Away mode should be used only by media-recording and media-distribution applications that must perform critical background processing on desktop computers while the computer appears to be sleeping.
+ ///
+ AwaymodeRequired = 0x40,
+
+ ///
+ /// Informs the system that the state being set should remain in effect until the next call that uses and one of the other state flags is cleared.
+ ///
+ Continuous = 0x80000000,
+ }
+
public partial class Kernel
{
+ [DllImport("kernel32")]
+ public static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags);
+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 CompareStringEx(string localeName, int flags, string str1, int count1, string str2,
int count2, IntPtr versionInformation, IntPtr reserved, int param);