需要在 Windows C#/.Net 中禁用屏幕保护程序/屏幕锁定

发布于 2024-07-07 05:53:35 字数 1453 浏览 11 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

奢华的一滴泪 2024-07-14 05:53:35

编辑 - 我在这里使用现代电源可用性请求 API(取代 SetThreadExecutionState)更新了答案: https://stackoverflow.com/a/63632916/67824

[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死点

EDIT - I have an updated answer using the modern Power Availability Request API (supersedes SetThreadExecutionState) here: https://stackoverflow.com/a/63632916/67824

[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
}

Credit: P/Invoke, deadpoint

星星的轨迹 2024-07-14 05:53:35

theoldnewthing 有您的答案:使用 SetThreadExecutionState(ES_DISPLAY_REQUIRED )

这是由视频播放器和 PowerPoint 使用的。

theoldnewthing has your answer: Use SetThreadExecutionState(ES_DISPLAY_REQUIRED).

This is used by video players and PowerPoint.

爱*していゐ 2024-07-14 05:53:35
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
            | EXECUTION_STATE.ES_DISPLAY_REQUIRED 
            | EXECUTION_STATE.ES_SYSTEM_REQUIRED);

这对 XP 没有帮助。

事实上,这个函数在不同的 Windows 版本之间不能交叉操作(尽管它在 Windows Vista 或更高版本中工作得很好)...在 Windows XP / 2003 中,应使用 ES_USER_PRESENT | 调用此函数。 ES_CONTINUOUS(两者都应该被调用)...这将定期重置系统和显示空闲计时器...在其他 Windows 版本中,建议您使用 ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS | ES_CONTINUOUS ES_AWAYMODE_REQUIRED...

SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
            | EXECUTION_STATE.ES_DISPLAY_REQUIRED 
            | EXECUTION_STATE.ES_SYSTEM_REQUIRED);

This is not helpful on XP.

In fact, this function is not cross operable between diferent Windows versions (although it works pretty well in Windows Vista or higher)... In Windows XP / 2003 this function shall be called with ES_USER_PRESENT | ES_CONTINUOUS (both should be called)... This will reset periodically both system and display idle timers... In other Windows versions, it's recommended that you use ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS | ES_AWAYMODE_REQUIRED...

三生路 2024-07-14 05:53:35

SystemParametersInfo 和 SPI_SETSCREENSAVEACTIVE 是执行此操作的正常方法。 但是,它不会禁用屏幕锁定。

SystemParametersInfo with SPI_SETSCREENSAVEACTIVE is the normal way to do this. However, it doesn't disable screen locking.

青瓷清茶倾城歌 2024-07-14 05:53:35

尝试将您的表单设置为最高值true,它对我有用,即使在空闲时间之后,屏幕保护程序也不会出现...

try making your form topmost value true it works for me screen-saver never came even after the idle time...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文