简体   繁体   English

如果 Windows 屏幕保护程序使用 C# 运行,如何关闭它?

[英]How do I turn off the windows screen saver if it is running using C#?

Ok, so I found some code to check if a screensaver is running and kill it if I want to.好的,所以我找到了一些代码来检查屏幕保护程序是否正在运行,如果我愿意,可以将其杀死。 This doesn't seem to work on Windows 7 PCs.这似乎不适用于 Windows 7 PC。 Does anyone know how this code can be modified, or provide new code to accomplish this?有谁知道如何修改此代码,或提供新代码来完成此操作?

My application is designed to run in the background until a particular event occurs, and then create and display a full screen notification.我的应用程序设计为在后台运行,直到发生特定事件,然后创建并显示全屏通知。 This needs to be displayed even if a screen saver is currently up.即使屏幕保护程序当前已启动,也需要显示此信息。

最简单的方法是通过调用 SendInput() 来伪造 x=y=0 的鼠标移动事件。

The following question may provide some insight:以下问题可能会提供一些见解:

How to turn screensaver on (windows 7) by a code (in cmd)? 如何通过代码(在 cmd 中)打开屏幕保护程序(Windows 7)?

However, what if the machine is locked?但是,如果机器被锁定怎么办? I don't think you'll be able to display any application on top of the lock screen unless it is actually a screensaver (and even then it may not be possible)我不认为你将能够在锁定屏幕上显示任何应用程序,除非它实际上是一个屏幕保护程序(即使这样也可能不可能)

假设您没有受密码保护的屏幕保护程序:(来自http://support.microsoft.com/kb/140723

PostMessage (GetActiveWindow(), WM_CLOSE, 0, 0L);

Use SetThreadExecutionState this winAPI to tell the operating system that the thread is in use, even if the user is not interacting with the computer.使用SetThreadExecutionState这个 winAPI 告诉操作系统线程正在使用中,即使用户没有与计算机交互。 These will prevent to appear screen saver and stop the machine from being suspended automatically.这些将防止出现屏幕保护程序并阻止机器自动暂停。

Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入睡眠或关闭显示器。

There are series of flags to specify a new state for the current thread:有一系列标志可以为当前线程指定新状态:

  • ES_AWAYMODE_REQUIRED (0x00000040) : Enables away mode. ES_AWAYMODE_REQUIRED (0x00000040) :启用离开模式。
  • ES_DISPLAY_REQUIRED (0x00000002) : Forces the display to be on by resetting the display idle timer. ES_DISPLAY_REQUIRED (0x00000002) :通过重置显示器空闲计时器强制显示器开启。
  • ES_SYSTEM_REQUIRED (0x00000001) : Forces the system to be in the working state by resetting the system idle timer. ES_SYSTEM_REQUIRED (0x00000001) :通过重置系统空闲定时器强制系统处于工作状态。
  • ES_CONTINUOUS (0x80000000) : Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared. ES_CONTINUOUS (0x80000000) :通知系统设置的状态应该保持有效,直到下一次使用 ES_CONTINUOUS 和其他状态标志之一的调用被清除。

As this is an winAPI, so you have to PInvoke this :因为这是一个 winAPI,所以你必须PInvoke这个:

[DllImport("kernel32.dll", CharSet = CharSet.Auto,SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

User-Defined Types:用户定义的类型:

[FlagsAttribute]
public enum EXECUTION_STATE :uint
{
   ES_AWAYMODE_REQUIRED = 0x00000040,
   ES_CONTINUOUS = 0x80000000,
   ES_DISPLAY_REQUIRED = 0x00000002,
   ES_SYSTEM_REQUIRED = 0x00000001
}

Here below is the calling procedure: 这里下面是调用过程:

//To stop screen saver and monitor power off event
//You can combine several flags and specify multiple behaviors with a single call
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);

//To reset or allow those event again you have to call this API with only ES_CONTINUOUS
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);//This will reset as normal

According to MSDN this API is safe to use.根据MSDN,这个 API 可以安全使用。

The system maintains a count of applications that have called SetThreadExecutionState.系统维护已调用 SetThreadExecutionState 的应用程序计数。 The system tracks each thread that calls SetThreadExecutionState and adjusts the counter accordingly.系统会跟踪调用 SetThreadExecutionState 的每个线程并相应地调整计数器。 If this counter reaches zero and there has not been any user input, the system enters sleep.如果此计数器达到零且没有任何用户输入,则系统进入睡眠状态。

If the Application crashed before resetting flag, the System will adjust and will reset automatically.如果应用程序在重置标志之前崩溃,系统将进行调整并自动重置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM