gpt4 book ai didi

c# - Windows 如何决定显示屏幕保护程序

转载 作者:可可西里 更新时间:2023-11-01 11:08:57 25 4
gpt4 key购买 nike

Windows 有一个内部机制,通过检查用户交互性和其他任务(有人正在观看视频等)来决定何时显示屏幕保护程序(或关闭屏幕)。

是否有 Win API 允许我询问用户是否处于事件状态,或者他们最后一次处于事件状态是什么时候?

最佳答案

它被称为“空闲计时器”。您可以通过调用 CallNtPowerInformation() 来获取它的值,询问 SystemPowerInformation。返回的 SYSTEM_POWER_INFORMATION.TimeRemaining 字段告诉您空闲计时器还剩多少时间。 SystemExecutionState 请求会告诉您是否有任何线程调用了 SetThreadExecutionState() 来停止计时器,就像显示视频的应用所做的那样。

using System;
using System.Runtime.InteropServices;

public static class PowerInfo {
public static int GetIdleTimeRemaining() {
var info = new SYSTEM_POWER_INFORMATION();
int ret = GetSystemPowerInformation(SystemPowerInformation, IntPtr.Zero, 0, out info, Marshal.SizeOf(info));
if (ret != 0) throw new System.ComponentModel.Win32Exception(ret);
return info.TimeRemaining;
}

public static int GetExecutionState() {
int state = 0;
int ret = GetSystemExecutionState(SystemExecutionState, IntPtr.Zero, 0, out state, 4);
if (ret != 0) throw new System.ComponentModel.Win32Exception(ret);
return state;
}

private struct SYSTEM_POWER_INFORMATION {
public int MaxIdlenessAllowed;
public int Idleness;
public int TimeRemaining;
public byte CoolingMode;
}
private const int SystemPowerInformation = 12;
private const int SystemExecutionState = 16;
[DllImport("powrprof.dll", EntryPoint = "CallNtPowerInformation", CharSet = CharSet.Auto)]
private static extern int GetSystemPowerInformation(int level, IntPtr inpbuf, int inpbuflen, out SYSTEM_POWER_INFORMATION info, int outbuflen);
[DllImport("powrprof.dll", EntryPoint = "CallNtPowerInformation", CharSet = CharSet.Auto)]
private static extern int GetSystemExecutionState(int level, IntPtr inpbuf, int inpbuflen, out int state, int outbuflen);

}

关于c# - Windows 如何决定显示屏幕保护程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10970625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com