xtd 0.2.0
context_menu.cpp

demonstrates the use of xtd::forms::context_menu component.

Windows

macOS

Gnome

#include <xtd/drawing/texts>
#include <xtd/forms/application>
#include <xtd/forms/context_menu>
#include <xtd/forms/form>
#include <xtd/forms/list_box>
#include <xtd/forms/menu_images>
#include <xtd/forms/message_box>
using namespace xtd;
using namespace xtd::drawing;
using namespace xtd::forms;
namespace context_menu_example {
class form1 : public form {
public:
form1() {
text("Context menu example");
list_box1.parent(*this);
list_box1.dock(dock_style::fill);
list_box1.context_menu(context_menu1);
}
protected:
void on_form_closing(form_closing_event_args& e) override {
e.cancel(message_box::show(*this, "Are you sure you want exit?", "Close Form", message_box_buttons::yes_no, message_box_icon::question) == dialog_result::no);
};
private:
void on_menu_click(object& sender, const event_args& e) {
list_box1.items().push_back(string::format("{} clicked", as<menu_item>(sender).text()));
list_box1.selected_index(list_box1.items().size() - 1);
if (file_exit_menu_item == sender) application::exit();
}
menu_item file_open_recent_file1_menu_item {"Path/File1", {*this, &form1::on_menu_click}};
menu_item file_open_recent_file2_menu_item {"Path/File2", {*this, &form1::on_menu_click}};
menu_item file_open_recent_file3_menu_item {"Path/File3", {*this, &form1::on_menu_click}};
menu_item file_open_recent_file4_menu_item {"Path/File4", {*this, &form1::on_menu_click}};
menu_item file_new_menu_item {texts::new_(), {*this, &form1::on_menu_click}, menu_images::file_new()};
menu_item file_separator1_menu_item {"-"};
menu_item file_open_menu_item {texts::open(), {*this, &form1::on_menu_click}, menu_images::file_open()};
menu_item file_open_recent_menu_item {"Open recent", {file_open_recent_file1_menu_item, file_open_recent_file2_menu_item, file_open_recent_file3_menu_item, file_open_recent_file4_menu_item}};
menu_item file_close_menu_item {texts::close(), {*this, &form1::on_menu_click}};
menu_item file_separator2_menu_item {"-"};
menu_item file_save_menu_item {texts::save(), {*this, &form1::on_menu_click}, menu_images::file_save()};
menu_item file_save_as_menu_item {texts::save_as(), {*this, &form1::on_menu_click}};
menu_item file_separator3_menu_item {"-"};
menu_item file_page_setup_menu_item {"Page &Seup...", {*this, &form1::on_menu_click}};
menu_item file_print_menu_item {texts::print(), {*this, &form1::on_menu_click}, menu_images::file_print()};
menu_item file_print_preview_menu_item {texts::print_preview(), {*this, &form1::on_menu_click}, menu_images::file_print_preview()};
menu_item file_separator4_menu_item {"-"};
menu_item file_exit_menu_item {texts::exit(), {*this, &form1::on_menu_click}, menu_images::file_exit()};
menu_item edit_undo_menu_item {texts::undo(), {*this, &form1::on_menu_click}, menu_images::edit_undo()};
menu_item edit_redo_menu_item {texts::redo(), {*this, &form1::on_menu_click}, menu_images::edit_redo()};
menu_item edit_separator1_menu_item {"-"};
menu_item edit_cut_menu_item {texts::cut(), {*this, &form1::on_menu_click}, menu_images::edit_cut()};
menu_item edit_copy_menu_item {texts::copy(), {*this, &form1::on_menu_click}, menu_images::edit_copy()};
menu_item edit_paste_menu_item {texts::paste(), {*this, &form1::on_menu_click}, menu_images::edit_paste()};
menu_item edit_separator2_menu_item {"-"};
menu_item edit_select_all_menu_item {texts::select_all(), {*this, &form1::on_menu_click}};
menu_item edit_separator3_menu_item {"-"};
menu_item edit_options_menu_item {texts::options(), {*this, &form1::on_menu_click}};
menu_item view_back_menu_item {texts::back(), {*this, &form1::on_menu_click}, menu_images::view_back()};
menu_item view_forward_menu_item {texts::forward(), {*this, &form1::on_menu_click}, menu_images::view_forward()};
menu_item view_separator1_menu_item {"-"};
menu_item view_show_menu_item {"Show", {*this, &form1::on_menu_click}};
menu_item view_hide_menu_item {"Hide", {*this, &form1::on_menu_click}};
menu_item options_value_a_menu_item {"Value A", {*this, &form1::on_menu_click}, menu_item_kind::check, true};
menu_item options_value_b_menu_item {"Value B", {*this, &form1::on_menu_click}, menu_item_kind::check};
menu_item options_value_c_menu_item {"Value C", {*this, &form1::on_menu_click}, menu_item_kind::check, true};
menu_item options_separator1_menu_item {"-"};
menu_item options_value_d_menu_item {"Value D", {*this, &form1::on_menu_click}, menu_item_kind::radio};
menu_item options_value_e_menu_item {"Value E", {*this, &form1::on_menu_click}, menu_item_kind::radio, true};
menu_item options_value_f_menu_item {"Value F", {*this, &form1::on_menu_click}, menu_item_kind::radio};
menu_item options_separator2_menu_item {"-"};
menu_item options_value_g_menu_item {"Value G", {*this, &form1::on_menu_click}, menu_item_kind::radio};
menu_item options_value_h_menu_item {"Value H", {*this, &form1::on_menu_click}, menu_item_kind::radio};
menu_item options_value_i_menu_item {"Value I", {*this, &form1::on_menu_click}, menu_item_kind::radio};
menu_item help_about_menu_item {texts::about(), {*this, &form1::on_menu_click}};
menu_item file_menu_item {texts::file(), {file_new_menu_item, file_separator1_menu_item, file_open_menu_item, file_open_recent_menu_item, file_close_menu_item, file_separator2_menu_item, file_save_menu_item, file_save_as_menu_item, file_separator3_menu_item, file_page_setup_menu_item, file_print_menu_item, file_print_preview_menu_item, file_separator4_menu_item, file_exit_menu_item}};
menu_item edit_menu_item {texts::edit(), {edit_undo_menu_item, edit_redo_menu_item, edit_separator1_menu_item, edit_cut_menu_item, edit_copy_menu_item, edit_paste_menu_item, edit_separator2_menu_item, edit_select_all_menu_item, edit_separator3_menu_item, edit_options_menu_item}};
menu_item view_menu_item {texts::view(), {view_back_menu_item, view_forward_menu_item, view_separator1_menu_item, view_show_menu_item, view_hide_menu_item}};
menu_item options_menu_item {texts::options(), {options_value_a_menu_item, options_value_b_menu_item, options_value_c_menu_item, options_separator1_menu_item, options_value_d_menu_item, options_value_e_menu_item, options_value_f_menu_item, options_separator2_menu_item, options_value_g_menu_item, options_value_h_menu_item, options_value_i_menu_item}};
menu_item help_menu_item {texts::help(), {help_about_menu_item}};
forms::context_menu context_menu1 {file_menu_item, edit_menu_item, view_menu_item, options_menu_item, help_menu_item};
list_box list_box1;
};
}
auto main() -> int {
application::run(context_menu_example::form1 {});
}
static xtd::string help()
Gets a system-defined text that has a string value of "&Help". This field is constant.
static xtd::string undo()
Gets a system-defined text that has a string value of "&Undo". This field is constant.
static xtd::string new_()
Gets a system-defined text that has a string value of "&New". This field is constant.
static xtd::string save_as()
Gets a system-defined text that has a string value of "Save &As...". This field is constant.
static xtd::string print_preview()
Gets a system-defined text that has a string value of "Print Re&view". This field is constant.
static xtd::string redo()
Gets a system-defined text that has a string value of "&Redo". This field is constant.
static xtd::string edit()
Gets a system-defined text that has a string value of "&Edit". This field is constant.
static xtd::string print()
Gets a system-defined text that has a string value of "&Print...". This field is constant.
static xtd::string save()
Gets a system-defined text that has a string value of "&Save". This field is constant.
static xtd::string close()
Gets a system-defined text that has a string value of "&Close". This field is constant.
static xtd::string cut()
Gets a system-defined text that has a string value of "Cu&t". This field is constant.
static xtd::string forward()
Gets a system-defined text that has a string value of "&Forward". This field is constant.
static xtd::string file()
Gets a system-defined text that has a string value of "&File". This field is constant.
static xtd::string options()
Gets a system-defined text that has a string value of "&Options". This field is constant.
static xtd::string copy()
Gets a system-defined text that has a string value of "&Copy". This field is constant.
static xtd::string exit()
Gets a system-defined text that has a string value of "E&xit". This field is constant.
static xtd::string open()
Gets a system-defined text that has a string value of "&Open...". This field is constant.
static xtd::string paste()
Gets a system-defined text that has a string value of "&Paste". This field is constant.
static xtd::string view()
Gets a system-defined text that has a string value of "&View". This field is constant.
static xtd::string select_all()
Gets a system-defined text that has a string value of "Select &All". This field is constant.
static xtd::string back()
Gets a system-defined text that has a string value of "&Back". This field is constant.
static xtd::string about()
Gets a system-defined text that has a string value of "&About". This field is constant.
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition event_args.hpp:18
static void exit()
Informs all message pumps that they must terminate, and then closes all application windows after the...
static void run()
Begins running a standard application message loop on the current thread, without a form.
Represents a shortcut menu.
Definition context_menu.hpp:36
Provides data for the form_closing event.
Definition form_closing_event_args.hpp:22
Represents a window or dialog box that makes up an application's user interface.
Definition form.hpp:54
static xtd::drawing::image edit_copy()
Edit copy image object.
Definition menu_images.hpp:184
static xtd::drawing::image file_print()
File print image object.
Definition menu_images.hpp:82
static xtd::drawing::image file_print_preview()
File print preview image object.
Definition menu_images.hpp:99
static xtd::drawing::image file_save()
File save image object.
Definition menu_images.hpp:65
static xtd::drawing::image file_exit()
File exit image object.
Definition menu_images.hpp:116
static xtd::drawing::image file_new()
File new image object.
Definition menu_images.hpp:31
static xtd::drawing::image file_open()
File open image object.
Definition menu_images.hpp:48
static xtd::drawing::image edit_cut()
Edit cut image object.
Definition menu_images.hpp:167
static xtd::drawing::image edit_redo()
Edit undo image object.
Definition menu_images.hpp:150
static xtd::drawing::image edit_paste()
Edit paste image object.
Definition menu_images.hpp:201
static xtd::drawing::image view_forward()
View forward image object.
Definition menu_images.hpp:235
static xtd::drawing::image view_back()
View back image object.
Definition menu_images.hpp:218
static xtd::drawing::image edit_undo()
Edit undo image object.
Definition menu_images.hpp:133
Represents an individual item that is displayed within a main_menu or context_menu.
Definition menu_item.hpp:31
static dialog_result show(const iwin32_window &owner)
Displays a message box in front of the specified window.
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
@ e
The E key.
Definition console_key.hpp:96
@ check
Check menu item.
Definition menu_item_kind.hpp:25
@ radio
Radio menu item.
Definition menu_item_kind.hpp:27
@ question
The message box contains a symbol consisting of a question mark in a circle. The question-mark messag...
Definition message_dialog_icon.hpp:28
@ no
The dialog box return value is No (usually sent from a button labeled No).
Definition dialog_result.hpp:59
@ fill
All the control's edges are docked to the all edges of its containing control and sized appropriately...
Definition dock_style.hpp:35
@ yes_no
The message box contains Yes and No buttons.
Definition message_dialog_buttons.hpp:32
The xtd::drawing namespace provides access to GDI+ basic graphics functionality. More advanced functi...
Definition actions_system_images.hpp:10
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition texts.hpp:217
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8