运行代码:
//My_window类(带有next和quit按钮)
#include"std_lib_facilities.h"
#include"GUI/Simple_window.h"
#include"GUI/GUI.h"
#include"GUI/Graph.h"
#include"GUI/Point.h"//-------------------------------------------------------------------------------class My_window :public Window
{
public:My_window(Point xy, int w, int h, const string& title);bool wait_for_button(); // simple event loopprivate:Button next_button; bool button_pushed; Button quit_button;bool quit_pushed;static void cb_next(Address, Address); void next();static void cb_quit(Address, Address);void quit();
};//-------------------------------------------------------------------------------My_window::My_window(Point xy, int w, int h, const string& title) :Window(xy, w, h, title),next_button(Point(x_max() - 70, 0), 70, 20, "Next", cb_next),button_pushed(false),quit_button(Point(x_max()-70,25),70,20,"Quit",cb_quit),quit_pushed(false)
{attach(next_button);attach(quit_button);
}//------------------------------------------------------------------------------bool My_window::wait_for_button()
{if(!quit_pushed)show();if (quit_pushed) {hide(); return quit_pushed;}button_pushed = false;
#if 1while (!button_pushed&&!quit_pushed) Fl::wait();Fl::redraw();
#elseif Fl::run();
#endifreturn button_pushed;
}//------------------------------------------------------------------------------void My_window::cb_next(Address, Address pw)
{reference_to<My_window>(pw).next();
}//------------------------------------------------------------------------------void My_window::next()
{button_pushed = true;hide();
}//------------------------------------------------------------------------------void My_window::cb_quit(Address, Address pw)
{reference_to<My_window>(pw).quit();
}//------------------------------------------------------------------------------void My_window::quit()
{quit_pushed=true;
}//------------------------------------------------------------------------------int main()
try
{My_window win(Point(100, 100), 600, 400, "My_window");win.wait_for_button();
}
catch (exception& e) {cerr << "error:" << e.what() << '\n';keep_window_open();return 1;
}
catch (...) {cerr << "Oops:unknown exception!\n";keep_window_open();return 2;
}//---------------------------------------------------------------------------------
运行结果: