Change Windows Wallpaper using .NET 4.0?

我们两清 提交于 2019-12-07 04:21:57

问题


Is there a way to change the Windows wallpaper using some new feature in .NET 4?


回答1:


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);



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/2886786/change-windows-wallpaper-using-net-4-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!