Home » Questions » Computers [ Ask a new question ]

How do I set the desktop background on Windows from a script?

How do I set the desktop background on Windows from a script?

On X Windows I had a cool 'silent-alarm" reminder script that would change my root window (background) color to solid red, just for a few seconds a few moments before changing it back. Is there a way to do this for Windows XP?

Asked by: Guest | Views: 76
Total answers/comments: 4
Guest [Entry]

"Here is one option. Create a small Console App with a SharpDevelop. Put this code into Programs.cs. I call the app ""CWP""; Change wallpaper. It takes just one parameter on command line: the file name. Tested on Windows 7 Ultimate 64-bit with .bmp -file.

/*
* Created by SharpDevelop.
* Date: 21.9.2012
* Time: 16:13
*/
using System;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;

namespace cwp
{

class Program
{
[DllImport(""user32.dll"")]
public static extern Int32 SystemParametersInfo(
UInt32 action, UInt32 uParam, String vParam, UInt32 winIni);

public static readonly UInt32 SPI_SETDESKWALLPAPER = 0x14;
public static readonly UInt32 SPIF_UPDATEINIFILE = 0x01;
public static readonly UInt32 SPIF_SENDWININICHANGE = 0x02;

public static void SetWallpaper(String path)
{
Console.WriteLine(""Setting wallpaper to '"" + path + ""'"");
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}

public static void Main(string[] args)
{
if (args.Length >= 1)
{
SetWallpaper( args[0] );
}
}
}
}"
Guest [Entry]

"For Windows 7, it works even in restricted areas!! ;) Replace your image location path with

C:\Users\1509967\Desktop\hi.jpg

reg add ""HKEY_CURRENT_USER\control panel\desktop"" /v wallpaper /t REG_SZ /d """" /f
reg add ""HKEY_CURRENT_USER\control panel\desktop"" /v wallpaper /t REG_SZ /d C:\Users\1509967\Desktop\hi.jpg /f
reg add ""HKEY_CURRENT_USER\control panel\desktop"" /v WallpaperStyle /t REG_SZ /d 2 /f
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
pause
exit"
"For Windows 7, it works even in restricted areas!! ;) Replace your image location path with

C:\Users\1509967\Desktop\hi.jpg

reg add ""HKEY_CURRENT_USER\control panel\desktop"" /v wallpaper /t REG_SZ /d """" /f
reg add ""HKEY_CURRENT_USER\control panel\desktop"" /v wallpaper /t REG_SZ /d C:\Users\1509967\Desktop\hi.jpg /f
reg add ""HKEY_CURRENT_USER\control panel\desktop"" /v WallpaperStyle /t REG_SZ /d 2 /f
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
pause
exit"
Guest [Entry]

"No matter I tried I couldn't reliably change wallpaper with regedit and UpdatePerUserSystemParameters (even with large loops), so I ended up using powershell, it works every time.

See www.joseespitia.com/2017/09/15/set-wallpaper-powershell-function/

Set-Wallpaper.ps1:

# use powershell.exe Set-Wallpaper.ps1 -Image ""<path to image>""

param ([string]$Image="""")

Function Set-WallPaper($Image) {
<#
.SYNOPSIS
Applies a specified wallpaper to the current user's desktop

.PARAMETER Image
Provide the exact path to the image

.EXAMPLE
Set-WallPaper -Image ""C:\Wallpaper\Default.jpg""

#>

Add-Type -TypeDefinition @""
using System;
using System.Runtime.InteropServices;

public class Params
{
[DllImport(""User32.dll"",CharSet=CharSet.Unicode)]
public static extern int SystemParametersInfo (Int32 uAction,
Int32 uParam,
String lpvParam,
Int32 fuWinIni);
}
""@

$SPI_SETDESKWALLPAPER = 0x0014
$UpdateIniFile = 0x01
$SendChangeEvent = 0x02

$fWinIni = $UpdateIniFile -bor $SendChangeEvent

$ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $fWinIni)
}

Set-WallPaper -Image $Image"
Guest [Entry]

"In this time, can use python, as it says changing desktop background in windows 10 via python:
By example:
import ctypes
path = 'C:\Windows\Web\Wallpaper\Windows\img0.jpg'
ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 3)"