FLTK 关闭窗口

发布于 2024-12-15 13:21:13 字数 977 浏览 2 评论 0原文

我正在使用 FLTK。我有一个带有各种按钮的窗口,用户可以单击来执行某些操作。在我的 int main() 中,我有一个 switch 语句来处理所有这些。当用户单击 exit 时, 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 函数中退出窗口,但不确定具体如何执行。

I am using 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. When the user clicks exit the switch statement is setup like so:

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). I got the yes button to work, exits the program, but the no button mean i should just hide the confirmation window. 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). How should I proceed? Thank you

P.S: 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

短暂陪伴 2024-12-22 13:21:13

当尝试关闭窗口时,将调用 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 的任何位置设置窗口的回调,例如在 ma​​in 中> 功能:

window->callback( win_cb );

The Fl_Window callback is called when an attempt is made to close the 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, e.g. in your main function:

window->callback( win_cb );
Oo萌小芽oO 2024-12-22 13:21:13

您可能需要考虑使用模式(即对话框)窗口。看

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

标头还具有弹出窗口的字体、标题等功能。

You probably need to look at using a modal (i.e., dialog) window. Look at <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.

佞臣 2024-12-22 13:21:13

当您构建时,您没有收到错误或警告吗?问题可能是您既有全局函数名称 yesno ,也有名称相同的局部变量。重命名变量的函数。

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. Rename either the functions of the variables.

你げ笑在眉眼 2024-12-22 13:21:13

无需使用 hide()。
您只需在回调中使用 exit(0); 即可。

No need to use hide().
You can simply use exit(0); in a callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文