2014-01-30 138 views
1

我只是想製作一個與終端相鄰的框窗口。所有我基本上問的是...ncurses.h確定窗口邊界

說我有一個窗口:

window(h,w,y,x); 

y = LINES +1 
x = COLS +1 

如何讓這個H和W像MAX_X -1MAX_Y -1

將箱我創建的概述終端?以及如何填充此框以某種顏色?

回答

1

您可以使用box()函數在窗口邊緣繪製邊框,不需要知道高度和寬度即可。這裏有一個簡單的例子,一個白色邊框,藍色的背景:

#include <stdlib.h> 
#include <curses.h> 

#define MAIN_WIN_COLOR 1 

int main(void) { 

    /* Create and initialize window */ 

    WINDOW * win; 
    if ((win = initscr()) == NULL) { 
     fputs("Could not initialize screen.", stderr); 
     exit(EXIT_FAILURE); 
    } 


    /* Initialize colors */ 

    if (start_color() == ERR || !has_colors() || !can_change_color()) { 
     delwin(win); 
     endwin(); 
     refresh(); 
     fputs("Could not use colors.", stderr); 
     exit(EXIT_FAILURE); 
    } 

    init_pair(MAIN_WIN_COLOR, COLOR_WHITE, COLOR_BLUE); 
    wbkgd(win, COLOR_PAIR(MAIN_WIN_COLOR)); 


    /* Draw box */ 

    box(win, 0, 0); 
    wrefresh(win); 


    /* Wait for keypress before ending */ 

    getch(); 


    /* Clean up and exit */ 

    delwin(win); 
    endwin(); 
    refresh(); 

    return EXIT_SUCCESS; 
} 

如果你想知道窗口的尺寸,無論如何,你可以使用ioctl()像這樣:

#include <sys/ioctl.h> 

void get_term_size(unsigned short * height, unsigned short * width) { 
    struct winsize ws = {0, 0, 0, 0}; 
    if (ioctl(0, TIOCGWINSZ, &ws) < 0) { 
     exit(EXIT_FAILURE); 
    } 

    *height = ws.ws_row; 
    *width = ws.ws_col; 
}