【问题标题】:Is there a command like pushd and popd that work with the console window size, text colour and window title?是否有类似 pushd 和 popd 的命令可以处理控制台窗口大小、文本颜色和窗口标题?
【发布时间】:2023-03-29 10:45:01
【问题描述】:

正是标题所说的。我有一个批处理脚本可以更改控制台的窗口分辨率、标题和文本颜色。当它关闭时,我希望我的脚本将所有这些恢复到用户最初的状态。这怎么可能?

【问题讨论】:

  • 请返回您之前的所有问题并接受他们的答案。不这样做是非常不礼貌的。
  • 我明白,但我不会接受答案,直到我可以验证所提供的答案满足我的问题。我以前做过,然后我意识到有些东西不起作用,所以我再次询问,但没有得到回应。
  • 只要您谈论的是用户第一次打开命令提示符时会看到什么,这是可能的。如果您正在谈论在脚本开始时获取 current 值(在用户在打开命令提示符和启动脚本之间更改它们之后),那么批量操作是不可能的。
  • 啊,好的。谢谢SomethingDark。那么我怎样才能把它改回他们第一次启动 CMD 时看到的样子呢?

标签: windows batch-file cmd windows-console pushd


【解决方案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"

这将首先测试用户是否在过去指定了一个标题,如果是,那么它将相应地更改标题,如果没有,它将提示用户输入一个标题。

【讨论】:

  • 感谢您的回答,但我希望我的脚本自动将当前标题存储为变量,以便在脚本完成后恢复它。
【解决方案2】:

这些存储在几个地方,但主要位于 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    相关资源
    最近更新 更多