C语言 在 c 中获取 Windows 中的终端大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6812224/
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
Getting terminal size in c for windows?
提问by Dr Beco
How to check ymax and xmax in a console window, under Windows, using plain c?
如何在 Windows 下的控制台窗口中使用纯 c 检查 ymax 和 xmax?
There is this piece of code for linux:
有一段 linux 代码:
#include <stdio.h>
#include <sys/ioctl.h>
int main (void)
{
struct winsize max;
ioctl(0, TIOCGWINSZ , &max);
printf ("lines %d\n", max.ws_row);
printf ("columns %d\n", max.ws_col);
}
Now I wonder how can I do the same for windows. I tried winioctl.hbut it does not define struct winsizenor any other with a similar name.
现在我想知道如何为 Windows 做同样的事情。我试过了,winioctl.h但它没有定义struct winsize或任何其他具有类似名称的。
Any tips? Thanks.
有小费吗?谢谢。
PS. In linux you also can find the console size using getenv("LINES");. Is there a similar variable under windows?
附注。在 linux 中,您还可以使用getenv("LINES");. windows下有没有类似的变量?
PPS. Also, there is always ncurses.h, that I suppose work both systems, but I'm avoiding it because of conflicts with other libraries I have.
聚苯乙烯。此外,总是有ncurses.h,我想这两个系统都可以使用,但是由于与我拥有的其他库发生冲突,我正在避免使用它。
PPPS. This question here Getting terminal width in C?has a lot of tips, so no need to repeat that.
购买力平价。这个问题在这里获取 C 中的终端宽度?有很多提示,所以不需要重复。
采纳答案by Dr Beco
(Partial answer)
(部分答案)
This code:
这段代码:
CONSOLE_SCREEN_BUFFER_INFO csbi;
int ret;
ret = GetConsoleScreenBufferInfo(GetStdHandle( STD_OUTPUT_HANDLE ),&csbi);
if(ret)
{
printf("Console Buffer Width: %d\n", csbi.dwSize.X);
printf("Console Buffer Height: %d\n", csbi.dwSize.Y);
}
Gives the size of the buffer. The only problem is that dwSize.Yis not really the size of the screen (300 here instead of 25 lines). But dwSize.Xmatches the column's number. Needs only windows.hto work.
给出缓冲区的大小。唯一的问题是这dwSize.Y不是真正的屏幕大小(这里是 300 行而不是 25 行)。但dwSize.X匹配列的编号。只windows.h需要工作。
回答by quantum
This prints the size of the console, not the buffer:
这将打印控制台的大小,而不是缓冲区:
#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;
}
This code works because srWindow"contains the console screen buffer coordinates of the upper-left and lower-right corners of the display window", and the SMALL_RECTstructure "specify the rows and columns of screen-buffer character cells" according to MSDN. I subtracted the parallel sides to get the size of the console window. Since I got 1less than the actual value, I added one.
根据 MSDN,此代码之所以有效,是因为srWindow“包含显示窗口左上角和右下角的控制台屏幕缓冲区坐标”,以及SMALL_RECT“指定屏幕缓冲区字符单元格的行和列”的结构。我减去平行边以获得控制台窗口的大小。因为我得到的1比实际值少,所以我加了一个。
回答by Leon van Dommelen
The below two functions will get the window size somewhat more directly.
以下两个函数将更直接地获得窗口大小。
Note that I found, using gcc, that neither this approach nor GetConsoleScreenBufferInfo works if the program is piped. That is somewhat of a pain as for/f then does not work either. Apparently the screen data is not available in a pipe.
请注意,我使用 gcc 发现,如果程序通过管道传输,则此方法和 GetConsoleScreenBufferInfo 都不起作用。这有点痛苦,因为 for/f 然后也不起作用。显然,屏幕数据在管道中不可用。
Um, the above remark is of course enormously stupid. ;) It is STDOUT that is not the screen in a pipe! That does mean I prefer using STD_ERROR_HANDLE above STD_OUTPUT_HANDLE. I am far less likely to direct standard error away from the screen than standard output.
嗯,上面这句话当然是非常愚蠢的。;) STDOUT 不是管道中的屏幕!这确实意味着我更喜欢在 STD_OUTPUT_HANDLE 之上使用 STD_ERROR_HANDLE。与标准输出相比,我不太可能将标准错误从屏幕上移开。
typedef struct _CONSOLE_FONT_INFO {
DWORD nFont;
COORD dwFontSize;
} CONSOLE_FONT_INFO, *PCONSOLE_FONT_INFO;
BOOL WINAPI GetCurrentConsoleFont(
HANDLE hConsoleOutput,
BOOL bMaximumWindow,
PCONSOLE_FONT_INFO lpConsoleCurrentFont
);
/* Get the window width */
int getww_(void)
{
CONSOLE_FONT_INFO info;
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &info);
return info.dwFontSize.X;
}
/* Get the window height */
int getwh_(void)
{
CONSOLE_FONT_INFO info;
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &info);
return info.dwFontSize.Y;
}

