簡體   English   中英

FLTK 關閉窗口

[英]FLTK Closing window

我正在使用 FLTK。 我有一個帶有各種按鈕的窗口,用戶可以單擊以執行某些操作。 在我的 int main() 中,我有一個 switch 語句來處理所有這些。 當用戶單擊退出時,switch 語句的設置如下:

case Exit_program:
    cout << "save files and exit\n";
    do_save_exit(sw);

這將轉到 do_save_exit 函數,該函數創建一個帶有兩個按鈕是(退出)和否(不退出)的退出確認窗口。 我讓 yes 按鈕工作,退出程序,但 no 按鈕意味着我應該隱藏確認窗口。 這是以下功能:

void yes(Address addr, Address)
{
    exit(0);
}
void no(Address addr, Address)
{

}
void do_save_exit(Window& w)
{
    Window quit(Point(w.x()+100, w.y()+100), 250, 55, "Exit confirmation");
    Text conf(Point(15,15),"Do you really want to save and exit?");
    Button yes(Point(60, 20),35,30,"Yes",yes);
    Button no(Point(140, 20),35,30,"No",no);
    quit.attach(conf);
    quit.attach(yes);
    quit.attach(no);
    wait_for_main_window_click();
}

問題是,當我單擊“否”按鈕時,它會變為“否”,但我不能從那里去任何地方。 我只想執行 quit.hide() 但 no 函數看不到退出窗口(超出范圍)。 我應該如何進行? 謝謝

PS:我曾想過使用指向退出窗口的指針,然后在 no 函數中使用指針退出窗口,但我不確定如何准確地做到這一點。

當試圖關閉窗口時調用Fl_Window回調。 默認回調隱藏窗口(如果所有窗口都隱藏,您的應用程序結束)。 如果您設置自己的窗口回調,則可以覆蓋此行為,以免隱藏窗口:

// This window callback allows the user to save & exit, don't save, or cancel.
static void window_cb (Fl_Widget *widget, void *) 
{
    Fl_Window *window = (Fl_Window *)widget;

    // fl_choice presents a modal dialog window with up to three choices.
    int result = fl_choice("Do you want to save before quitting?", 
                           "Don't Save",  // 0
                           "Save",        // 1
                           "Cancel"       // 2
                           );
    if (result == 0) {  // Close without saving
        window->hide();
    } else if (result == 1) {  // Save and close
        save();
        window->hide();
    } else if (result == 2) {  // Cancel / don't close
        // don't do anything
    }
}

在您設置Fl_Window 的任何地方設置窗口的回調,例如在您的函數中:

window->callback( win_cb );

您可能需要考慮使用模態(即對話框)窗口。 查看<FL/fl_ask.h>

if (fl_ask("Do you really want to save and exit?"))
    save_and_exit();

標題還具有彈出窗口的字體、標題等功能。

無需使用隱藏()。 您可以簡單地使用exit(0); 在回調中。

構建時沒有收到錯誤或警告? 問題可能是您有兩個全局函數名稱yesno以及調用相同的局部變量。 重命名變量的函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM