2017-10-14 108 views
0

標題所說的是什麼。我有一個批處理腳本來改變控制檯的窗口分辨率,標題和文本顏色。當它關閉時,我希望我的腳本能夠將所有這些恢復到用戶原來的位置。這有多可能?有沒有像控制檯窗口大小,文本顏色和窗口標題一樣的pushd和popd命令?

+0

請回到以前的所有問題並接受他們的答案。極不禮貌的不這樣做。 – Squashman

+0

我明白了,但我不會接受答案,直到我可以驗證提供的答案滿足我的問題。我之前做過這件事,然後我意識到有些東西不起作用,所以我再問一次,但我沒有得到答覆。 – ditheredtransparency

+0

只要你在談論第一次打開命令提示符時用戶會看到什麼,這是可能的。如果您正在討論在腳本開始時採用_current_值(在用戶在打開命令提示符和啓動腳本之間對其進行更改之後),那麼這是不可能的。 – SomethingDark

回答

1

您可以將所有數據保存在各種文本文件中,如此。

set /p title=Create a Title: 
title %title% 
break >"title.txt" 
echo %title% >>"title.txt" 

這將要求用戶輸入他們喜歡的標題名稱,並將該標題名稱保存在文本文件中。現在要把它作爲標題從現在開始,你可以做到這一點。

if exist "title.txt" (
    set /p title1=<title.txt 
    title %title1% 
) 
set /p title=Create a Title: 
title %title% 
break >"title.txt" 
echo %title% >>"title.txt" 

這將如果用戶尚未指定在過去的一個標題,如果是的話那麼它會相應更改標題,如果沒有它會提示爲標題用戶第一次測試。

+0

我很欣賞答案,但我希望我的腳本自動將當前標題存儲爲變量,以便在腳本完成時可以恢復它。 – ditheredtransparency

0

這些存儲在幾個地方,但大多位於HKCU \ Console下的註冊表中。另外,默認情況下,cmd標題只是cmd.exe的路徑,但如果通過快捷方式打開它,標題將更改爲任何快捷方式的名稱。不幸的是,快捷鍵的位置隨着每個Windows版本而改變,並且不可能確定命令提示符是如何打開的,所以我們堅持使用默認的默認標題。

要麼將​​您的其他代碼粘貼在標有「您的其他代碼在這裏」的部分,要麼只有一行說明call yourscript.bat

@echo off 

::------------------------------------------------------------------------------ 
:: Store default values from the registry 
::------------------------------------------------------------------------------ 
call :get_rows_and_columns WindowSize 
call :get_rows_and_columns ScreenBufferSize 
call :get_HKCU_Console_value ScreenColors 
set "default_colors=%hkcu_value:~-2%" 

:: Leading zeroes get removed in a reg query which makes things complicated 
:: when the default background color is black 
set "default_colors=%default_colors:x=0%" 

::------------------------------------------------------------------------------ 
:: YOUR OTHER CODE HERE 
::------------------------------------------------------------------------------ 


::------------------------------------------------------------------------------ 
:: RESTORE CMD TO ITS ORIGINAL APPEARANCE 
::------------------------------------------------------------------------------ 
call :resize_console %WindowSize_cols% %WindowSize_rows% %ScreenBufferSize_cols% %ScreenBufferSize_rows% 

:: Google says the default cmd window title is the path to cmd.exe, which is 
:: stored in %COMSPEC%, but I've also seen it be based on the name of the 
:: shortcut in %APPDATA%\Microsoft\Windows\Start Menu\Programs\System Tools 
:: in Windows 10 or %APPDATA%\Microsoft\Windows\Start Menu\Programs\Accessories 
:: in Windows Vista. I don't have a 7, 8, or 8.1 VM so I don't know the paths 
:: for those. 
title %COMSPEC% 
pause 
exit /b 

::------------------------------------------------------------------------------ 
:: Gets the registry value of a specified key 
:: 
:: Arguments: %1 - the key to search for 
:: Returns: The value of the registry key 
::------------------------------------------------------------------------------ 
:get_HKCU_Console_value 
set "hkcu_value=" 
for /f "tokens=3" %%A in ('reg query HKCU\Console /v %~1 ^| find "%~1"') do set "hkcu_value=%%A" 
exit /b 

::------------------------------------------------------------------------------ 
:: Calculates rows and columns of a screen size based on registry value. 
:: According to https://stackoverflow.com/a/10664060/4158862, the decimal value 
:: of the registry key is equal to (rows*65536)+columns. 
:: 
:: Arguments: %1 - The registry key to search for 
:: Returns: The number of rows and columns used by that screen 
::------------------------------------------------------------------------------ 
:get_rows_and_columns 
set "key=%~1" 
call :get_HKCU_Console_value "%key%" 
set "%key%Size_hex=%hkcu_value%" 
set /a %key%Size_dec=%key%Size_hex + 0 
set /a %key%_cols=%key%Size_dec %% 65536 
set /a %key%_rows=%key%Size_dec/65536 
exit /b 

::------------------------------------------------------------------------------ 
:: Adjusts the size of both the command prompt window and its line buffer 
:: From https://stackoverflow.com/a/13351373/4158862 
:: 
:: Arguments: %1 - Columns in cmd screen width 
::   %2 - Rows in cmd screen width 
::   %3 - Columns in buffer width 
::   %4 - Rows in cmd screen width 
:: Returns: None 
::------------------------------------------------------------------------------ 
:resize_console 
mode con: cols=%1 lines=%2 
powershell -command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%3;$B.height=%4;$W.buffersize=$B;}" 
exit /b