c - ncurses.h 确定窗口边框

标签 c ncurses

我只是想制作一个围绕终端的框窗口。我基本上要问的是......

假设我有一个窗口:

window(h,w,y,x);

y = LINES +1
x = COLS +1

如何使 h 和 w 像 MAX_X -1MAX_Y -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;
}

关于c - ncurses.h 确定窗口边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21445871/

相关文章:

c - 如何告诉 GCC 发出一个由我命名的特定调试符号?

c - 在 ncurses 中处理信号

c - 标量集成

c - 关于常用算术转换的问题-GCC编译器

c++ - 无法将 ncurses 表单与窗口相关联

c - wprintw : In ncurses, 当写入与窗口宽度完全相同的换行符终止行时,打印两个换行符

c++ - 在 ncurses 中提取带有属性的宽字符

c - Linux 中的终止和信号问题

C : segmentation fault 11 结构中的 Char**