在 ncurses 中制作一个 1x8 的无边框单元格

Make a 1x8 cell in ncurses without border

我想在 ncurses 中制作一个 1x8 的无边框单元格。我做的第一件事是制作 window

WINDOW*win = newwin(height, width, 0, 0);

高度 24 和宽度 80。我想制作一列 header 和一行 header.In 我想要字符串 'A' 到 'I' 并且在rowheader 我想要字符串“1”到“23”。这意味着所有单元格的高度为 1,宽度为 8,并且位置 (0,0) 上有一个空单元格。我想要 header 和 属性 STANDOUT 中的每个单元格。所以我写了一个函数DrawCell()。这是我试过的

void DrawCell(int x , int y, const char* ch){
   clear();
   wattron(win, A_STANDOUT);
   mvwprintw(win, x,y,ch);
   wrefresh(win);
   getchar(); 
   endwin();
}//DrawCell

问题是这个函数只显示STANDOUT中的字符串'ch'。但是我不知道如何将这个字符串放在高度为 1 宽度为 8 的单元格中。

根据描述,听起来您似乎想要类似的东西

#define CELL_WIDE 8
#define CELL_HIGH 1

void DrawCell(int col , int row, const char* ch) {
   int y = row * CELL_HIGH;
   int x = col * CELL_WIDE;
   wattron(win, A_STANDOUT);
   wmove(win, y, x);                 // tidier to be separate...
   wprintw("%*s", " ");              // fill the cell with blanks
   wprintw("%.*s", CELL_WIDE, ch);   // write new text in the cell
   wrefresh(win);
#if 0
   getchar(); 
   endwin();
#endif
}//DrawCell

因为您必须将单元格的行和列位置转换为 x 和 y 坐标。

一些注意事项:

  • 我 ifdef 取消了对 getchar 的调用,因为这似乎是您用于调试的内容。

  • 如果要绘制很多单元格,还应该将 wrefresh(win) 移出此函数,例如,到刷新整个单元格的位置 window。

  • 为了避免清除window,你应该使用wgetch(win)而不是getch(),因为后者会刷新stdscr,它可能会覆盖你的 window.

寻址注释,若功能改为

void DrawCell(int col , int row, const char* ch) {
   int y = row * CELL_HIGH;
   int x = col * CELL_WIDE;
   wmove(win, y, x);                 // tidier to be separate...
   wprintw("%*s", " ");              // fill the cell with blanks
   wattron(win, A_STANDOUT);
   wprintw("%.*s", CELL_WIDE, ch);   // write new text in the cell
   wattroff(win, A_STANDOUT);
   wrefresh(win);
#if 0
   getchar(); 
   endwin();
#endif
}//DrawCell

那么只有单元格的文本以突出模式显示。