2014-04-29 14 views
12

Có cách nào đáng tin cậy để nhận số lượng cột/hàng của cửa sổ đầu ra hiện tại không?Nhận kích thước cửa sổ đầu cuối (hàng/cột)

Tôi muốn truy xuất các số này trong chương trình C/C++.

Tôi đang tìm giải pháp GNU/Linux chủ yếu, nhưng cũng cần giải pháp Windows.

+0

Trên Linux, bạn cũng có thể thử 'stty size' – choroba

+0

[này] (http://stackoverflow.com/questions/18783988/how-to-get- windows-size-from-linux) có thể hữu ích. – devnull

+0

Có thể trùng lặp của [Nhận chiều rộng đầu cuối trong C?] (Https://stackoverflow.com/questions/1022957/getting-terminal-width-in-c) –

Trả lời

12

Linux/unix: Sử dụng ioctl với số tệp đầu ra tiêu chuẩn STDOUT_FILENOTIOCGWINSZ.

struct winsize size; 
ioctl(STDOUT_FILENO,TIOCGWINSZ,&size); 
/* size.ws_row is the number of rows, size.ws_col is the number of columns. */ 


Ngoài ra, trong khi tôi không chạm vào cửa sổ trong vòng 5 năm trở lại đây, GetConsoleScreenBufferInfo sẽ giúp bạn có được giao diện điều khiển kích thước hiện tại.

+0

Tôi đã đọc đây không phải là tiêu chuẩn POSIX - là có bất kỳ xác định có thể cho tôi biết nếu mã sẽ hoạt động trên máy hiện tại? –

+0

Vui lòng thêm lại '# include's, vì đây không phải là câu trả lời đầy đủ. Xem: https://stackoverflow.com/questions/1022957/getting-terminal-width-in-c –

1

On GNU/Linux dùng libtermcap (https://www.gnu.org/software/termutils/manual/termcap-1.3/html_mono/termcap.html) tạo 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; 
} 

Sau đó biên dịch với gcc -o demo.x demo.c -ltermcap và chạy để cung cấp cho:

$ ./demo.x 
Got: Lines: 24, Columns: 80 

tôi nghi ngờ điều này giúp nhiều trên Windows mặc dù, Tôi không biết nền tảng đó.

(Một số mã này được sao chép trực tiếp từ tài liệu termcap.)

12

Trên Windows, sử dụng đoạn mã sau để in các kích thước của cửa sổ giao diện điều khiển (mượn từ here):

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

Trên Linux, sử dụng sau đây để thay thế (mượn từ here):

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

Để mở rộng câu trả lời @herohuyongtao cho Windows. Thuộc tính .srWindow cung cấp câu trả lời cho kích thước của cửa sổ bảng điều khiển, tức là các hàng và cột hiển thị. Điều này không nói chiều rộng và chiều cao của bộ đệm màn hình có sẵn thực tế là gì, có thể lớn hơn nếu cửa sổ chứa các thanh cuộn. Nếu đây là trường hợp, sử dụng .dwSize:

CONSOLE_SCREEN_BUFFER_INFO sbInfo; 
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbInfo); 
int availableColumns = sbInfo.dwSize.X; 
int availableRows = sbInfo.dwSize.Y; 
Các vấn đề liên quan