xtd - Reference Guide  0.1.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Static Public Member Functions | Static Public Attributes | List of all members
xtd::forms::application Class Referencefinal

Provides static methods and properties to manage an application, such as methods to start and stop an application, to process Windows messages, and methods to get information about an application. This class cannot be inherited. More...

#include <application.h>

Static Public Member Functions

static void add_message_filter (const imessage_filter &value)
 Adds a message filter to monitor Windows messages as they are routed to their destinations. More...
 
static bool allow_quit ()
 Gets a value indicating whether the caller can quit this application. More...
 
static std::string common_app_data_path ()
 Gets the path for the application data that is shared among all users. More...
 
static std::string company_name ()
 Gets the company name associated with the application. More...
 
static bool dark_mode_enabled ()
 Return true if dark mode is enabled for the application; otherwise return false. More...
 
static void do_events ()
 Processes all Windows messages currently in the message queue. More...
 
static void enable_button_images ()
 Enables button images for the application. More...
 
static void enable_dark_mode ()
 Enables dark mode for the application. More...
 
static void enable_light_mode ()
 Enables light mode for the application. More...
 
static void enable_menu_images ()
 Enables menu images for the application. More...
 
static void enable_visual_styles ()
 Enables visual styles for the application. More...
 
static std::string executable_name ()
 Gets the executable name for the executable file that started the application, including the executable extension. More...
 
static std::string executable_path ()
 Gets the path for the executable file that started the application, including the executable name. More...
 
static void exit ()
 Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. More...
 
static void exit (cancel_event_args &e)
 Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. More...
 
static void exit_thread ()
 Exits the message loop on the current thread and closes all windows on the thread. More...
 
static bool light_mode_enabled ()
 Return true if dark mode is enabled for the application; otherwise return false. More...
 
static bool message_loop ()
 Gets a value indicating whether a message loop exists on this thread. More...
 
static const form_collection open_forms ()
 Gets a collection of open forms owned by the application. More...
 
static std::string product_name ()
 Gets the product name associated with this application. More...
 
static std::string product_version ()
 Gets the product version associated with this application. More...
 
static void raise_idle (const event_args &e)
 Raises the Idle event. More...
 
static void register_message_loop_callback (message_loop_callback callback)
 Registers a callback for checking whether the message loop is running in hosted environments. More...
 
static void remove_message_filter (const imessage_filter &value)
 Removes a message filter from the message pump of the application. More...
 
static void restart ()
 Shuts down the application and starts a new instance immediately. More...
 
static void run ()
 Begins running a standard application message loop on the current thread, without a form. More...
 
static void run (application_context &context)
 Begins running a standard application message loop on the current thread, with an application_context. More...
 
static void run (const form &main_form)
 Begins running a standard application message loop on the current thread, and makes the specified form visible. More...
 
static std::string startup_path ()
 Gets the path for the executable file that started the application, not including the executable name. More...
 
static void theme (const std::string &theme_name)
 
static void theme (const xtd::forms::theme &theme)
 
static const xtd::forms::themetheme ()
 
static const std::vector< std::string > & theme_names ()
 
static bool use_visual_styles ()
 Gets a value that indicates whether visual styles are enabled for the application. More...
 
static bool use_wait_cursor ()
 Gets whether the wait cursor is used for all open forms of the application. More...
 
static void use_wait_cursor (bool use_wait_cursor)
 Sets whether the wait cursor is used for all open forms of the application. More...
 
static std::string user_app_data_path ()
 Gets the path for the application data of a user. More...
 

Static Public Attributes

static event< application, delegate< void(const event_args &)> > application_exit
 Occurs when the application is about to shut down. More...
 
static event< application, delegate< void(const event_args &)> > enter_thread_modal
 Occurs when the application is about to enter a modal state. More...
 
static event< application, delegate< void(const event_args &)> > idle
 Occurs when the application finishes processing and is about to enter the idle state. More...
 
static event< application, delegate< void(const event_args &)> > leave_thread_modal
 Occurs when the application is about to leave a modal state. More...
 
static event< application, delegate< void(const event_args &)> > thread_exit
 Occurs when a thread is about to shut down. When the main thread for an application is about to be shut down, this event is raised first, followed by an application_exit event. More...
 

Detailed Description

Provides static methods and properties to manage an application, such as methods to start and stop an application, to process Windows messages, and methods to get information about an application. This class cannot be inherited.

Library
xtd.forms
Remarks
The application class has methods to start and stop applications and threads, and to process Windows messages, as follows:
  • run() starts an application message loop on the current thread and, optionally, makes a form visible.
  • exit() or exit_thread() stops a message loop.
  • do_events() processes messages while your program is in a loop.
  • add_message_filter() adds a message filter to the application message pump to monitor Windows messages.
You cannot create an instance of this class.
Examples
The following code example demonstrate the use of application class.
#include <xtd/xtd>
int main() {
}
Windows
application_w.png

application_wd.png
macOS
application_m.png

application_md.png
Gnome
application_g.png

application_gd.png
Examples
How to manage exception with application class.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::forms;
class main_form : public form {
public:
static void main() {
application::run(main_form());
}
main_form() {
text("application and exception example");
// uncomment next line to throw error and catch it in main entry point
//throw system_exception(current_stack_frame_);
generate_handled_exception_button.auto_size(true);
generate_handled_exception_button.location({10, 10});
generate_handled_exception_button.parent(*this);
generate_handled_exception_button.text("Generate handled exception");
generate_handled_exception_button.click += event_handler(*this, &main_form::generate_handled_exception);
generate_exception_button.auto_size(true);
generate_exception_button.location({10, 40});
generate_exception_button.parent(*this);
generate_exception_button.text("Generate exception");
generate_exception_button.click += event_handler(*this, &main_form::generate_exception);
generate_system_exception_button.auto_size(true);
generate_system_exception_button.location({10, 70});
generate_system_exception_button.parent(*this);
generate_system_exception_button.text("Generate system exception");
generate_system_exception_button.click += event_handler(*this, &main_form::generate_system_exception);
generate_unknown_exception_button.auto_size(true);
generate_unknown_exception_button.location({10, 100});
generate_unknown_exception_button.parent(*this);
generate_unknown_exception_button.text("Generate unknown exception");
generate_unknown_exception_button.click += event_handler(*this, &main_form::generate_unknown_exception);
}
private:
void generate_handled_exception() {
try {
throw operation_canceled_exception(current_stack_frame_);
} catch(const xtd::system_exception& e) {
message_box::show(*this, e.message(), ustring::format("Exception {} handled", e.name()));
}
}
void generate_exception() {throw invalid_argument("Invalid argument");}
void generate_system_exception() {throw argument_out_of_range_exception(current_stack_frame_);}
void generate_unknown_exception() {throw "Unknown exception occured";}
button generate_handled_exception_button;
button generate_exception_button;
button generate_system_exception_button;
button generate_unknown_exception_button;
};
startup_(main_form);
Windows
application_and_exception_w.png

application_and_exception_wd.png
macOS
application_and_exception_m.png

application_and_exception_md.png
Gnome
application_and_exception_g.png

application_and_exception_gd.png

Member Function Documentation

◆ add_message_filter()

static void xtd::forms::application::add_message_filter ( const imessage_filter value)
static

Adds a message filter to monitor Windows messages as they are routed to their destinations.

Parameters
Theimplementation of the imessage_filter interface you want to install.
Remarks
Use a message filter to prevent specific events from being raised or to perform special operations for an event before it is passed to an event handler. Message filters are unique to a specific thread.

◆ allow_quit()

static bool xtd::forms::application::allow_quit ( )
static

Gets a value indicating whether the caller can quit this application.

Returns
true if the caller can quit this application; otherwise, false.
Remarks
This method returns false if it is called from a control being hosted within a Web browser. Thus, the control cannot quit the application.

◆ common_app_data_path()

static std::string xtd::forms::application::common_app_data_path ( )
static

Gets the path for the application data that is shared among all users.

Returns
The path for the application data that is shared among all users.
Remarks
If a path does not exist, one is created in the following format: base_path
product_version first looks to see if the assembly containing the main executable has the AssemblyInformationalVersion attribute on it. If this attribute exists, it is used for both product_version and common_app_data_path. If this attribute does not exist, both properties use the version of the executable file instead.
The path will be different depending on whether the Windows Forms application is deployed using ClickOnce. ClickOnce applications are stored in a per-user application cache in the C: and Settings directory. For more information, see Accessing Local and Remote Data in ClickOnce Applications.

◆ company_name()

static std::string xtd::forms::application::company_name ( )
static

Gets the company name associated with the application.

Returns
The company name.
Remarks
company_name is taken from the application_informations containing the current application. You can set it by setting application_informations::company_name property. For more information, see application_informations.
Examples
The following code example gets this property and displays its value in a text box. The example requires that textBox1 has been placed on a form.
void PrintCompanyName() {
textBox1.Text(xtd;;strings::format("The company name is: {0}", application::company_name);
}

◆ dark_mode_enabled()

static bool xtd::forms::application::dark_mode_enabled ( )
static

Return true if dark mode is enabled for the application; otherwise return false.

◆ do_events()

static void xtd::forms::application::do_events ( )
static

Processes all Windows messages currently in the message queue.

Remarks
When you run a Windows form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.
If you call do_events in your code, your application can handle the other events. For example, if you have a form that adds data to a list_box and add do_events to your code, your form repaints when another window is dragged over it. If you remove do_events from your code, your form will not repaint until the click event handler of the button is finished executing.
Typically, you use this method in a loop to process messages.
Warning
Calling this method causes the current thread to be suspended while all waiting window messages are processed. If a message causes an event to be triggered, then other areas of your application code may execute. This can cause your application to exhibit unexpected behaviors that are difficult to debug. If you perform operations or computations that take a long time, it is often preferable to perform those operations on a new thread.
Examples:
busy_box.cpp, and busy_dialog.cpp.

◆ enable_button_images()

static void xtd::forms::application::enable_button_images ( )
static

Enables button images for the application.

Remarks
This method has an effect only on linux.
Examples:
command_link_button.cpp.

◆ enable_dark_mode()

static void xtd::forms::application::enable_dark_mode ( )
static

Enables dark mode for the application.

◆ enable_light_mode()

static void xtd::forms::application::enable_light_mode ( )
static

Enables light mode for the application.

◆ enable_menu_images()

static void xtd::forms::application::enable_menu_images ( )
static

Enables menu images for the application.

Remarks
This method has an effect only on linux.
Examples:
main_menu.cpp, and main_menu_create_standard_items.cpp.

◆ enable_visual_styles()

static void xtd::forms::application::enable_visual_styles ( )
static

Enables visual styles for the application.

Remarks
This method enables visual styles for the application. Visual styles are the colors, fonts, and other visual elements that form an operating system theme. Controls will draw with visual styles if the control and the operating system support it. To have an effect, nnable_visual_styles() must be called before creating any controls in the application; typically, enable_visual_styles() is the first line in the Main function. A separate manifest is not required to enable visual styles when calling enable_visual_styles().
Examples:
check_box.cpp, and toggle_button.cpp.

◆ executable_name()

static std::string xtd::forms::application::executable_name ( )
static

Gets the executable name for the executable file that started the application, including the executable extension.

Returns
The executable name and executable name for the executable file that started the application.

◆ executable_path()

static std::string xtd::forms::application::executable_path ( )
static

Gets the path for the executable file that started the application, including the executable name.

Returns
The path and executable name for the executable file that started the application.

◆ exit() [1/2]

static void xtd::forms::application::exit ( )
static

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

Remarks
The exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit. The exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call exit_thread.
Exit raises the following events and performs the associated conditional actions:
  • A form_closing event is raised for every form represented by the open_forms property. This event can be canceled by setting the cancel property of their form_closing_event_args parameter to true.
  • If one of more of the handlers cancels the event, then exit returns without further action. Otherwise, a form_closed event is raised for every open form, then all running message loops and forms are closed.
Examples:
exception_box.cpp, exception_dialog.cpp, main_menu.cpp, and main_menu_create_standard_items.cpp.

◆ exit() [2/2]

static void xtd::forms::application::exit ( cancel_event_args e)
static

Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed.

Parameters
eReturns whether any Form within the application cancelled the exit.
Remarks
The exit method stops all running message loops on all threads and closes all windows of the application. This method does not necessarily force the application to exit. The exit method is typically called from within a message loop, and forces Run to return. To exit a message loop for the current thread only, call exit_thread.
Exit raises the following events and performs the associated conditional actions:
  • A form_closing event is raised for every form represented by the open_forms property. This event can be canceled by setting the cancel property of their form_closing_event_args parameter to true.
  • If one of more of the handlers cancels the event, then exit returns without further action. Otherwise, a form_closed event is raised for every open form, then all running message loops and forms are closed.

◆ exit_thread()

static void xtd::forms::application::exit_thread ( )
static

Exits the message loop on the current thread and closes all windows on the thread.

Remarks
Use this method to exit the message loop of the current thread. This method causes the call to run for the current thread to return. To exit the entire application, call exit.

◆ light_mode_enabled()

static bool xtd::forms::application::light_mode_enabled ( )
static

Return true if dark mode is enabled for the application; otherwise return false.

◆ message_loop()

static bool xtd::forms::application::message_loop ( )
static

Gets a value indicating whether a message loop exists on this thread.

Returns
true if a message loop exists; otherwise, false.

◆ open_forms()

static const form_collection xtd::forms::application::open_forms ( )
static

Gets a collection of open forms owned by the application.

Returns
A form_collection containing all the currently open forms owned by this application.
Remarks
The open_forms property represents a read-only collection of forms owned by the application.

◆ product_name()

static std::string xtd::forms::application::product_name ( )
static

Gets the product name associated with this application.

Returns
The product name.
Remarks
product_name is taken from the application_informations containing the current application. You can set it by setting application_informations::product_name property. For more information, see application_informations.

◆ product_version()

static std::string xtd::forms::application::product_version ( )
static

Gets the product version associated with this application.

Returns
The product version.
Remarks
product_version is taken from the application_informations containing the current application. You can set it by setting application_informations::product_version property. For more information, see application_informations.

◆ raise_idle()

static void xtd::forms::application::raise_idle ( const event_args e)
static

Raises the Idle event.

Parameters
eThe event_args objects to pass to the idle event.

◆ register_message_loop_callback()

static void xtd::forms::application::register_message_loop_callback ( message_loop_callback  callback)
static

Registers a callback for checking whether the message loop is running in hosted environments.

Parameters
callbackThe method to call when Windows Forms needs to check if the hosting environment is still sending messages.
Remarks
This method is used when hosting Windows Forms in another environment. In hosted environments, the message_loop property will always return false if Windows Forms is not processing messages. Use this callback to tell Windows Forms if the hosting environment is still processing messages.

◆ remove_message_filter()

static void xtd::forms::application::remove_message_filter ( const imessage_filter value)
static

Removes a message filter from the message pump of the application.

Parameters
valueThe implementation of the imessage_filter to remove from the application.
Remarks
You can remove a message filter when you no longer want to capture Windows messages before they are dispatched.

◆ restart()

static void xtd::forms::application::restart ( )
static

Shuts down the application and starts a new instance immediately.

Remarks
Applications are restarted in the context in which they were initially run.
If your application was originally supplied command-line options when it first executed, restart will launch the application again with the same options.
Examples
The following code example demonstrate the use of application restart method.
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::forms;
int main() {
auto restart_count = 0;
if (!environment::get_environment_variable("applicatioon_restart_count").empty())
restart_count = parse<int>(environment::get_environment_variable("applicatioon_restart_count"));
auto main_form = control::create<form>(ustring::format("Restart {} times", restart_count));
auto restart_button = control::create<button>(*main_form, "Restart", {10 ,10});
restart_button->click += application::restart;
environment::set_environment_variable("applicatioon_restart_count", to_string(restart_count + 1));
application::run(*main_form);
}
Windows
application_restart_w.png

application_restart_wd.png
macOS
application_restart_m.png

application_restart_md.png
Gnome
application_restart_g.png

application_restart_gd.png

◆ run() [1/3]

static void xtd::forms::application::run ( )
static

Begins running a standard application message loop on the current thread, without a form.

Remarks
In a Win32-based or Windows Forms application, a message loop is a routine in code that processes user events, such as mouse clicks and keyboard strokes. Every running Windows-based application requires an active message loop, called the main message loop. When the main message loop is closed, the application exits. In Windows Forms, this loop is closed when the exit method is called, or when the exit_thread method is called on the thread that is running the main message loop.
Most Windows Forms developers will not need to use this version of the method. You should use the run(const form&) overload to start an application with a main form, so that the application terminates when the main form is closed. For all other situations, use the run(application_context&) overload, which supports supplying an application_context object for better control over the lifetime of the application.
Examples
The following code example demonstrate the use of application run method.
#include <xtd/xtd>
using namespace xtd::forms;
int main() {
}
Examples:
about_box.cpp, about_dialog.cpp, about_dialog_system.cpp, application.cpp, application_context.cpp, busy_box.cpp, busy_dialog.cpp, button.cpp, buttons.cpp, check_box.cpp, check_boxes.cpp, command_link_button.cpp, date_time_picker.cpp, debug_form.cpp, enable_debug.cpp, exception_box.cpp, exception_dialog.cpp, font_dialog.cpp, font_picker.cpp, group_box.cpp, group_box_and_check_box.cpp, input_box.cpp, input_dialog.cpp, input_dialog_multiline.cpp, input_dialog_password.cpp, label.cpp, main_menu.cpp, main_menu_create_standard_items.cpp, message_box.cpp, message_dialog.cpp, numeric_up_down.cpp, panel.cpp, radio_button.cpp, radio_buttons.cpp, save_file_dialog.cpp, screen.cpp, tab_control.cpp, text_box.cpp, texts.cpp, toggle_button.cpp, toggle_buttons.cpp, trace_form.cpp, track_bar.cpp, up_down_button.cpp, and use_wait_cursor.cpp.

◆ run() [2/3]

static void xtd::forms::application::run ( application_context context)
static

Begins running a standard application message loop on the current thread, with an application_context.

Parameters
contextAn ApplicationContext in which the application is run.
Remarks
The message loop runs until Exit or exit_thread is called or the thread_exit event is raised on the context object.
Examples
The following code example demonstrate the use of application run method.
#include <xtd/xtd>
using namespace xtd::forms;
int main() {
form form1;
form1.click += [&] {
context.main_form(form1);
};
form1.text("Form 1 (Click the client area to set form as the main form)");
form1.show();
form form2;
form2.click += [&] {
context.main_form(form2);
};
form2.text("Form 2 (Click the client area to set form as the main form)");
form2.show();
form form3;
form3.click += [&] {
context.main_form(form3);
};
form3.text("Form 3 (Click the client area to set form as the main form)");
form3.show();
// if no client area form clicked, the application will not exit when you close the forms.
application::run(context);
}
Windows
application_context_w.png

application_context_wd.png
macOS
application_context_m.png

application_context_md.png
Gnome
application_context_g.png

application_context_gd.png

◆ run() [3/3]

static void xtd::forms::application::run ( const form main_form)
static

Begins running a standard application message loop on the current thread, and makes the specified form visible.

Parameters
main_formA form that represents the form to make visible.
Remarks
Typically, the main function of an application calls this method and passes to it the main window of the application.
This method adds an event handler to the main_form parameter for the closed event. The event handler calls exit_thread to clean up the application.
Examples
The following code example demonstrate the use of application run method.
#include <xtd/xtd>
int main() {
}
Windows
application_w.png

application_wd.png
macOS
application_m.png

application_md.png
Gnome
application_g.png

application_gd.png

◆ startup_path()

static std::string xtd::forms::application::startup_path ( )
static

Gets the path for the executable file that started the application, not including the executable name.

Returns
The path for the executable file that started the application.

◆ use_visual_styles()

static bool xtd::forms::application::use_visual_styles ( )
static

Gets a value that indicates whether visual styles are enabled for the application.

Returns
true if visual styles are enabled; otherwise, false.
Remarks
The visual styles can be enabled by calling enable_visual_styles().

◆ use_wait_cursor() [1/2]

static bool xtd::forms::application::use_wait_cursor ( )
static

Gets whether the wait cursor is used for all open forms of the application.

Returns
true is the wait cursor is used for all open forms; otherwise, false.
Remarks
When this property is set to true, the use_wait_cursor property of all open forms in the application will be set to true. This call will not return until this property has been set on all forms. Use this property when you have a long-running operation, and want to indicate in all application forms that the operation is still processing.
Examples
The following code example demonstrate the use of application use wait cursor property.
#include <thread>
#include <xtd/xtd>
using namespace std::chrono;
using namespace std::this_thread;
using namespace xtd::forms;
class form1 : public form {
public:
form1() {
text("Application use wait cursor example");
button1.auto_size(true);
button1.location({10, 10});
button1.parent(*this);
button1.text("Do something...");
button1.click += [] {
for (auto count = 0; count < 500; ++count) {
sleep_for(milliseconds(10)); // Simulate work...
}
};
}
private:
};
int main() {
}
Windows
application_use_wait_cursor_w.png

application_use_wait_cursor_wd.png
macOS
application_use_wait_cursor_m.png

application_use_wait_cursor_md.png
Gnome
application_use_wait_cursor_g.png

application_use_wait_cursor_gd.png

◆ use_wait_cursor() [2/2]

static void xtd::forms::application::use_wait_cursor ( bool  use_wait_cursor)
static

Sets whether the wait cursor is used for all open forms of the application.

Parameters
use_wait_cursortrue is the wait cursor is used for all open forms; otherwise, false.
Remarks
When this property is set to true, the use_wait_cursor property of all open forms in the application will be set to true. This call will not return until this property has been set on all forms. Use this property when you have a long-running operation, and want to indicate in all application forms that the operation is still processing.
Examples
The following code example demonstrate the use of application use wait cursor property.
#include <thread>
#include <xtd/xtd>
using namespace std::chrono;
using namespace std::this_thread;
using namespace xtd::forms;
class form1 : public form {
public:
form1() {
text("Application use wait cursor example");
button1.auto_size(true);
button1.location({10, 10});
button1.parent(*this);
button1.text("Do something...");
button1.click += [] {
for (auto count = 0; count < 500; ++count) {
sleep_for(milliseconds(10)); // Simulate work...
}
};
}
private:
};
int main() {
}
Windows
application_use_wait_cursor_w.png

application_use_wait_cursor_wd.png
macOS
application_use_wait_cursor_m.png

application_use_wait_cursor_md.png
Gnome
application_use_wait_cursor_g.png

application_use_wait_cursor_gd.png

◆ user_app_data_path()

static std::string xtd::forms::application::user_app_data_path ( )
static

Gets the path for the application data of a user.

Returns
The path for the application data of a user.
Remarks
If a path does not exist, one is created in the following format: base_path
Data stored in this path is part of user profile that is enabled for roaming. A roaming user works on more than one computer in a network. The user profile for a roaming user is kept on a server on the network and is loaded onto a system when the user logs on. For a user profile to be considered for roaming, the operating system must support roaming profiles and it must be enabled.
A typical base path is "C:\Documents and Settings\username\Application Data".

The documentation for this class was generated from the following file: