开发者

Disabling Screen Saver and Power Options in C#

开发者 https://www.devze.com 2022-12-20 06:58 出处:网络
I am writing an application in C# that plays a movie. I need to figure out how to disable the screen saver and power options using C#.

I am writing an application in C# that plays a movie. I need to figure out how to disable the screen saver and power options using C#.

I know 开发者_开发百科the Windows SDK API has a function called SetThreadExecutionState() which can be used to do this, however, I do not know if there is a better way to do it. If not, how do I incorporate this function into C#?


Not sure if there is a better .NET solution but here is how you could use that API:

The required usings:

using System.Runtime.InteropServices;

The P/Invoke:

public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
public const uint ES_DISPLAY_REQUIRED = 0x00000002;

[DllImport("kernel32.dll", SetLastError = true)]
public static extern uint SetThreadExecutionState([In] uint esFlags);

And then disable screensaver by:

SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED);

Finnaly enable screensaver by reseting the execution state back to original value:

SetThreadExecutionState(ES_CONTINUOUS);

Note that I just picked one of the flags at random in my example. You'd need to combine the correct flags to get the specific behavior you desire. You will find the description of flags on MSDN.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号