我如何编程改变屏幕保护程序? [英] How do I change the screensaver programatically?
问题描述
我想改变当前屏保一个自定义的使用C#(我以前加载在Visual Studio中的资源)。怎么可能做到?我看着它在谷歌和SO,但所有谈判如何创建一个屏幕保护程序,而不是如何更改屏幕保护程序。如果可能的话,应该在WinXP,Vista和Windows 7的工作。
I'd like to change the current Screensaver for a custom one (which I previously loaded as a resource in Visual Studio) using C#. How could that be done? I've looked for it on Google and SO, but it all talks about "How to create a Screensaver", not "How to change a Screensaver". If possible, it should work on WinXP, Vista and 7.
推荐答案
我会回答我的一段代码疑问,工作对我说:
I'll answer my question with the piece of code that worked to me:
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());
}
}
}
然后,调用它时我的应用程序:
Then, when calling it from my application:
static string ResourcePath(string resource)
{
return Application.StartupPath + "\\Resources\\" + resource;
}
Program.Screensaver.Set(Program.ResourcePath("svr1.scr"));
我读的地方,我应该不再写一个名字超过8个字符(有点不可思议,但XP是所有类似这样的),所以我的屏幕被称为 svr1.scr
(不是真正面向对象的,但做的伎俩)
I read somewhere I should write a name no longer than 8 characters (a bit weird, but XP is all like this), so my screensaver is called svr1.scr
(not really object oriented, but does the trick)
这篇关于我如何编程改变屏幕保护程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!