Curses库退格问题

时间:2012-11-29 02:39:11

标签: c curses

我正在尝试在使用Curses库的简单窗口中实现删除字符。

基本上,窗口是使用以下边框代码创建的:

box(local_win, 0 , 0); // Set the border of the window to the default border style.

以及稍后当我继续处理退格时,我使用以下代码执行此操作:

initscr();
cbreak();
keypad(window, TRUE);
int ch; // The character pressed by the user.

while((ch = wgetch(window)) != EOF)
{
   switch(ch)
   {
      case KEY_BACKSPACE: // Handle the backspace.
      {
         wdelch(window); // Delete the character at the position in the window.

         wrefresh(window);
         refresh();
      }
   }
}

虽然它确实删除了字符,但它最终会从边框拉出右侧垂直条,从而在边框中创建一个洞。我在这里做错了什么,或者这是我必须在每次删除后手动插入一个空格以保持边框在其初始位置的情况。

感谢您对此提供任何帮助!

3 个答案:

答案 0 :(得分:0)

是的,你需要在垂直条之前重新插入一个空格,或者(我不确定这是否可行)设置一个小于终端全宽的滚动区域。

答案 1 :(得分:0)

你可能想要删除而不是删除一个字符。

答案 2 :(得分:0)

而不是尝试修复窗口,curses中的通常做法是创建子窗口。例如,可以创建一个窗口,在其上绘制box,并创建的子窗口(并且小于框),其中绘制和更新文本。

以下是一个示例程序(使用derwin):

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

int
main(void)
{
    int ch;
    WINDOW *frame;
    WINDOW *display;
    int xf, yf;

    setlocale(LC_ALL, "");
    initscr();
    cbreak();
    noecho();

    frame = newwin(LINES - 5, COLS - 10, 2, 2);
    box(frame, 0, 0);
    wrefresh(frame);

    getmaxyx(frame, yf, xf);
    display = derwin(frame, yf - 2, xf - 2, 1, 1);

    keypad(display, TRUE);

    while ((ch = wgetch(display)) != ERR) {
        switch (ch) {
        case '\b':
        case KEY_BACKSPACE:
            getyx(display, yf, xf);
            if (wmove(display, yf, xf - 1) != ERR) {
                wdelch(display);
            }
            break;
        default:
            waddch(display, (chtype) ch);
            break;
        }
    }
    endwin();
    return EXIT_SUCCESS;
}
相关问题