Getting terminal size in c for windows?

后端 未结 3 1078
野趣味
野趣味 2020-11-28 08:45

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

There is this piece of code for linux:

         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 09:30

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

提交回复
热议问题