2011-11-15 250 views
1

我正在使用FLTK。我有一个带有各种按钮的窗口,用户可以点击执行一些操作。在我的int main()我有一个switch语句来处理所有这些。当用户点击退出switch语句设置像这样:FLTK关闭窗口

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

这正好与两个按钮是(出口)和NO(不退出)创建一个退出确认窗口的do_save_exit功能。我得到了yes按钮来工作,退出程序,但没有按钮意味着我应该隐藏确认窗口。这是如下功能:

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函数不会看到退出窗口(超出范围)。我应该如何继续?谢谢

P.S:我想过如何使用指向退出窗口的指针,然后使用指针在no函数中退出窗口,但我不确定如何完全做到这一点。

回答

3

您可能需要查看使用模式(即对话框)窗口。看看<FL/fl_ask.h>

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

头也有弹出窗口的字体,标题功能等

+0

真棒。非常感谢。把我的头发拿出来找出这一个。 – Richard

0

当你建立你没有得到一个错误或警告?问题可能是你的全局函数名称分别为yesno,而且局部变量的调用也是一样的。重命名变量的功能。

2

The 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);