windows Windows批处理脚本切换桌面背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7041703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:49:44  来源:igfitidea点击:

Windows batch script to switch desktop background

windowsscriptingbatch-file

提问by Tejen Shrestha

Is there a way to check what a user currently has as their background and then changing it depending on what it is? For example: I want a white background during day time and a black background for night time. Running the script would check the current background, if it is white it will switch to the black background, and if it is black it will switch to the white.

有没有办法检查用户当前的背景是什么,然后根据背景进行更改?例如:我想要白天的白色背景和晚上的黑色背景。运行脚本会检查当前背景,如果是白色则切换到黑色背景,如果是黑色则切换到白色。

I'm a little unfamiliar with Windows batch script and I'm seeking some tips and advice on how I can accomplish the task above. Here is what I've been able to find so far:

我对 Windows 批处理脚本有点不熟悉,我正在寻求一些关于如何完成上述任务的提示和建议。到目前为止,这是我能够找到的:

@echo off
call :quiet>nul 2>&1
goto :EOF

:quiet


:: Configure Wallpaper 
REG ADD "HKCU\Control Panel\Desktop" /V Wallpaper /T REG_SZ /F /D "%SystemRoot%\energybliss.bmp"
REG ADD "HKCU\Control Panel\Desktop" /V WallpaperStyle /T REG_SZ /F /D 0
REG ADD "HKCU\Control Panel\Desktop" /V TileWallpaper /T REG_SZ /F /D 2


:: Configure the screen saver.
:: REG ADD "HKCU\Control Panel\Desktop" /V SCRNSAVE.EXE /T REG_SZ /F /D "%SystemRoot%\System32\scrnsave.scr"
:: REG ADD "HKCU\Control Panel\Desktop" /V ScreenSaveActive /T REG_SZ /F /D 1


:: Set the time out to 900 seconds (15 minutes).
:: REG ADD "HKCU\Control Panel\Desktop" /V ScreenSaveTimeOut /T REG_SZ /F /D 900


:: Set the On resume, password protect box 
:: REG ADD "HKCU\Control Panel\Desktop" /V ScreenSaverIsSecure /T REG_SZ /F /D 1


:: Remove the user's ability to see the Screen Saver, background, and appearance tabs of Display Properties. 
::REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System" /V NoDispScrSavPage /T REG_DWORD /F /D 1
::REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System" /V NoDispBackgroundPage /T REG_DWORD /F /D 1
::REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System" /V NoDispAppearancePage /T REG_DWORD /F /D 1

:: Make the changes effective immediately
%SystemRoot%\System32\RUNDLL32.EXE user32.dll, UpdatePerUserSystemParameters

采纳答案by Viktor Lova

You can use code like that:

你可以使用这样的代码:

@echo off

:: '>nul 2>&1' was moved to other place
call :quiet
exit /b

:quiet
    :: Put there wallpaper name (with extension, bigger that 8 symbols)
    set "Wallpaper.Night.BadWrited=Wallpaper1.bmp"

    :: It is a dirty hack and example of bad code
    for /F "tokens=*" %%a in ('reg query "HKCU\Control Panel\Desktop" /v Wallpaper') do     set "Wallpaper.Current.BadWrited=%%a"

    :: Take last 8 symbols of wallpaper name. Change number of symbols to your own  minimal
    set "Wallpaper.Current.BadWrited=%Wallpaper.Current.BadWrited:~-8%"
    set "Wallpaper.Night.BadWrited=%Wallpaper.Night.BadWrited:~-8%"

    if "%Wallpaper.Current.BadWrited%"=="%Wallpaper.Night.BadWrited%" (
        call :MakeDayWallpaper>nul 2>&1
    ) else (
        call :MakeNightWallpaper>nul 2>&1
    )
exit /b

:MakeDayWallpaper
    echo Day wallpaper setted
    :: Put your code here
exit /b

:MakeNightWallpaper
    echo Night wallpaper setted 
    :: Put your code here
exit /b

But i recommend to use the system scheduler. You can acces it from control panel, 'Scheduled Tasks' or something. You can make 2 files named 'makeday.bat' and 'makenight.bat'. Scheduler will run them every day at needed time

但我建议使用系统调度程序。您可以从控制面板、“计划任务”或其他方式访问它。您可以制作 2 个名为“makeday.bat”和“makenight.bat”的文件。调度程序将每天在需要的时间运行它们