【问题标题】:Make a 1x8 cell in ncurses without border在 ncurses 中创建一个 1x8 单元格,无边框
【发布时间】:2016-03-26 15:00:43
【问题描述】:

我想在 ncurses 中制作一个没有边框的 1x8 单元格。我做的第一件事是做一个窗户

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

高度为 24,宽度为 80。我想制作一个列标题和一个行标题。在列中我想要字符串“A”到“I”,在行标题中我想要字符串“1”到“23”。这意味着所有单元格的高度为 1,宽度为 8,并且在位置 (0,0) 上有一个空单元格。我希望标题中的每个单元格都具有属性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 的单元格中。

【问题讨论】:

    标签: c++ ncurses


    【解决方案1】:

    根据描述,听起来好像你想要类似的东西

    #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) 移出此函数,例如,移至刷新整个窗口的位置。

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

    地址注释,如果函数改为

    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
    

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

    【讨论】:

    • 如果我运行你的代码,我的整个窗口是 STANDOUT 期望窗口中第一行的前半部分。我想要的是 DrawCell() 制作一个高度为 1 和宽度为 8 的单元格,可以在其中放置一个字符串。
    • 我原以为你想让整个牢房脱颖而出。为了使每个单元格中的文本突出,将 wattron 调用移到第一个 printw 之后。在任何一种情况下,问题都是说单元格没有边框(这会使整个屏幕被文本覆盖)。
    • 我希望 1x8 单元格突出,而不是除单元格之外的整个窗口。如果我移动 wattron 调用,我只打印光标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 2022-08-04
    • 1970-01-01
    相关资源
    最近更新 更多