2017-10-14 84 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