Jump to content

Set Wallpaper Based On Screen Resolution via Scrip - Windows 10


Recommended Posts

Posted

Hi,

 

I have used the below script in Windows 7 and & and it has worked fine. It will set the wallpaper based on the resolution of the screen so we don't end up getting stretched images.

 

However it doesn't work in Windows 10. Does anyone have any thoughts please?

 

Thanks

 

vberror.PNG

 

Set objWMIService = GetObject("Winmgmts:\\.\root\cimv2") 
Set objSh = CreateObject("Wscript.Shell")
Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor where DeviceID = 'DesktopMonitor1'",,0) 
For Each objItem in colItems 
   intHorizontal = objItem.ScreenWidth 
   intVertical = objItem.ScreenHeight 
Next

strResolution = CStr(intHorizontal) & " x " & CStr(intVertical)

Select Case strResolution
Case "800 x 600"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1024x768.jpg"
Case "1024 x 768"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1024x768.jpg"
Case "768 x 1280"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\768x1280.jpg"
Case "900 x 1440"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\900x1440.jpg"
Case "960 x 1280"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\960x1280.jpg"
Case "1024 x 1280"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1024x1280.jpg"
Case "1280 x 768"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1280x768.jpg"
Case "1280 x 960"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1280x960.jpg"
Case "1280 x 1024"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1280x1024.jpg"
Case "1360 x 768"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1360x768.jpg"
Case "1366 x 768"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1360x768.jpg"
Case "1600 x 1200"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1600x1200.jpg"
Case "1920 x 1080"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1920x1080.jpg"
Case "1920 x 1200"
       strWallpaperPath = "C:\IT-Services\User Wallpapers\1920x1200.jpg"
Case Else
End Select

'Set the reg value
objSh.RegWrite "HKCU\Control Panel\Desktop\Wallpaper", strWallpaperPath, "REG_SZ"

'Apply the change
objSh.Run "%windir%\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", 0, False

Posted

How about this in Powershell (haven't tested as don't have a Win 10 machine handy)

 

$Monitor = Get-WmiObject -Class Win32_DesktopMonitor | Where {$_.DeviceId -eq 'DesktopMonitor1'}
$Resolution = "$($Monitor.ScreenWidth)x$($Monitor.ScreenHeight)"
$WallpaperPath = "C:\IT-Services\User Wallpapers\$resolution.jpg"
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop\" -Name "Wallpaper" -Value $Wallpaperpath
Rundll32.exe user32.dll,UpdatePerUserSystemParameters

 

A few sources don't include the "0,False" on the end of rundll or have "1,True" but I haven't delved into rundll32 to know if it's needed or not.

Posted

This powershell script works on Windows 7 and 10:

 

#Get current screen resolution
$CurrentRes = Get-WmiObject -Class win32_videocontroller 
$width = $CurrentRes.currentHorizontalResolution
$height = $CurrentRes.currentVerticalResolution


$Resolution = "$width" + "x" + "$height"

#Default wallpaper size if resolution not found in switch below
$WallpaperPath = "C:\windows\scripts\1024x768.jpg"

switch($Resolution)
{
("800x600")
       {$WallpaperPath = "C:\windows\scripts\1024x768.jpg"}
("1024x768")
       {$WallpaperPath = "C:\windows\scripts\1024x768.jpg"}
("1280x768")
       {$WallpaperPath = "C:\windows\scripts\1280x768.jpg"}
("1280x960")
       {$WallpaperPath = "C:\windows\scripts\1280x960.jpg"}
("1280x1024")
       {$WallpaperPath = "C:\windows\scripts\1280x1024.jpg"}
("1360x768")
       {$WallpaperPath = "C:\windows\scripts\1360x768.jpg"}
("1366x768")
       {$WallpaperPath = "C:\windows\scripts\1360x768.jpg"}
("1440x900")
       {$WallpaperPath = "C:\windows\scripts\1440x900.jpg"}
("1600x1200")
       {$WallpaperPath = "C:\windows\scripts\1600x1200.jpg"}
("1920x1080")
       {$WallpaperPath = "C:\windows\scripts\1920x1080.jpg"}
("1920x1200")
       {$WallpaperPath = "C:\windows\scripts\1920x1200.jpg"}
}

#Update registry value for the correct wallpaper
Set-ItemProperty -Path "HKCU:\Control Panel\Desktop\" -Name "Wallpaper" -Value $Wallpaperpath

#Function to set the wallpaper and refresh it
Add-Type @"
using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace Wallpaper
{
  public enum Style : int
  {
      Tile, Center, Stretch, NoChange
  }
  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, Wallpaper.Style style ) {
        SystemParametersInfo( SetDesktopWallpaper, 0, path, UpdateIniFile | SendWinIniChange );
        RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
        switch( style )
        {
           case Style.Stretch :
              key.SetValue(@"WallpaperStyle", "2") ; 
              key.SetValue(@"TileWallpaper", "0") ;
              break;
           case Style.Center :
              key.SetValue(@"WallpaperStyle", "1") ; 
              key.SetValue(@"TileWallpaper", "0") ; 
              break;
           case Style.Tile :
              key.SetValue(@"WallpaperStyle", "1") ; 
              key.SetValue(@"TileWallpaper", "1") ;
              break;
           case Style.NoChange :
              break;
        }
        key.Close();
     }
  }
}
"@

#Set Wallpaper and stretch!
[Wallpaper.Setter]::SetWallpaper( $WallpaperPath, 2 )

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...