【问题标题】:How can i prevent my windows from showing screensaver or powering down from my application?如何防止我的窗口显示屏幕保护程序或关闭我的应用程序?
【发布时间】:2016-03-28 10:02:56
【问题描述】:

我有一个数字标牌应用程序,我想在计算机上优先运行它。我现在已经连接到 win32 事件 (ES_DISPLAY_REQUIRED) 以防止这种情况发生。但是我想摆脱这个 win32 调用,因为我认为那是提升我的应用程序权限的调用,因此它需要以管理员身份运行。

有没有管理的方式来做同样的事情?我的意思是,当你看电影时,媒体播放器等在做什么来防止睡眠/屏幕保护程序?

【问题讨论】:

    标签: c#


    【解决方案1】:

    你可以尝试像这样使用 SetThreadExecutionState 函数:

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern uint SetThreadExecutionState(uint esFlags);
    public const uint ES_CONTINUOUS = 0x80000000;
    public const uint ES_SYSTEM_REQUIRED = 0x00000001;
    public const uint ES_AWAYMODE_REQUIRED = 0x00000040;
    
    public void AllowStandbyMode(bool allow)
    {
        if (allow)
        {
            // Set state back to normal
            SetThreadExecutionState(ES_CONTINUOUS);
        }
        else
        {
            // Try this for vista, it will fail on XP
            if (SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED) == 0)
            {
                // Try XP variant as well just to make sure 
                SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
            }    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-04
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      相关资源
      最近更新 更多