Monday, April 19, 2010

Set Desktop Wallpaper according to screen Resolution

Recently I had a strange problem in our organisation.
We had Windows 7 Ent workstations with Windows 2003 Domain with soem restrictive GPOs.
One of them is company wallpaper through GPO. The GPO setting works fine but there was a strange problem. Some of the workstations appeared to be black background. The wallpaper is set, can se the filename with path in Destop Preferences, but does'nt appearing, unless you select Fit/Fill/Stretch/Center any on of them. This setting can be set from GPO but if it works on standard resolution then does'nt works on widescreen or vice versa. But if i use the wallpaper with the same resolution then works fine, but does'nt looks fine on other widescreen looks.
After some disgnosis i found that we have basically two types of screen size one Widescreen and other standard. But the wallper i was using in GPO was widescreen and higher resolution than max resolution supported by screens.
The problem was how to set by GPO to check resolution and set the wallpaper accordingly.
The solution i found was to write a script and set at logon by GPO which will detect the resolution and set the wallpaper accordingly.
I took help from Internet to write this code, so i had release to internet in return.

It is writen in vbscript to use run cscript script-name.vbs or set by GPO

I had kept the background images at a location named according to resolution like bg-1024.jpg, bg-1280.jpg, bg-1440.jpg ___________________________________________________________________
Dim strComputer
strComputer = "."

' Prepare WMI Connection
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

' Query WMI
Set getPixelsWidth = objWMIService.Execquery("Select * from CIM_VideoController")

' Get the Horizontal Resolution
strPixelWidth = getPixelsWidth.ItemIndex(0).CurrentHorizontalResolution

' Prepare filename for background wallpaper
strWallPaper = "\\x.x.x.x\usrdata\commonfiles\bg-" & strPixelWidth & ".jpg"

' Setting the wallpaper
Dim strWallPaper
Dim wshShell
Dim sUserName

Set wshShell = WScript.CreateObject("WScript.Shell")
sUserName = wshShell.ExpandEnvironmentStrings("%USERNAME%")

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")

sWinDir = oFSO.GetSpecialFolder(0)
sWallPaper = strWallPaper

' update in registry
oShell.RegWrite "HKCU\Control Panel\Desktop\Wallpaper", sWallPaper

' let the system know about the change ( looped few times as always does'nt work first time )
For i = 0 to 15
oShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", True
wscript.echo i
Next
___________________________________________________________________
Best Regards.

No comments:

Post a Comment