1

This vbs works fine with XP, but have problem with Windows 7, the wallpaper change wont take effect unless you login again. Is there a way to redraw desktop immediately? thanks

Set w = WScript.CreateObject("Wscript.Shell")
filePath = "D:\wp.bmp"
w.RegWrite "HKCU\Control Panel\Desktop\Wallpaper", filePath
w.Run "%windir%\System32\RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters", 1, True

visitantz
  • 161
  • 2
  • 8
  • from windows Vista this approach to wallpaper changes does not work. Or at least not as intended. The changes to the registry will apply on new logon. The only robust way to get the wallpaper changed instantly is to use the windows api. You can get [here](http://stackoverflow.com/a/9440226/2861476) what you need – MC ND Nov 01 '13 at 12:00

1 Answers1

4

Thank you guys, as you said Powershell pInvoke method is indeed the only workable solution I could find on web, so I copy it here in case someone gets stuck into the same problem.

Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
   public class Setter {
      public const int SetDesktopWallpaper = 20;
      public const int UpdateIniFile = 0x01;
      public const int SendWinIniChange = 0x02;
      [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
      private static extern int SystemParametersInfo (int uAction, int uParam, string lpvParam, int fuWinIni);
      public static void SetWallpaper ( string path) {
         SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
         RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
         key.Close();
      }
   }
}
"@

[Wallpaper.Setter]::SetWallpaper('D:\wp1.bmp')
visitantz
  • 161
  • 2
  • 8