2011-12-05 74 views
5

我想使用C#将自定义的屏幕保护程序(我之前在Visual Studio中作为资源加载)更改为当前屏幕保护程序。这怎么可能完成?我已经在Google和SO上找到了它,但都是关于“如何创建屏幕保护程序”,而不是“如何更改屏幕保护程序”。如果可能的话,应该在WinXP,Vista的工作,7如何以编程方式更改屏幕保护程序?

+0

这一个可能是有用的: http://bytes.com/topic/c-sharp/answers/263953-setting-up-screensaver-via-csharp-c-application 你需要通过注册表 –

+0

感谢您的链接。这缓解了这些步骤。但是,它不适用于Windows XP(在Win7中它就像一个魅力)。你知道这件事吗? – Korcholis

回答

4

我会回答我的问题与一段代码,合作,我:

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(不是真正的面向对象,但没有窍门)

+1

丑陋,但我找不到更好的解决方案。 'desk.cpl'中的'InstallScreenSaver'后面显示控制面板UI,'SystemParametersInfo'似乎不公开此功能。 – CodesInChaos

0

这是Windows安装时执行该命令一个新

rundll32.exe desk.cpl,InstallScreenSaver %l 
相关问题