windows 调整 CMD 窗口大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7552644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:09:44  来源:igfitidea点击:

Resize CMD Window

windowscommand-promptdoscmd

提问by ctype.h

How can I resize the Command Prompt window programmatically in C or C++? For example 80x25 or 80x40 characters. Thank you in advance.

如何在 C 或 C++ 中以编程方式调整命令提示符窗口的大小?例如 80x25 或 80x40 字符。先感谢您。

回答by ctype.h

The MODE command allows you to set the size of the Command Prompt window. The syntax is:

MODE 命令允许您设置命令提示符窗口的大小。语法是:

MODE [COLUMNS],[LINES]

For example for a 80x25 window you would use

例如,对于 80x25 窗口,您将使用

system("MODE 80,25");
此大小与窗口的特定实例相关联,因此其他命令窗口将设置为默认大小。它适用于较新的基于 WinNT 的操作系统(即 Win2000/XP/7)和 Win9x。如果大小不受支持,则不会更改。

Place it before any output, as it clears the screen.

将它放在任何输出之前,因为它会清除屏幕。

回答by ctype.h

I did some more research and this is what I came up with:

我做了更多的研究,这就是我想出的:

#include <windows.h>

int main(){
  system("mode 80,25");   //Set mode to ensure window does not exceed buffer size
  SMALL_RECT WinRect = {0, 0, 80, 25};   //New dimensions for window in 8x12 pixel chars
  SMALL_RECT* WinSize = &WinRect;
  SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), true, WinSize);   //Set new size for window

  //Insert your code here

  return 0;
}