ncurses.h确定窗口边框

时间:2014-01-30 00:52:55

标签: c ncurses

我只想制作一个与终端相邻的盒子窗口。我基本上都要问的是......

说我有一个窗口:

window(h,w,y,x);

y = LINES +1
x = COLS +1

如何制作h和w就像MAX_X -1MAX_Y -1

这样我创建的方框概述了终端?以及如何用特定颜色填充此框?

1 个答案:

答案 0 :(得分: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;
}