C++ 获取终端窗口的大小(行/列)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23369503/
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
Get size of terminal window (rows/columns)
提问by Vittorio Romeo
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.
我想在 C/C++ 程序中检索这些数字。
I'm looking for a GNU/Linux solution primarily, but also need a Windows solution.
我主要在寻找 GNU/Linux 解决方案,但也需要一个 Windows 解决方案。
回答by herohuyongtao
On Windows, use the following code to print the size of the console window (borrowed from here):
在 Windows 上,使用以下代码打印控制台窗口的大小(从这里借用):
#include <windows.h>
int main(int argc, char *argv[])
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
printf("columns: %d\n", columns);
printf("rows: %d\n", rows);
return 0;
}
On Linux, use the following instead (borrowed from here):
在 Linux 上,请改用以下内容(从此处借用):
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
int main (int argc, char **argv)
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
printf ("lines %d\n", w.ws_row);
printf ("columns %d\n", w.ws_col);
return 0; // make sure your main returns int
}
回答by Moshe Gottlieb
For Unix(-based), use ioctl(2)
and TIOCGWINSZ
:
对于 Unix(-based),使用ioctl(2)
和TIOCGWINSZ
:
#include <sys/ioctl.h> //ioctl() and TIOCGWINSZ
#include <unistd.h> // for STDOUT_FILENO
// ...
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
/* size.ws_row is the number of rows, size.ws_col is the number of columns. */
// ...
Also, while I haven't touched Windows in the last five years, GetConsoleScreenBufferInfo()
should help you get the console window size.
此外,虽然我在过去五年中没有接触过 Windows,但GetConsoleScreenBufferInfo()
应该可以帮助您了解控制台窗口的大小。
回答by Killzone Kid
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:
为 Windows 扩展 @herohuyongtao 答案。.srWindow 属性给出了控制台窗口大小的答案,即可见行和列。这并没有说明实际可用的屏幕缓冲区宽度和高度是多少,如果窗口包含滚动条,则可能会更大。如果是这种情况,请使用 .dwSize:
CONSOLE_SCREEN_BUFFER_INFO sbInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbInfo);
int availableColumns = sbInfo.dwSize.X;
int availableRows = sbInfo.dwSize.Y;
回答by Andrew
On GNU/Linux using libtermcap (https://www.gnu.org/software/termutils/manual/termcap-1.3/html_mono/termcap.html) create demo.c:
在 GNU/Linux 上使用 libtermcap ( https://www.gnu.org/software/termutils/manual/termcap-1.3/html_mono/termcap.html) 创建 demo.c:
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <term.h>
static char term_buffer[2048];
void
init_terminal_data (void)
{
char *termtype = getenv ("TERM");
int success;
if (termtype == NULL)
fprintf (stderr, "Specify a terminal type with `setenv TERM <yourtype>'.\n");
success = tgetent (term_buffer, termtype);
if (success < 0)
fprintf (stderr, "Could not access the termcap data base.\n");
if (success == 0)
fprintf (stderr, "Terminal type `%s' is not defined.\n", termtype);
}
int
main ()
{
init_terminal_data ();
printf ("Got: Lines: %d, Columns: %d\n", tgetnum ("li"), tgetnum ("co"));
return 0;
}
Then compile with gcc -o demo.x demo.c -ltermcap
and run to give:
然后编译gcc -o demo.x demo.c -ltermcap
并运行给出:
$ ./demo.x
Got: Lines: 24, Columns: 80
I doubt this helps much on Windows though, I don't know that platform.
我怀疑这对 Windows 有多大帮助,但我不知道那个平台。
(Some of this code is copied straight from the termcap documentation.)
(其中一些代码是直接从 termcap 文档中复制的。)