C 诅咒库退格问题
我试图在一个利用Curses库的简单窗口中实现删除字符 基本上,使用以下边界代码创建窗口:C 诅咒库退格问题,c,curses,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((
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();
}
}
}
虽然它确实会删除字符,但最终会将右侧垂直条从边框中拉出,从而在边框中创建一个孔。我在这里做错了什么,还是每次删除后都必须手动插入一个空格,以便将边框保持在初始位置
谢谢你的帮助 是的,您需要在竖条前重新插入一个空格,或者(我不确定这是否可行)设置一个小于终端全宽的滚动区域。您可能希望删除而不是删除字符 诅咒中的通常做法是创建子窗口,而不是尝试修复窗口。例如,可以创建一个窗口,在该窗口上绘制
框
,并创建该窗口的一个子窗口(小于该框),在该窗口中绘制和更新文本
以下是一个示例程序(使用):
#包括
#包括
#包括
int
主(空)
{
int-ch;
窗框;
窗口*显示;
int-xf,yf;
setlocale(LC_ALL,“”);
initscr();
cbreak();
noecho();
frame=newwin(第5行,第10行,第2行,第2行);
框(框,0,0);
框架;
getmaxyx(帧,yf,xf);
显示=德温(帧,yf-2,xf-2,1,1);
键盘(显示,正确);
而((ch=wgetch(显示))!=ERR){
开关(ch){
案例'\b':
大小写键\u退格:
getyx(显示器,yf,xf);
如果(wmove(显示屏,yf,xf-1)!=错误){
wdelch(显示);
}
打破
违约:
waddch(显示,(chtype)ch);
打破
}
}
endwin();
返回退出成功;
}
谢谢,我现在将尝试实现它,并将结果发回。嗯,这实际上是一个好主意,我将研究该解决方案,因为我偏离了方向,从未尝试过空间插入。我只是尝试了一下,但似乎是诅咒(不使用ncurses),或者至少是我的实现,不包括一个橡皮擦哈尔(WIN*w)函数,我需要这个函数,因为这个删除是在一个窗口中进行的。还有其他选择吗?
#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;
}