Resize CMD Window

2020-06-20 08:31发布

问题:

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

回答1:

SetConsoleWindowInfo



回答2:

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

MODE [COLUMNS],[LINES]

For example for a 80x25 window you would use

system("MODE 80,25");
This size is associated with a specific instance of the window so other command windows will be set to the default size. It works in both newer WinNT based OSs (i.e. Win2000/XP/7) and Win9x. If the size is not supported it will not change.

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



回答3:

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;
}