正是标题所说的。我有一个批处理脚本可以更改控制台的窗口分辨率、标题和文本颜色。当它关闭时,我希望我的脚本将所有这些恢复到用户最初的状态。这怎么可能?
问问题
408 次
2 回答
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"
这将首先测试用户是否在过去指定了一个标题,如果是,那么它将相应地更改标题,如果没有,它将提示用户输入一个标题。
于 2017-10-14T05:32:31.803 回答
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
于 2017-10-18T01:43:05.853 回答