2013-03-03 34 views
1

我需要知道什麼是從屏幕上刪除菜單的正確方法。菜單張貼我專門爲它創建了一個窗口中,看到代碼:如何正確刪除ncurses中的菜單?

void makeMenu() { 
    MENU *menu; 
    WINDOW *win; 
    ITEM **items; 
    int choicesC = 4; 

    items = (ITEM**) calloc(choicesC, sizeof (ITEM*)); 
    for (int i = 0; i < choicesC; i++) { 
     items[i] = new_item(choices[i], choices[i]); 
    } 

    menu = new_menu(items); 

    win = newwin(10, 40, 4, 4); 
    keypad(win, true); 

    set_menu_win(menu, win); 
    set_menu_sub(menu, derwin(win, 6, 38, 3, 1)); 

    set_menu_mark(menu, "* "); 

    box(win, 0, 0); 

    post_menu(menu); 
    wrefresh(win); 


    int c; 
    bool continueB=true; 
    while ((c = wgetch(win)) != KEY_F(1) && continueB) { 
     switch (c) { 
      case KEY_DOWN: 
       menu_driver(menu, REQ_DOWN_ITEM); 
       break; 
      case KEY_UP: 
       menu_driver(menu, REQ_UP_ITEM); 
       break; 

      case 10: 
       if (current_item(menu)->index== 3) continueB=false; 
       break; 
     } 
     wrefresh(win); 
    } 

    unpost_menu(menu); 
    for (int i=0; i<choicesC; i++){ 
     free_item(items[i]); 
    } 
    free_menu(menu); 

    /* I can't figure out which commands to 
    call to instantly delete and redraw the menu 
    wclear(win); 
    wrefresh(win); 
    delwin(win); 
    clear(); 
    refresh(); 
    */ 
} 

如果我打電話makemenu(),然後以這種方式摧毀它,在菜單上下一殘培消失()調用(例如)當我摧毀它時,我希望它消失。我應該使用哪些命令?

謝謝

+0

單憑這些代碼很難說明什麼窗口/面板立即落後。首先看看[這個問題](http://stackoverflow.com/questions/14899708/ncurses-difference-between-doupdate-and-refresh-for-panels/14917120),然後考慮使用'touchwin()'on窗戶後面的東西你要毀掉,最後用面板輕鬆做到:) – 2013-03-06 16:21:15

+0

現在窗戶後面什麼也沒有,只有stdscr。奇怪的事情現在正在發生 - 我只調用'free_menu()'而沒有任何刷新或什麼,並且首先調用'makemenu()',一切正常,菜單消失,只要我按Enter鍵;但是當我再次調用'makemenu()'時,它不會消失。非常奇怪的行爲。 我會嘗試touchwin,謝謝你的提示 – 2013-03-06 20:23:04

回答

3

好吧,我想出了一個想法,我認爲這是正確的解決方案。問題是unpost_menu(*MENU)只能銷燬該菜單的子窗口,所以你必須自己刪除框架窗口。然後,清理過程是這樣的:

(...) 
unpost_menu(menu); 
for (int i=0; i<choicesC; i++){ 
    free_item(items[i]); 
} 
free_menu(menu); 
wborder(win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); // Erase frame around the window 
wrefresh(win); // Refresh it (to leave it blank) 
delwin(win); // and delete 

在此之後就沒有必要refresh(),當然,除非有菜單背後的東西。在這種情況下,你需要照顧重新粉刷它。