Ric*_*ard 3 c++ user-interface window fltk
我正在使用 FLTK。我有一个带有各种按钮的窗口,用户可以单击来执行某些操作。在我的 int main() 中,我有一个 switch 语句来处理所有这些。当用户单击退出时, switch 语句设置如下:
case Exit_program:
cout << "save files and exit\n";
do_save_exit(sw);
Run Code Online (Sandbox Code Playgroud)
这将转到 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();
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我单击“否”按钮时,它会变为“无效”,但我无法从那里去任何地方。我只想执行 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
}
}
Run Code Online (Sandbox Code Playgroud)
在您设置Fl_Window的任何位置设置窗口的回调,例如在main函数中:
window->callback( win_cb );
Run Code Online (Sandbox Code Playgroud)