Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Windows Wallpaper using .NET 4.0?

Tags:

c#

.net-4.0

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

like image 322
BrunoLM Avatar asked May 22 '10 03:05

BrunoLM


2 Answers

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);
like image 103
Chris Taylor Avatar answered Nov 17 '22 18:11

Chris Taylor


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

like image 22
Jason Avatar answered Nov 17 '22 20:11

Jason