开发者

Change Windows Wallpaper using .NET 4.0?

开发者 https://www.devze.com 2022-12-31 00:38 出处:网络
Is there a 开发者_如何学运维way to change the Windows wallpaper using some new feature in .NET 4?You can use SystemParametersInfo to set the desktop wallpaper. This should work consistently on all ver

Is there a 开发者_如何学运维way to change the Windows wallpaper using some new feature in .NET 4?


You can use SystemParametersInfo to set the desktop wallpaper. This should work consistently on all versions of windows that your app can run on, however will require some interop.

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. The details may have changed with newer versions of Windows, but the concept should be the same. Framework version should be irrelevant.

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! (on Windows 8 at least)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号