在 c 中为 Windows 获取终端大小? [英] Getting terminal size in c for windows?

查看:14
本文介绍了在 c 中为 Windows 获取终端大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Windows 下的控制台窗口中使用纯 c 检查 ymax 和 xmax?

How to check ymax and xmax in a console window, under Windows, using plain c?

linux有这段代码:

There is this piece of code for linux:

#include <stdio.h>
#include <sys/ioctl.h>
int main (void)
{
    struct winsize max;
    ioctl(0, TIOCGWINSZ , &max);
    printf ("lines %d
", max.ws_row);
    printf ("columns %d
", max.ws_col);
}

现在我想知道如何对 windows 做同样的事情.我试过 winioctl.h 但它没有定义 struct winsize 也没有定义任何其他类似名称.

Now I wonder how can I do the same for windows. I tried winioctl.h but it does not define struct winsize nor any other with a similar name.

有什么建议吗?谢谢.

PS.在 linux 中,您还可以使用 getenv("LINES"); 找到控制台大小.windows下有类似的变量吗?

PS. In linux you also can find the console size using getenv("LINES");. Is there a similar variable under windows?

PPS.此外,总是有 ncurses.h,我想这两个系统都可以使用,但我避免使用它,因为与我拥有的其他库发生冲突.

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.

购买力平价.这里的这个问题Getting terminal width in C?有很多tips,所以不需要重复一遍.

PPPS. This question here Getting terminal width in C? has a lot of tips, so no need to repeat that.

推荐答案

(部分回答)

这段代码:

CONSOLE_SCREEN_BUFFER_INFO csbi;
int ret;
ret = GetConsoleScreenBufferInfo(GetStdHandle( STD_OUTPUT_HANDLE ),&csbi);
if(ret)
{
    printf("Console Buffer Width: %d
", csbi.dwSize.X);
    printf("Console Buffer Height: %d
", csbi.dwSize.Y);
}

给出缓冲区的大小.唯一的问题是 dwSize.Y 并不是真正的屏幕大小(这里是 300 而不是 25 行).但是 dwSize.X 匹配列的编号.只需要 windows.h 即可工作.

Gives the size of the buffer. The only problem is that dwSize.Y is not really the size of the screen (300 here instead of 25 lines). But dwSize.X matches the column's number. Needs only windows.h to work.

这篇关于在 c 中为 Windows 获取终端大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆