简体   繁体   English

FLTK 关闭窗口

[英]FLTK Closing window

I am using FLTK.我正在使用 FLTK。 I have a window with a variety of buttons the user can click to perform some action.我有一个带有各种按钮的窗口,用户可以单击以执行某些操作。 In my int main() i have a switch statement to handle all of these.在我的 int main() 中,我有一个 switch 语句来处理所有这些。 When the user clicks exit the switch statement is setup like so:当用户单击退出时,switch 语句的设置如下:

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

This goes to the do_save_exit function that creates a exit confirmation window with two buttons yes (exit) and no (don't exit).这将转到 do_save_exit 函数,该函数创建一个带有两个按钮是(退出)和否(不退出)的退出确认窗口。 I got the yes button to work, exits the program, but the no button mean i should just hide the confirmation window.我让 yes 按钮工作,退出程序,但 no 按钮意味着我应该隐藏确认窗口。 This is the follow functions:这是以下功能:

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();
}

The problem is, when i click the no button it goes to void no, but I can't go anywhere from there.问题是,当我单击“否”按钮时,它会变为“否”,但我不能从那里去任何地方。 I just want to do quit.hide() but the no function doesn't have sight of the quit window (out of scope).我只想执行 quit.hide() 但 no 函数看不到退出窗口(超出范围)。 How should I proceed?我应该如何进行? Thank you谢谢

PS: I have thought about using a pointer to the quit window and then using the pointer to quit the window in the no function but am not sure how to do that exactly. PS:我曾想过使用指向退出窗口的指针,然后在 no 函数中使用指针退出窗口,但我不确定如何准确地做到这一点。

The Fl_Window callback is called when an attempt is made to close the window.当试图关闭窗口时调用Fl_Window回调。 The default callback hides the window (and if all windows are hidden, your application ends).默认回调隐藏窗口(如果所有窗口都隐藏,您的应用程序结束)。 If you set your own window callback, you can override this behaviour, so as not to hide the 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
    }
}

Set your window's callback wherever you set up your Fl_Window , eg in your main function:在您设置Fl_Window 的任何地方设置窗口的回调,例如在您的函数中:

window->callback( win_cb );

You probably need to look at using a modal (ie, dialog) window.您可能需要考虑使用模态(即对话框)窗口。 Look at <FL/fl_ask.h>查看<FL/fl_ask.h>

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

The header also has functions for the popup's font, title, etc.标题还具有弹出窗口的字体、标题等功能。

No need to use hide().无需使用隐藏()。 You can simply use exit(0);您可以简单地使用exit(0); in a callback.在回调中。

When you build you don't get an error or warning?构建时没有收到错误或警告? The problem is probably that you have both global functions names yes and no and also local variables called just the same.问题可能是您有两个全局函数名称yesno以及调用相同的局部变量。 Rename either the functions of the variables.重命名变量的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM