Get size of terminal window (rows/columns)

前端 未结 6 2101
执笔经年
执笔经年 2020-12-02 13:37

Is there any reliable way of getting the number of columns/rows of the current output terminal window?

I want to retrieve these numbers in a C/C++ program.

I

6条回答
  •  爱一瞬间的悲伤
    2020-12-02 14:22

    To expand @herohuyongtao answer for Windows. The .srWindow property gives the answer to the size of the console window, i.e. visible rows and cols. This doesn't say what is the actual available screen buffer width and height, which could be larger if window contains scroll bars. If this is the case, use .dwSize:

    CONSOLE_SCREEN_BUFFER_INFO sbInfo;
    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbInfo);
    int availableColumns = sbInfo.dwSize.X;
    int availableRows = sbInfo.dwSize.Y;
    

提交回复
热议问题