运行代码:
//GUI-Menu菜单实例(颜色+线型菜单)
#include"std_lib_facilities.h"
#include"GUI/Simple_window.h"
#include"GUI/GUI.h"
#include"GUI/Graph.h"
#include"GUI/Point.h"
struct Lines_window :Window
{
Lines_window(Point xy, int w, int h, const string& title);
Open_polyline lines;
private:
Menu color_menu;
Menu Line_style_menu;
Button menu_button;
Button Line_style_button;
Button next_button;
Button quit_button;
In_box next_x;
In_box next_y;
Out_box xy_out;
void change(Color c) { lines.set_color(c); }
void change_Line_style(Line_style l) { lines.set_style(l); }
void hide_menu() { color_menu.hide(); menu_button.show(); }
void hide_Line_style_menu() { Line_style_menu.hide(); Line_style_button.show(); }
static void cb_red(Address, Address);
void red_pressed() { change(Color::red); hide_menu(); }
static void cb_blue(Address, Address);
void blue_pressed() { change(Color::blue); hide_menu(); }
static void cb_black(Address, Address);
void black_pressed() { change(Color::black); hide_menu(); }
static void cb_solid(Address, Address);
void solid_pressed() { change_Line_style(Line_style::solid); hide_Line_style_menu(); }
static void cb_dash(Address, Address);
void dash_pressed() { change_Line_style(Line_style::dash); hide_Line_style_menu(); }
static void cb_dot(Address, Address);
void dot_pressed() { change_Line_style(Line_style::dot); hide_Line_style_menu(); }
static void cb_menu(Address, Address);
void menu_pressed() { menu_button.hide(); color_menu.show(); }
static void cb_Line_style_menu(Address, Address);
void Line_style_menu_pressed() { Line_style_button.hide(); Line_style_menu.show(); }
static void cb_next(Address, Address);
void next();
static void cb_quit(Address, Address);
void quit();
};
Lines_window::Lines_window(Point xy, int w, int h, const string& title)
:Window(xy, w, h, title),
color_menu(Point(x_max() - 70, 40), 70, 20, Menu::vertical, "color"),
Line_style_menu(Point(x_max()-190,40),70,20,Menu::vertical,"line style"),
menu_button(Point(x_max() - 80, 30), 80, 20, "color menu", cb_menu),
Line_style_button(Point(x_max()-190,30),100,20,"line style menu",cb_Line_style_menu),
next_button(Point(x_max() - 150, 0), 70, 20, "Next point", cb_next),
quit_button(Point(x_max() - 70, 0), 70, 20, "Quit", cb_quit),
next_x(Point(x_max() - 310, 0), 50, 20, "next x:"),
next_y(Point(x_max() - 210, 0), 50, 20, "next y:"),
xy_out(Point(100, 0), 100, 20, "current(x,y):")
{
color_menu.attach(new Button(Point(0, 0), 0, 0, "red", cb_red));
color_menu.attach(new Button(Point(0, 0), 0, 0, "blue", cb_blue));
color_menu.attach(new Button(Point(0, 0), 0, 0, "black", cb_black));
Line_style_menu.attach(new Button(Point(0, 0), 0, 0, "solid", cb_solid));
Line_style_menu.attach(new Button(Point(0, 0), 0, 0, "dash", cb_dash));
Line_style_menu.attach(new Button(Point(0, 0), 0, 0, "dot", cb_dot));
attach(color_menu);
attach(Line_style_menu);
attach(next_button);
attach(quit_button);
attach(next_x);
attach(next_y);
attach(xy_out);
xy_out.put("no point");
color_menu.hide();
Line_style_menu.hide();
attach(menu_button);
attach(Line_style_button);
lines.set_color(Color::black);
attach(lines);
}
void Lines_window::cb_red(Address, Address pw)
{
reference_to(pw).red_pressed();
}
void Lines_window::cb_blue(Address, Address pw)
{
reference_to(pw).blue_pressed();
}
void Lines_window::cb_black(Address, Address pw)
{
reference_to(pw).black_pressed();
}
void Lines_window::cb_solid(Address, Address pw)
{
reference_to(pw).solid_pressed();
}
void Lines_window::cb_dash(Address, Address pw)
{
reference_to(pw).dash_pressed();
}
void Lines_window::cb_dot(Address, Address pw)
{
reference_to(pw).dot_pressed();
}
void Lines_window::cb_menu(Address, Address pw)
{
reference_to(pw).menu_pressed();
}
void Lines_window::cb_Line_style_menu(Address, Address pw)
{
reference_to(pw).Line_style_menu_pressed();
}
void Lines_window::cb_quit(Address, Address pw)
{
reference_to(pw).quit();
}
void Lines_window::quit()
{
hide();
}
void Lines_window::cb_next(Address, Address pw)
{
reference_to(pw).next();
}
void Lines_window::next()
{
int x = next_x.get_int();
int y = next_y.get_int();
lines.add(Point(x, y));
stringstream ss;
ss << '(' << x << ',' << y << ')';
xy_out.put(ss.str());
redraw();
}
int main()
try
{
Lines_window win(Point(100, 100), 600, 400, "lines");
return gui_main();
}
catch (exception& e) {
cerr << "error:" << e.what() << '\n';
keep_window_open();
return 1;
}
catch (...) {
cerr << "Oops:unknown exception!\n";
keep_window_open();
return 2;
}
运行结果: