我想使用C#更改当前屏幕保护程序为自定义的屏幕保护程序(我之前在Visual Studio中作为资源加载过)。应该如何实现?我在Google和SO上搜索过,但都是关于“如何创建屏幕保护程序”,而不是“如何更改屏幕保护程序”。如果可能的话,它应该在WinXP、Vista和7上运行。
这段代码对我有效:
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
似乎没有公开该功能。 - CodesInChaos当安装新程序时,Windows 执行的命令是这个:
rundll32.exe desk.cpl,InstallScreenSaver %l