首页 > 解决方案 > Windows - 调整壁纸大小,除非

问题描述

我在 Linux Mint 中有一个墙纸更换器脚本,如下所示(在 crontab* 中运行)。如果你分析一下,意思是如果随机图片的高度或宽度大于屏幕宽度,则缩放它ELSE不缩放它,只是将它居中(这里有一个条件)。

而且效果很好,我喜欢我的剧本。但是,我在 Windows 中找不到类似的东西,我想知道附近是否有人有类似的脚本,而不是我必须编写它?

Windows 壁纸的问题在于,没有选项可以不缩放低于宽度/高度屏幕的图片,同时也不能同时缩放高于宽度/高度的图片。Linux 中也存在同样的问题,但这就是我为此编写脚本的原因。

请不要回答我必须调整图像大小。我不需要。我在 Linux 中找到了解决方案,当然可以在 Windows 中自动执行此操作。只需要一个将进入任务计划程序的 VBS 或 PowerShell 脚本。

* 2m 壁纸更改的 Crontab 注释:

*/2 * * * *   bash /somewhere/wallpaper.sh

.

#!/bin/bash

DESKTOP=mate16
wallpaperFolder="your/wallpaper/folder/with/a/trailing/slash/"
wallpaperWidth="2560"
wallpaperHeight="1080"

# grab a random image
wallpaperImage="$(ls $wallpaperFolder | shuf -n1)"
wallpaperImage=$wallpaperFolder$wallpaperImage

# grab the width and the height of the picture
wallpaperWidth=`identify -format "%w" "$wallpaperImage" | tr -d ' '`
wallpaperHeight=`identify -format "%h" "$wallpaperImage" | tr -d ' '`


if [[ "$DESKTOP" = "gnome2" ]]; then
    gconftool-2 -t string -s /desktop/gnome/background/picture_filename "$wallpaperImage"
fi


if [[ "$DESKTOP" = "gnome3" ]]; then
    if [ $wallpaperImage_width -gt $wallpaperWidth -o $wallpaperImage_height -gt $wallpaperHeight ]; then
        DISPLAY=:0 gsettings set org.gnome.desktop.background picture-options scaled
    else
        DISPLAY=:0 gsettings set org.gnome.desktop.background picture-options centered
    fi
    DISPLAY=:0 gsettings set org.gnome.desktop.background picture-uri "file:$wallpaperImage"
    DISPLAY=:0 gsettings set org.gnome.desktop.background primary-color "#000000"
    DISPLAY=:0 gsettings set org.gnome.desktop.background secondary-color "#000000"
fi

if [[ "$DESKTOP" = "mate14" ]]; then
    if [ $wallpaperImage_width -gt $wallpaperWidth -o $wallpaperImage_height -gt $wallpaperHeight ]; then
        mateconftool-2 -t string -s /desktop/mate/background/picture_options scaled
    else
        mateconftool-2 -t string -s /desktop/mate/background/picture_options centered
    fi
    mateconftool-2 -t string -s /desktop/mate/background/picture_filename "$wallpaperImage"
    mateconftool-2 -t string -s /desktop/mate/background/primary_color "#000000"
    mateconftool-2 -t string -s /desktop/mate/background/secondary_color "#000000"
fi

if [[ "$DESKTOP" = "mate16" ]]; then
    matePID=$(pgrep mate-session)
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$matePID/environ|cut -d= -f2-)

    if [ $wallpaperImage_width -gt $wallpaperWidth -o $wallpaperImage_height -gt $wallpaperHeight ]; then
        DISPLAY=:0 gsettings set org.mate.background picture-options scaled
    else
        DISPLAY=:0 gsettings set org.mate.background picture-options centered
    fi
    DISPLAY=:0 gsettings set org.mate.background picture-filename "$wallpaperImage"
    DISPLAY=:0 gsettings set org.mate.background primary-color "#000001"
    DISPLAY=:0 gsettings set org.mate.background secondary-color "#000002"
fi


if [[ "$DESKTOP" = "xfce" ]]; then
    # image-style: 0=Auto, 1=Centered, 2=Tiled, 3=Stretched, 4=Scaled, 5=Zoomed
    xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-show -s false
    if [ $wallpaperImage_width -gt $wallpaperWidth -o $wallpaperImage_height -gt $wallpaperHeight ]; then
        xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-style -s 4
    else
        xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-style -s 1
    fi
    xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "$wallpaperImage"
    xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-show -s true
fi

标签: resizewallpaper

解决方案


不幸的是,我不得不对整个事情进行编码。效果很好,除了在极少数情况下 Fit 和 Center 会混淆。

壁纸更换器.vbs

Dim shell,command
command = "powershell.exe -nologo -command ""C:\Scripts\wallpaper-changer.ps1"""
Set shell = CreateObject("WScript.Shell")
shell.Run command,0

壁纸更换器.ps1

# https://gist.github.com/ivandeex/43477bdecb9ebd4e86fc328f81485e4f

# Installation
# Add C:\Scripts\wallpaper-changer.vbs
# Add C:\Scripts\wallpaper-changer.ps1
# Run in command line:
# schtasks /Create /TN "wallpaper-changer" /SC MINUTE /MO 2 /TR "C:\Scripts\wallpaper-changer.vbs"
# /MO 2 = change every 2 minutes

$wallPaperFolder = 'C:\your-picture-folder'




Function Update-Wallpaper {
    Param(
        [Parameter(Mandatory=$true)]
        $Path,

        [ValidateSet('Center','Stretch','Fill','Tile','Fit')]
        $Style
    )
    Try {
        if (-not ([System.Management.Automation.PSTypeName]'Wallpaper.Setter').Type) {
            Add-Type -TypeDefinition @"
            using System;
            using System.Runtime.InteropServices;
            using Microsoft.Win32;
            namespace Wallpaper {
                public enum Style : int {
                    Center, Stretch, Fill, Fit, Tile
                }
                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.Tile :
                                key.SetValue(@"WallpaperStyle", "0") ; 
                                key.SetValue(@"TileWallpaper", "1") ; 
                                break;
                            case Style.Center :
                                key.SetValue(@"WallpaperStyle", "0") ; 
                                key.SetValue(@"TileWallpaper", "0") ; 
                                break;
                            case Style.Stretch :
                                key.SetValue(@"WallpaperStyle", "2") ; 
                                key.SetValue(@"TileWallpaper", "0") ;
                                break;
                            case Style.Fill :
                                key.SetValue(@"WallpaperStyle", "10") ; 
                                key.SetValue(@"TileWallpaper", "0") ; 
                                break;
                            case Style.Fit :
                                key.SetValue(@"WallpaperStyle", "6") ; 
                                key.SetValue(@"TileWallpaper", "0") ; 
                                break;
}
                        key.Close();
                    }
                }
            }
"@ -ErrorAction Stop 
            } 
        } 
        Catch {
            Write-Warning -Message "Wallpaper not changed because $($_.Exception.Message)"
        }
    [Wallpaper.Setter]::SetWallpaper( $Path, $Style )
}


# Get a random image file
$wallpaperRandom = Get-ChildItem -Path "$wallpaperFolder" -Recurse | Select-Object FullName | Get-Random -Count 1

# Get the width/height of the file
$wallpaperDetails  = New-Object -ComObject Wia.ImageFile
$wallpaperDetails.loadfile($wallpaperRandom.FullName)

# Get screen resolution
Add-Type -AssemblyName System.Windows.Forms

# Set wallpaper
If ( $wallpaperDetails.Width -gt [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width -Or $wallpaperDetails.Height -gt [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height ) {
  Update-Wallpaper -Path $wallpaperRandom.FullName -Style Fit
}
Else {
  Update-Wallpaper -Path $wallpaperRandom.FullName -Style Center
}

推荐阅读