From 1dc2ec9b5395ed3b23044a28423d817e7e75ae43 Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 22 Feb 2023 17:40:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86Windows=E9=87=8C?= =?UTF-8?q?=E9=98=BB=E6=AD=A2=E7=B3=BB=E7=BB=9F=E7=94=B5=E6=BA=90=E9=80=89?= =?UTF-8?q?=E9=A1=B9=E9=87=8C=E7=9A=84=E7=B3=BB=E7=BB=9F=E7=9D=A1=E7=9C=A0?= =?UTF-8?q?=EF=BC=8C=E5=B1=8F=E5=B9=95=E5=85=B3=E9=97=ADAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Common/USystem.cs | 30 ++++++++++++++++++++++++++++++ SunnyUI/Win32/Win32.Added.cs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) 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);