Tuesday 11 June 2019

ChangeWallpaper.exe - Changes the desktop wallpaper from the command line

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.

Although this is from the 2001 documentation and has been removed from current.


Setting pvParam to "" removes the wallpaper. Setting pvParam to VBNULL reverts to the default wallpaper.

See docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow

REM Compiles ChangeWallpaper.vb to ChangeWallpaper.exe
REM To use
REM ChangeWallpaper Wallpaper.bmp
REM EG ChangeWallpaper "C:\Windows\Web\Wallpaper\Theme1\img1.jpg"
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "ChangeWallpaper.vb" /out:"%~dp0\ChangeWallpaper.exe" /target:winexe
pause



;ChangeWallpaper.vb
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Public Module SendWinKey
    Public Declare Unicode Function SystemParametersInfoW Lib "user32" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer
    Public Const SPI_SETDESKWALLPAPER = 20
    Public Const SPIF_SENDWININICHANGE = &H2
    Public Const SPIF_UPDATEINIFILE = &H1

Public Sub Main()    
    Dim Ret as Integer
    Dim FName As String
    'Takes a filename on the command line removing quotes
    FName = Replace(Command(), """", "")
    Ret = SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, FName, SPIF_SENDWININICHANGE + SPIF_UPDATEINIFILE)
    If Ret = 0 Then Msgbox(err.lastdllerror)
End Sub

End Module

No comments:

Post a Comment