【问题标题】:How do I change the screensaver programmatically?如何以编程方式更改屏幕保护程序?
【发布时间】:2011-12-05 09:57:45
【问题描述】:

我想使用 C# 将当前屏幕保护程序更改为自定义屏幕保护程序(我之前在 Visual Studio 中作为资源加载)。那怎么可能呢?我已经在 Google 和 SO 上寻找它,但它都在谈论“如何创建屏幕保护程序”,而不是“如何更改屏幕保护程序”。如果可能,它应该可以在 WinXP、Vista 和 7 上运行。

【问题讨论】:

  • 这个可能有用:bytes.com/topic/c-sharp/answers/…你需要通过注册表来完成
  • 感谢您的链接。这简化了步骤。但是,它不适用于 Windows XP(在 Win7 中它就像一个魅力)。你知道吗?

标签: c# windows windows-screensaver


【解决方案1】:

我会用对我有用的代码来回答我的问题:

public sealed class Screensaver
{
    Screensaver() { }

    const int SPI_SETSCREENSAVEACTIVE = 0x0011;

    [DllImport("user32", CharSet=CharSet.Auto)]
    unsafe public static extern short SystemParametersInfo (int uiAction, int uiParam, int* pvParam, int fWinIni);

    public static void Set(string path)
    {
        try
        {
            RegistryKey oKey = Registry.CurrentUser.OpenSubKey("Control Panel",
            true);
            oKey = oKey.OpenSubKey("desktop", true);
            oKey.SetValue("SCRNSAVE.EXE", path);
            oKey.SetValue("ScreenSaveActive", "1");

            unsafe
            {
                int nX = 1;
                SystemParametersInfo(
                SPI_SETSCREENSAVEACTIVE,
                0,
                &nX,
                0
                );
            }
        }
        catch (Exception exc)
        {
            System.Windows.Forms.MessageBox.Show(exc.ToString());
        }
    }
}

然后,当从我的应用程序调用它时:

static string ResourcePath(string resource)
{
    return Application.StartupPath + "\\Resources\\" + resource;
}

Program.Screensaver.Set(Program.ResourcePath("svr1.scr"));

我在某处读到我应该写一个不超过 8 个字符的名称(有点奇怪,但 XP 都是这样的),所以我的屏幕保护程序称为 svr1.scr(不是真正面向对象,但可以解决问题)

【讨论】:

  • 丑陋,但我找不到更好的解决方案。 desk.cpl中的InstallScreenSaver之后会显示控制面板UI,而SystemParametersInfo似乎没有暴露该功能。
【解决方案2】:

这是windows安装新的时候执行的命令

rundll32.exe desk.cpl,InstallScreenSaver %l

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-11
    • 1970-01-01
    • 2016-02-20
    • 2010-10-26
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2010-11-25
    相关资源
    最近更新 更多