如何打开给定窗口大小的控制台应用程序?


16

我要启动的应用程序是MongoDB。如果我能正常启动它,它看起来像这样:

在此处输入图片说明

我不喜欢换行符的数量,而且屏幕空间很大,所以我想利用上述空间来消除换行符。

我可以使用更改控制台窗口的大小MODE,因此我编写了一个批处理文件,如下所示:

@ECHO OFF
MODE con:cols=140 lines=70
%~dp0mongodb\bin\mongod --dbpath %~dp0data --rest

到目前为止,一切都很好。启动此批处理文件时,将根据需要获得一个更大的窗口。

但是当我现在按Ctrl+ C退出MongoDB时,我得到了烦人的提示:

Terminate batch job (Y/N)?

这没用,因为我刚刚退出的命令无论如何都是批处理作业中的最后一条命令,无论我回答什么,结果都是一样的。

因此,当我按Ctrl+ 时,如何在没有提示的情况下为应用程序获取更大的控制台窗口C

Answers:


12

今天我花了几个小时来实现一个小的C#应用​​程序,该应用程序可以包装另一个控制台应用程序并调整控制台主机的窗口大小。

console-wrapper易于使用。它只需要几个命令行参数:

Usage: console-wrapper.exe [OPTIONS]

Options:

      --subject=VALUE        The application that should be started by the
                               console wrapper.
      --width=VALUE          The desired width of the console window.
      --height=VALUE         The desired height of the console window.
  -h, -?, --help             Shows this help message

--width--height用于设置结果控制台窗口的大小。--subject可以可选地使用,如果省略,则其余参数被视为要启动的命令(带有参数)。

因此,我现在在启动脚本中使用的最后一个调用是:

START "MongoDB" database\console-wrapper.exe --width=140 --height=70 %~dp0database\mongodb\bin\mongod.exe --dbpath %~dp0database\data --rest

生成的窗口将具有请求的大小,并且在按Ctrl+时C,窗口将关闭(在正确关闭所包含的进程之后)。


6

您是否考虑过通过Powershell脚本运行它?Powershell能够调整其自身窗口的大小

$h = Get-Host
$buf = $h.UI.RawUI.BufferSize
$buf.Width = 300
$h.ui.rawui.BufferSize = $buf
$win = $h.UI.RawUI.WindowSize
$win.Width = 150
$h.ui.rawui.WindowSize = $win
#Command to start mongodb here.

这将创建一个大小为150的窗口,但其侧向可滚动缓冲区为300。


是的,我确实考虑过,但当时不知道如何实施。除了可能会禁用脚本执行的小麻烦之外,这非常理想。
Der Hochstapler

5

据我了解的问题,您希望MongoDB具有指定大小的控制台窗口,并在按Ctrl+ 时退出C。然后答案很简单:使用快捷方式启动它。

我没有mongod可执行文件,因此我将perl.exe举一个例子。从“运行”对话框(Windows+ R)启动时,控制台窗口的大小将为标准80×25,缓冲区大小为300行。

  1. 创建mongod(或perl在我的情况下)的快捷方式。
  2. 右键单击快捷方式,然后单击属性
    1. 快捷方式的选项卡目标字段中添加参数mongod
    2. “开始于”字段控制已启动程序的初始当前目录。默认情况下,它将是.exe所在的目录。
  3. 单击布局选项卡。
    1. 窗口大小更改为140×70。
      快捷方式属性,“布局”选项卡
    2. 您可以将“ 屏幕高度”缓冲区的大小增加到1000或更大,以便能够向上滚动到较旧的消息。
    3. 如果清除“ 让系统位置窗口”,则可以在屏幕上指定控制台窗口的位置。
  4. 单击“ 确定”关闭快捷方式属性。

现在,当您单击此快捷方式时,控制台窗口将具有指定的大小。当您按Ctrl+时C,应用程序退出并且窗口关闭,而没有任何其他提示。


“运行”对话框启动窗口后,还可以更改窗口的布局。右键单击控制台窗口的标题,然后单击属性。单击布局选项卡,调整窗口大小,然后单击确定以关闭属性并立即应用更改。(如果窗口是从快捷方式启动的,则快捷方式的属性会更新。)

下次您启动同一应用程序时,控制台窗口的大小将神奇地相同。(我这次没有检查它,但是它曾经可以工作。)


的位置mongod.exe不固定。它将根据应用程序的部署而变化。这就是为什么我直接通过控制台寻求解决方案的原因。当我在目标或工作目录字段中输入相对路径作为快捷方式时,它不会接受它们:(
Der Hochstapler 2013年

@OliverSalzburg您是否尝试过重新启动批次的方法?我的意思是您的批处理通过自身开始start cmd /k并传递附加参数或设置环境变量。然后,第二个实例更改控制台窗口大小并启动mongod.exe
阿列克谢·伊凡诺夫

是的,我尝试了很多事情。但是,只要涉及到批处理文件,该解决方案就会受到以下Terminate batch job (Y/N)?提示的困扰:\我实际上刚刚完成了一个包装器应用程序,为我解决了这个问题。
Der Hochstapler

@OliverSalzburg这将是一个非常有用的实用程序。我还认为只有包装是唯一的方法……
阿列克谢·伊凡诺夫

3

默认控制台布局设置位于注册表中:

[HKEY_CURRENT_USER\Console]

您可以修改这些设置,并且它们将应用于所有启动的命令窗口,而与路径无关。不幸的是,原始价值并不友好。

若要以所需方式设置它们,请打开常规的cmd窗口,然后根据自己的喜好编辑设置。在注册表中的上述路径中查找,您应该在cmd.exe的“控制台”下看到一个子项。将该键导出到文件中,将其更改为以上路径并导入,或者仅手动将值从一个复制到另一个。然后,您可以删除cmd.exe特定项。现在,所有打开的命令窗口将使用修改后的默认值。

我发现使用标准默认值创建几个注册表项文件非常方便,这样,当我第一次使用RDP时,我可以轻松地将它们弹出到新服务器上。我为通常使用的两种尺寸进行了预设。

对于1024 x 768(我将其用于通常通过多会话RDP工具访问的服务器):

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Console]
"ScreenBufferSize"=dword:012c0050
"WindowSize"=dword:00190050
"WindowPosition"=dword:000a000a
"FontSize"=dword:000c0007
"FontFamily"=dword:00000030
"FontWeight"=dword:00000190
"FaceName"="Terminal"
"QuickEdit"=dword:00000001

对于1280 x 1024:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Console]
"ScreenBufferSize"=dword:0bb800af
"WindowSize"=dword:004b00af
"WindowPosition"=dword:000a000a
"FontSize"=dword:000c0007
"FontFamily"=dword:00000030
"FontWeight"=dword:00000190
"FaceName"="Terminal"
"QuickEdit"=dword:00000001

0

Start命令放在调用mongodb的前面。我相信应该解决它。


不,这会在新的控制台窗口中打开MongoDB。当我使用start /b它时,按Ctrl + C后将使控制台窗口保持打开状态。
Der Hochstapler

0

如果您乐意同时运行批处理文件和一些微型帮助程序,请在此处发布完整的解决方案:
批处理文件如何运行程序并设置窗口的位置和大小?

内容如下。。。。

该演示批处理文件将运行命令行程序,并设置其“命令提示符”控制台窗口的位置和大小。该批处理的总体主题与前两个解决方案相同(在上面的链接中的“堆栈溢出”页面上),但是有一些独特的问题和解决方法才能使它起作用—这是“ CMD”托管的命令提示符窗口.EXE”,而不是GUI窗口,例如“ EXPLORER.EXE”。

阅读批处理文件中的所有注释和描述以获取更多信息。

使用的工具:
1.   MonitorInfoView  用NIR Sofer (41 KB)
2   的NirCmd  用NIR Sofer (43 KB)
3.   GetPIDs  由Daniel Scheibli (280 KB)
4.   upx.exe  ---是,我们将使用这个演示的命令行程序; 将其放置在与批处理文件相同的目录中。
5.批处理文件---参见下文

将所有五个文件收集到一个目录中。这是批处理文件,可以在任何Windows系统上运行。

该演示批处理将要执行的工作,除了向您提供一个自定义的控制台窗口外,还将upx.exe与命令行开关一起运行--best -v -o getpids-compressed.exe getpids.exe。我们将把我们的工具“ GetPIDs”压缩到88 KB ........启动后,双击此控制台窗口的标题栏,可以看到一个有趣的现象。此控制台窗口的行为类似于GUI!

@echo off
setlocal enabledelayedexpansion enableextensions
pushd %~dp0%

REM ----- ADD SOME USEFUL INFORMATION TO THIS CONSOLE WINDOW TITLE.
for /f "tokens=3" %%A in ('getpids.exe') do set _PROCESS_ID_=%%A
title This batch file runs a command-line program and sets the position and size of its console window        (PROCESS ID = %_PROCESS_ID_%)

REM ********************** DESCRIPTION ************************************
REM ** This script opens a command-line program console window with specified
REM ** screen properties at the primary monitor (containing the taskbar).
REM ** The "X/Y position" and "W/H size" of the console window is auto-set by
REM ** this script and the monitor resolution is auto-calculated to suit.
REM ** 'MonitorInfoView.exe' is the helper tool used to capture the resolution
REM ** info of the monitor.
REM ** 'nircmd.exe' is the tool performing all the display trickery.
REM ** 'getpids.exe' is the helper tool used to capture the Process ID of the
REM ** working batch file, thus allowing us to identify which CMD.EXE host this
REM ** console window belongs to when looking at Task Manager.
REM **
REM ** To tweak this script, go to the code section named:
REM ** >>>>> USER INPUT/PREFERENCES ARE ALL SET HERE <<<<<
REM ***********************************************************************

REM ----- CLEAR ANY PREVIOUS JOB OUTPUTS IF THEY EXIST
if exist ~TMP.TXT type NUL > ~TMP.TXT

REM ----- OUTPUT THE PRIMARY MONITOR INFORMATION TO A TEXT FILE
MonitorInfoView.exe /hideinactivemonitors 1 /stext ~TMP.TXT

REM ----- ISOLATE THE RESOLUTION LINE, REMOVING ALL THE OTHER LINES IN THE TEXT FILE
for /f "delims=" %%A in ('type "~TMP.TXT" ^|find.exe /i "Maximum Resolution"') do echo %%A>~TMP.TXT

REM ----- GET THE RESOLUTION NUMBERS, AND SET THEM AS VARIABLES
for /f "tokens=3,4 delims=:X " %%A in ('type "~TMP.TXT"') do set _SCREENW_=%%A& set _SCREENH_=%%B


REM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
REM >>>>>>>>>> USER INPUT/PREFERENCES ARE ALL SET HERE [BEGIN] <<<<<<<<<<<<

REM ----- ----------------------------------------
REM ----- |COMMAND-LINE PROGRAM TO BE RUN:  FILE NAME (WITH EXTENSION)
REM ----- |
REM ----- | |IF PROG TO BE RUN IS 'CMD.EXE' (WINDOWS COMMAND PROCESSOR)
REM ----- | |ALSO READ THE NEXT COMMENTS ABOUT SETTING SWITCHES   

    set _MYPROGRAM_=upx.exe

REM ----- ----------------------------------------   
REM ----- |COMMAND-LINE PROGRAM TO BE RUN:  SWITCHES (TO BE PASSED TO THE PROGRAM)
REM ----- |
REM ----- | |YOU CAN LEAVE THIS VARIABLE WITH A BLANK VALUE IF RUNNING THE PROG WITHOUT ANY PARAMETERS
REM ----- | | 
REM ----- | | |IF THE VALUE CONTAINS THE CHARS  '|'  OR  '<'  OR  '>'  THIS BATCH WILL FAIL TO RUN !!!
REM ----- | | |
REM ----- | | | |YOU CAN APPEND EXTRA TEXT TO THE END OF THE COMMAND BY USING THE AMPERSAND CHARACTER '&'
REM ----- | | | | EXAMPLES:
REM ----- | | | | set _MYPROGRAMSWITCHES_=dir "%systemroot%\system32" /a/o/s/4 & this text will be ignored; useful for adding some inline comments.
REM ----- | | | | set _MYPROGRAMSWITCHES_=dir "%systemroot%\system32" /a/o/s/4         & this works too, and the extra blank spaces will also be ignored.
REM ----- | | | | 
REM ----- | | | | |IF PROG TO BE RUN IS 'CMD.EXE' (WINDOWS COMMAND PROCESSOR), PATH ARGUMENTS MUST POINT TO A SPECIFIC LOCATION
REM ----- | | | | |AND BE ABSOLUTE OR INCLUDE ENVIRONMENT VARIABLES, AND ALWAYS ENCLOSE PATHS IN QUOTES.
REM ----- | | | | | EXAMPLES:
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "C:\Folder With Spaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "C:\FolderWithoutSpaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%SYSTEMROOT%\..\Folder With Spaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%SYSTEMROOT%\..\FolderWithoutSpaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type %0                                       <<<<<<<this batch file (quotes are optional; the only exception to the rule)
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%~dp0%\..\File.txt"                     <<<<<<<goes back one Dir from this batch file
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%~dp0%\..\SomeFolder\File.txt"          <<<<<<<goes back one Dir from this batch file and then forwards into "SomeFolder"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=dir "%systemroot%\system32" /a/o/s/4          <<<<<<<displays directory listing of the System folder. NOTE: 'dir' by itself and
REM ----- | | | | |                                                                                without a specific path argument will resolve to the folder of this batch file.

    set _MYPROGRAMSWITCHES_=--best -v -o getpids-compressed.exe getpids.exe      &// original is 280 KB, will compress down to 88 KB

REM ----- ----------------------------------------
REM ----- |COMMAND-LINE PROGRAM TO BE RUN:  DIRECTORY PATH (OF THE PROGRAM)
REM ----- | 
REM ----- | |YOU CAN LEAVE THIS VARIABLE WITH A BLANK VALUE IF THE PROG IS AT SAME LOCATION AS BATCH
REM ----- | |THIS VARIABLE WILL BE IGNORED IF THE PROG IS 'CMD.EXE'
REM ----- | | 
REM ----- | | EXAMPLES:
REM ----- | | set _MYPROGRAMDIR_=.\                               <<<<<<<relative path, program and batch in same directory
REM ----- | |                                                                  dot backslash *OR* dot (trailing backslash is optional).
REM ----- | | set _MYPROGRAMDIR_=%dp0%                            <<<<<<<relative path, program and batch in same directory
REM ----- | |                                                                  same locatiion as previous but using environment variable.
REM ----- | | set _MYPROGRAMDIR_=..\                              <<<<<<<relative path, program is back one directory from batch
REM ----- | |                                                                  dot dot backslash *OR* dot dot (trailing backslash is optional).
REM ----- | | set _MYPROGRAMDIR_=%dp0%\..                         <<<<<<<relative path, program is back one directory from batch
REM ----- | |                                                                  same as location as previous but using environment variable.
REM ----- | | set _MYPROGRAMDIR_=%SYSTEMROOT%\..\My Utilities     <<<<<<<relative path, program is back one directory from the Windows folder
REM ----- | |                                                                  and then forwards into the folder 'My Utilities'
REM ----- | | set _MYPROGRAMDIR_=C:\My Utilities\                 <<<<<<<absolute path, enclosing quotes are optional; trailing backslash is optional
REM ----- | |                                                                  same location as previous

    set _MYPROGRAMDIR_=

REM ----- ----------------------------------------
REM ----- |SET THE WANTED DIMENSIONS OF THIS CONSOLE WINDOW

    set /a _WINW_=(%_SCREENW_% / 2) + 250
    set /a _WINH_=(%_SCREENH_% / 2) + 150
    set /a _WINX_=(%_SCREENW_% - %_WINW_%) / 2
    set /a _WINY_=(%_SCREENH_% - %_WINH_%) / 2

REM ----- ----------------------------------------
REM ----- |STYLE THIS CONSOLE WINDOW   !!!IMPORTANT!!!
REM ----- |
REM ----- | |IF WE DO NOT USE THE 'MODE' COMMAND HERE ALONG WITH ITS 'COLS/LINES' VALUES THEN
REM ----- | |THE POSITIONING/SIZING OF THIS WINDOW BY 'NIRCMD.EXE' FURTHER DOWN WILL NOT WORK !!!
REM ----- | |
REM ----- | | |SOME COMMANDS WILL OUTPUT A LOT OF LINES AND AND THE CONSOLE DISPLAY WILL BE TRUNCATED
REM ----- | | |RUN THIS FOR AN EXAMPLE=====>   cmd.exe dir "%systemroot%\system32" /a/o/s/4   <=====
REM ----- | | |TO SOLVE THIS PROBLEM, SET THE 'LINES' VALUE OF 'MODE' TO A VERY HIGH NUMBER
REM ----- | | |THE MAXIMUM NUMBER IS 32000 (ON MY SYSTEM); 'MODE' WILL BE IGNORED IF THE NUMBER IS HIGHER !!!   

    color 0A
    mode.com con:cols=%_SCREENW_% lines=10000

REM ----- ----------------------------------------
REM ----- |DELETE THE TEMP TEXT FILE MADE BY 'MonitorInfoView.exe'
REM ----- |COMMENT OUT THE LINE IF YOU DON'T CARE ABOUT DELETING IT

    del /f /q ~TMP.TXT 2>nul >nul

REM >>>>>>>>>> USER INPUT/PREFERENCES ARE ALL SET HERE [END] <<<<<<<<<<<<<<
REM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


REM ----- RUN THE TASK . . .
nircmd.exe win setsize foreground %_WINX_% %_WINY_% %_WINW_% %_WINH_%
popd

if not defined _MYPROGRAMDIR_ set _MYPROGRAMDIR_=%~dp0
if "!_MYPROGRAMDIR_!" == "." set _MYPROGRAMDIR_=%~dp0
if "!_MYPROGRAMDIR_!" == ".\" set _MYPROGRAMDIR_=%~dp0
pushd %_MYPROGRAMDIR_%
if not exist %_MYPROGRAMDIR_% goto _SKIP_
set _CONVERT_PATH_="%CD%"
set _CONVERT_PATH_=%_CONVERT_PATH_:"=%
set _MYPROGRAMDIR_=%_CONVERT_PATH_%
:_SKIP_
popd

echo ===============================================================================
echo PROGRAM ..............: !_MYPROGRAM_!
if "!_MYPROGRAM_!"=="cmd.exe" (echo PROGRAM DIR ..........: %SYSTEMROOT%\system32) else (echo PROGRAM DIR ..........: !_MYPROGRAMDIR_!)
echo PROGRAM SWITCHES .....: !_MYPROGRAMSWITCHES_!
echo ===============================================================================

if "!_MYPROGRAM_!"=="cmd.exe" (goto _PROGRAM_IS_CMD_) else (goto _PROGRAM_IS_OTHER_)

:_PROGRAM_IS_CMD_
cd /d "%SYSTEMROOT%\system32"
!_MYPROGRAMSWITCHES_!
echo. & echo. & echo.
goto _END_

:_PROGRAM_IS_OTHER_ 
cd /d "!_MYPROGRAMDIR_!"
if "%CD%\!_MYPROGRAM_!" == "%CD%\" goto _ERRORMESSAGE_
if not exist "%CD%\!_MYPROGRAM_!" goto _ERRORMESSAGE_
"!_MYPROGRAMDIR_!\!_MYPROGRAM_!" !_MYPROGRAMSWITCHES_!
echo. & echo. & echo.
goto _END_

:_ERRORMESSAGE_
cls
color 4F
echo ===============================================================================
echo PROGRAM ..............: !_MYPROGRAM_!
echo PROGRAM DIR ..........: !_MYPROGRAMDIR_!
echo PROGRAM SWITCHES .....: !_MYPROGRAMSWITCHES_!
echo ===============================================================================
echo                        ^^!^^!^^!  E R R O R  ^^!^^!^^!
echo.
echo The program has failed to run; the path set by the above values does not exist.
echo Please exit this window and check the values you have set in this batch file.
echo.
echo This batch file is located here:
echo %0
echo. & echo. & echo. & echo. & echo.

:_END_

REM ----- CLEAR VARIABLE VALUES FROM MEMORY . . .
set _PROCESS_ID_=
set _SCREENW_=
set _SCREENH_=
set _MYPROGRAM_=
set _MYPROGRAMSWITCHES_=
set _MYPROGRAMDIR_=
set _WINW_=
set _WINH_=
set _WINX_=
set _WINY_=
set _CONVERT_PATH_=

REM ----- STOP THIS CONSOLE WINDOW FROM CLOSING   !!!IMPORTANT!!! . . .
REM ----- |
REM ----- | http://superuser.com/questions/306167/how-to-prevent-the-command-prompt-from-closing-after-execution
REM ----- | | by 'kitsu.eb' (April 26, 2014)
REM ----- | | | "My solution (just tested on Win7) is to add CMD as the last line in the batch file.
REM ----- | | |  This runs a nested command prompt that inherits the environment of its parent.
REM ----- | | |  That child shell holds the batch process open until you EXIT, at which point the
REM ----- | | |  batch has no child processes and also exits."
REM ----- | Comment . . .
REM ----- | | by me
REM ----- | | | "This is a workaround. You can't use PAUSE as the last command to stop this window from closing
REM ----- | | |  because the window will terminate after you press a key. While this batch is running, if you open
REM ----- | | |  Task Manager/Process Hacker/Process Explorer you will see an extra CMD.EXE child process running
REM ----- | | |  under the parent CMD.EXE of this batch file."
cmd.exe

同样,在此处复制相关内容也很好。
slhck 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.