增加了Windows里阻止系统电源选项里的系统睡眠,屏幕关闭API

This commit is contained in:
Sunny 2023-02-22 17:40:56 +08:00
parent 438b1cd0c7
commit 1dc2ec9b53
2 changed files with 65 additions and 0 deletions

View File

@ -37,6 +37,36 @@ namespace Sunny.UI
/// </summary> /// </summary>
public static class SystemEx public static class SystemEx
{ {
/// <summary>
/// 阻止系统电源选项里的系统睡眠,屏幕关闭。
/// </summary>
/// <param name="keepDisplayOn"></param>
public static void StartKeepDisplayOn(bool keepDisplayOn = true)
{
Kernel.SetThreadExecutionState(keepDisplayOn
? ExecutionState.Continuous | ExecutionState.SystemRequired | ExecutionState.DisplayRequired
: ExecutionState.Continuous | ExecutionState.SystemRequired);
}
/// <summary>
/// 恢复系统电源选项里的系统睡眠,屏幕关闭。
/// </summary>
public static void RestoreDisplayState()
{
Kernel.SetThreadExecutionState(ExecutionState.Continuous);
}
/// <summary>
/// 重置系统计时器,临时性阻止系统睡眠和屏幕关闭。此效果类似于手动使用鼠标或键盘控制了一下电脑。
/// </summary>
/// <param name="keepDisplayOn"></param>
public static void ResetDisplayState(bool keepDisplayOn = true)
{
Kernel.SetThreadExecutionState(keepDisplayOn
? ExecutionState.SystemRequired | ExecutionState.DisplayRequired
: ExecutionState.SystemRequired);
}
/// <summary> /// <summary>
/// 获取程序当前窗口的大小和位置 /// 获取程序当前窗口的大小和位置
/// </summary> /// </summary>

View File

@ -178,8 +178,43 @@ namespace Sunny.UI.Win32
uint BufferLength, IntPtr PreviousState, uint ReturnLength); uint BufferLength, IntPtr PreviousState, uint ReturnLength);
} }
[Flags]
public enum ExecutionState : uint
{
/// <summary>
/// Forces the system to be in the working state by resetting the system idle timer.
/// </summary>
SystemRequired = 0x01,
/// <summary>
/// Forces the display to be on by resetting the display idle timer.
/// </summary>
DisplayRequired = 0x02,
/// <summary>
/// This value is not supported. If <see cref="UserPresent"/> is combined with other esFlags values, the call will fail and none of the specified states will be set.
/// </summary>
[Obsolete("This value is not supported.")]
UserPresent = 0x04,
/// <summary>
/// Enables away mode. This value must be specified with <see cref="Continuous"/>.
/// <para />
/// 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.
/// </summary>
AwaymodeRequired = 0x40,
/// <summary>
/// Informs the system that the state being set should remain in effect until the next call that uses <see cref="Continuous"/> and one of the other state flags is cleared.
/// </summary>
Continuous = 0x80000000,
}
public partial class Kernel public partial class Kernel
{ {
[DllImport("kernel32")]
public static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 CompareStringEx(string localeName, int flags, string str1, int count1, string str2, public static extern Int32 CompareStringEx(string localeName, int flags, string str1, int count1, string str2,
int count2, IntPtr versionInformation, IntPtr reserved, int param); int count2, IntPtr versionInformation, IntPtr reserved, int param);