如何调用屏幕保护程序在Windows在C#中?屏幕保护程序、Windows

2023-09-03 04:23:26 作者:花开花败你已不在

我想调用用户的屏幕保护程序,如果这样的定义,在Windows环境。 我知道它可以使用纯C ++ code来完成(然后在C#包装为pretty的简单),所建议的此处。 尽管如此,出于好奇,我想知道如果这样的任务可以由纯粹的管理code。使用点网框架(2.0版以上)来完成,没有的P / Invoke并没有访问C ++的一面(这在转,可以使用Windows API pretty的容易)。

I'd like to invoke the user's screen saver if such is defined, in a Windows environment. I know it can be done using pure C++ code (and then the wrapping in C# is pretty simple), as suggested here. Still, for curiosity, I'd like to know if such task can be accomplished by purely managed code using the dot net framework (version 2.0 and above), without p/invoke and without visiting the C++ side (which, in turn, can use windows API pretty easily).

推荐答案

我的想法,我不知道如何始终如一这会工作,所以你需要研究了一下,我认为,但我希望它足够让你开始。

I've an idea, I'm not sure how consistently this would work, so you'd need to research a bit I think, but hopefully it's enough to get you started.

一个屏幕保护程序只是一个可执行文件,注册表存储此可执行文件的位置在 HKCU \控制面板\桌面\ SCRNSAVE.EXE

A screen saver is just an executable, and the registry stores the location of this executable in HKCU\Control Panel\Desktop\SCRNSAVE.EXE

在我的Vista的拷贝,这个工作对我来说:

On my copy of Vista, this worked for me:

RegistryKey screenSaverKey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop");
if (screenSaverKey != null)
{
    string screenSaverFilePath = screenSaverKey.GetValue("SCRNSAVE.EXE", string.Empty).ToString();
    if (!string.IsNullOrEmpty(screenSaverFilePath) && File.Exists(screenSaverFilePath))
    {
        Process screenSaverProcess = Process.Start(new ProcessStartInfo(screenSaverFilePath, "/s"));  // "/s" for full-screen mode
        screenSaverProcess.WaitForExit();  // Wait for the screensaver to be dismissed by the user
    }
}