简体   繁体   English

使用.NET 4.0更改Windows Wallpaper?

[英]Change Windows Wallpaper using .NET 4.0?

有没有办法使用.NET 4中的一些新功能来更改Windows壁纸?

You can use SystemParametersInfo to set the desktop wallpaper. 您可以使用SystemParametersInfo设置桌面墙纸。 This should work consistently on all versions of windows that your app can run on, however will require some interop. 这应该在您的应用程序可以运行的所有Windows版本上一致地工作,但是需要一些互操作。

The following interop declarations are what you need 您需要以下互操作声明

public const int SPI_SETDESKWALLPAPER = 20;
public const int SPIF_UPDATEINIFILE = 1;
public const int SPIF_SENDCHANGE = 2;

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int SystemParametersInfo(
  int uAction, int uParam, string lpvParam, int fuWinIni);

Which can be used like this to change the desktop wallpaper 这可以用来改变桌面壁纸

SystemParametersInfo(
  SPI_SETDESKWALLPAPER, 0, "filename.bmp", 
  SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);

You set the wallpaper by updating the registry. 您可以通过更新注册表来设置壁纸。 Here's an article from 2006 explaining how to do it. 这是2006年的一篇文章,解释了如何做到这一点。 The details may have changed with newer versions of Windows, but the concept should be the same. 新版本的Windows可能会更改细节,但概念应该相同。 Framework version should be irrelevant. 框架版本应该是无关紧要的。

http://blogs.msdn.com/coding4fun/archive/2006/10/31/912569.aspx http://blogs.msdn.com/coding4fun/archive/2006/10/31/912569.aspx

Note that SystemParametersInfo will even return true, if the specified file does not exist! 请注意,如果指定的文件不存在,SystemParametersInfo甚至会返回true! (on Windows 8 at least) (至少在Windows 8上)

Plus you must give the full path to the file, not just a relative path. 另外,您必须提供文件的完整路径,而不仅仅是相对路径。

Also on windows 7 and above this will create a new theme, and will turn off picture shuffling of course. 此外,在Windows 7及更高版本上,这将创建一个新的主题,并将关闭图片洗牌当然。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM