在c中获取windows的终端大小?

发布于 2024-11-26 12:47:17 字数 796 浏览 1 评论 0原文

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

有一段针对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);
}

现在我想知道如何为windows做同样的事情。我尝试了 winioctl.h 但它没有定义 struct winsize 或任何其他具有类似名称的内容。

有什么建议吗?谢谢。

附言。在 Linux 中,您还可以使用 getenv("LINES"); 查找控制台大小。 windows下有类似的变量吗?

聚苯硫醚。另外,总是有 ncurses.h,我认为它可以在两个系统上工作,但由于与我拥有的其他库发生冲突,我避免使用它。

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

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

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\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.h but it does not define struct winsize nor any other with a similar name.

Any tips? Thanks.

PS. In linux you also can find the console size using getenv("LINES");. Is there a similar variable under 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妥活 2024-12-03 12:47:17

这会打印控制台的大小,而不是缓冲区的大小:

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

此代码之所以有效,是因为 srWindow“包含显示窗口左上角和右下角的控制台屏幕缓冲区坐标”,并且 <根据 MSDN,code>SMALL_RECT 结构“指定屏幕缓冲区字符单元的行和列”。我减去平行边以获得控制台窗口的大小。由于我得到的值比实际值少 1,所以我添加了 1。

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_RECT structure "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 1 less than the actual value, I added one.

情愿 2024-12-03 12:47:17

(部分答案)

此代码:

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

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

(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.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.

楠木可依 2024-12-03 12:47:17

下面的两个函数将更直接地获取窗口大小。

请注意,我发现,使用 gcc,如果程序通过管道传输,则此方法和 GetConsoleScreenBufferInfo 都不起作用。这有点痛苦,因为 for/f then 也不起作用。显然屏幕数据在管道中不可用。

嗯,上面的言论当然是非常愚蠢的。 ;) STDOUT 不是管道中的屏幕!这确实意味着我更喜欢使用 STD_ERROR_HANDLE 而不是 STD_OUTPUT_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;
}

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.

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.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文