2011-08-12 64 views
3

有沒有辦法檢查用戶當前具有哪些背景,然後根據其內容進行更改?例如:我想在白天時間使用白色背景,在夜間使用黑色背景。運行腳本將檢查當前背景,如果它是白色的,則會切換到黑色背景,如果是黑色,則會切換到白色。用於切換桌面背景的Windows批處理腳本

我有點不熟悉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 

回答

1

可以使用代碼類似:

@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 

但我建議使用系統調度。您可以從控制面板,「計劃任務」或其他內容訪問它。您可以創建2個名爲'makeday.bat'和'makenight.bat'的文件。調度程序將在需要的時間每天運行它們

+0

這幾乎可行,但似乎有一個問題。有時(實際上大多數情況下)腳本必須在背景更改之前運行多次。註冊表值似乎會改變,但背景保持不變。 '%SystemRoot%\ System32 \ RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters'該行假設無需註銷就可以更改背景,但由於某種原因它不起作用。 –

+0

確保您正在運行具有相應權限的rundll32.exe ...它可能是UAC(用戶帳戶控制)停止(=攔截)該系統調用工作。 ;) – 2011-12-20 16:23:07