2017-02-13 106 views
2

我正在制作一个ncurses程序,将屏幕分成两个窗口。顶部屏幕可以接受输入,通过按'#'它会将所有文本向下移动到底部窗口并擦除顶部窗口。在我的代码中,我试图使用copywin()来替换底部窗口,但它不会在第二个窗口中粘贴措辞。这是我有...覆盖窗口

#include <ncurses.h> 

int main(int argc, char *argv[]) 
{ 
// Declare variables for windows and sizes 
WINDOW *top_win, *bottom_win; 
int maxx, maxy, halfy, flag = 0, ch; 

// Start curses 
initscr(); 
noecho(); 
refresh(); 

// get the max x's and y's 
getmaxyx(stdscr, maxy, maxx); 
halfy = maxy >> 1; 

// Start color 
start_color(); 
init_pair(1, COLOR_BLACK, COLOR_WHITE); 
init_pair(2, COLOR_WHITE, COLOR_CYAN); 
init_pair(3, COLOR_RED, COLOR_WHITE); 

// Make windows 
top_win = newwin(halfy, maxx, 0, 0); 
wbkgd(top_win, COLOR_PAIR(1)); 
wrefresh(top_win); 

bottom_win = newwin(halfy, maxx, halfy, 0); 
wbkgd(bottom_win, COLOR_PAIR(2)); 
wrefresh(bottom_win); 

// Allow functions keys 
keypad(top_win, TRUE); 
keypad(bottom_win, TRUE); 

// while loop to get input 
while((ch = getch()) != '`') 
    { 
     if(ch == '@') 
      { 
       if(flag == 1) 
        { 
         flag = 0; 
        } 
       else 
        { 
         flag = 1; 
        } 
      } 
     else if(ch == '#') 
      { 
       //waddstr(bottom_win, "testing"); 
       copywin(top_win, bottom_win, 0, 0, halfy, 0, halfy, maxx, TRUE); 
       //overwrite(top_win, bottom_win); 
       //werase(top_win); 
      } 
     else if(flag != 1) 
      { 
       waddch(top_win, ch | COLOR_PAIR(1)); 
      } 
     else if(flag == 1) 
      { 
       waddch(top_win, ch | COLOR_PAIR(3)); 
      } 
     wrefresh(top_win); 
     wrefresh(bottom_win); 
    } 

// end curses 
delwin(top_win); 
delwin(bottom_win); 
endwin(); 
return 0; 
} 

我知道我可以使用'#'字符打印到窗口,因为我的注释掉,测试语句。我也刚刚尝试使用覆盖(),但也没有工作。我只是把争论混淆了,还是其他的东西?有任何想法吗?先谢谢你!

回答

1

我没有一个很好的解释,为什么它的工作,但只要xoffyoff至少1在下面的代码中,从上层窗口的数据复制到较低的窗口确定(并清除上部窗口)。颜色不被复制。如果任一偏移量为0,则不会复制数据。字符串testing添加在较低窗口的左上角 - 可以省略,复制的材料仍然可以。

#include <ncurses.h> 

int main(void) 
{ 
    // Declare variables for windows and sizes 
    WINDOW *top_win, *bottom_win; 
    int maxx, maxy, halfy, flag = 0, ch; 

    // Start curses 
    initscr(); 
    noecho(); 
    refresh(); 

    // get the max x's and y's 
    getmaxyx(stdscr, maxy, maxx); 
    halfy = maxy >> 1; 

    // Start color 
    start_color(); 
    init_pair(1, COLOR_BLACK, COLOR_WHITE); 
    init_pair(2, COLOR_WHITE, COLOR_CYAN); 
    init_pair(3, COLOR_RED, COLOR_WHITE); 

    // Make windows 
    top_win = newwin(halfy, maxx, 0, 0); 
    wbkgd(top_win, COLOR_PAIR(1)); 
    wrefresh(top_win); 

    bottom_win = newwin(halfy, maxx, halfy, 0); 
    wbkgd(bottom_win, COLOR_PAIR(2)); 
    wrefresh(bottom_win); 

    // Allow functions keys 
    // keypad(top_win, TRUE); 
    // keypad(bottom_win, TRUE); 

    // while loop to get input 
    int xoff = 1; 
    int yoff = 1; 
    while ((ch = getch()) != '`') 
    { 
     if (ch == '@') 
     { 
      if (flag == 1) 
      { 
       flag = 0; 
      } 
      else 
      { 
       flag = 1; 
      } 
     } 
     else if (ch == '#') 
     { 
      waddstr(bottom_win, "testing"); 
      // copywin(top_win, bottom_win, 0, 0, halfy, 0, halfy, maxx, TRUE); 
      copywin(top_win, bottom_win, 0, 0, yoff, xoff, halfy-yoff, maxx-xoff, TRUE); 
      // overwrite(top_win, bottom_win); 
      werase(top_win); 
     } 
     else if (flag != 1) 
     { 
      waddch(top_win, ch | COLOR_PAIR(1)); 
     } 
     else if (flag == 1) 
     { 
      waddch(top_win, ch | COLOR_PAIR(3)); 
     } 
     wrefresh(top_win); 
     wrefresh(bottom_win); 
    } 

    // end curses 
    delwin(top_win); 
    delwin(bottom_win); 
    endwin(); 
    return 0; 
} 

测试在Mac上运行的MacOS塞拉利昂10.12.3与GCC 6.3.0,使用本地-lncurses库。

+0

这是很奇怪的。我猜是因为它在复制所有内容和边界时存在问题。它的工作原理,并非常感谢你了解它!我将不得不深入探讨copywin()的参数。 –

+0

是的 - 我怀疑它是与边框有关的,但'testing'字符串出现在您期望复制的材质出现的地方。正如我所说,我没有很好的解释发生了什么事。我以(10,10)的偏移量开始,并且出现了材料。这导致最小化过程(偏移量2,1和0在10之后尝试)。如果你想出了一个很好的解释,请告诉我(例如在这里发表评论)。 –

3

copywin检查给定的行/列和decides that your destination rectangle doesn't lie completely within the destination window。这里有一个快速修复程序:

--- foo.c.orig 2017-02-13 16:13:12.000000000 -0500 
+++ foo.c  2017-02-13 16:30:18.037987489 -0500 
@@ -51,7 +51,7 @@ 
     else if(ch == '#') 
      { 
       //waddstr(bottom_win, "testing"); 
-    copywin(top_win, bottom_win, 0, 0, halfy, 0, halfy, maxx, TRUE); 
+    copywin(top_win, bottom_win, 0, 0, 0, 0, halfy - 1, maxx - 1, TRUE); 
       //overwrite(top_win, bottom_win); 
       //werase(top_win); 
      } 
@@ -73,4 +73,3 @@ 
endwin(); 
return 0; 
} 

行和列从零到最后一个行/列(比窗口大小少一)编号,所以我减去一个从dmaxrowdmaxcol参数。第五个参数dminrow已经过去了窗口的底部。

ncurses检查参数。关于兼容性和可移植性,使用Solaris curses运行相同的程序(将“ncurses.h”更改为“curses.h”)会转储核心。

的手册页可以改进,但很明显不够有关colors

只有文字其中两个窗口重叠复制