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

#include <application.h>

Definition

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() {
}
static void run()
Begins running a standard application message loop on the current thread, without a form.
Represents a window or dialog box that makes up an application's user interface.
Definition form.h:40
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 {
} 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);
The exception that is thrown when one of the arguments provided to a method is out of range.
Definition argument_out_of_range_exception.h:18
Represents a Windows button control.
Definition button.h:54
The exception that is thrown in a thread upon cancellation of an operation that the thread was execut...
Definition operation_canceled_exception.h:18
Defines the base class for predefined exceptions in the xtd namespace.
Definition system_exception.h:24
generic_event_handler<> event_handler
Represents the method that will handle an event that has no event data.
Definition event_handler.h:33
#define startup_(main_class)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:49
#define current_stack_frame_
Provides information about the current stack frame.
Definition stack_frame.h:201
@ invalid_argument
Invalid argument.
@ e
The E key.
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition about_box.h:13
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition system_report.h:17

Inherits xtd::static_object.

Static Public Attributes

static event< application, delegate< void(const event_args &)> > application_exit
 Occurs when the application is about to shut down.
 
static event< application, delegate< void(const event_args &)> > enter_thread_modal
 Occurs when the application is about to enter a modal state.
 
static event< application, delegate< void(const event_args &)> > idle
 Occurs when the application finishes processing and is about to enter the idle state.
 
static event< application, delegate< void(const event_args &)> > leave_thread_modal
 Occurs when the application is about to leave a modal state.
 
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.
 

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

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 xtd::ustring 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\company_name\product_name\product_version
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:\Documents and Settings\username directory. For more information, see Accessing Local and Remote Data in ClickOnce Applications.

◆ company_name()

static xtd::ustring 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::ustring::format("The company name is: {0}", application::company_name);
}
static xtd::ustring company_name()
Gets the company name associated with the application.
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition ustring.h:689

◆ 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.

◆ 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.

◆ 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.

◆ 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, enable_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().

◆ executable_name()

static xtd::ustring 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 xtd::ustring 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.

◆ 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 xtd::ustring 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 xtd::ustring 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);
}
static void set_environment_variable(const xtd::ustring &variable, const xtd::ustring &value)
Creates, modifies, or deletes an environment variable stored in the current process.
Definition environment.h:392
static xtd::ustring get_environment_variable(const xtd::ustring &variable)
Retrieves the value of an environment variable from the current process.
Definition environment.h:272
static void restart()
Shuts down the application and starts a new instance immediately.
int parse< int >(const std::string &str, number_styles styles)
Convert a type into a string.
Definition parse.h:103
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition to_string.h:37

◆ 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
application.cpp, busy_box.cpp, busy_dialog.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);
}
Specifies the contextual information about an application thread.
Definition application_context.h:26
const form & main_form() const
Gets the form to use as context.
Definition application_context.h:44
virtual const xtd::ustring & text() const
Gets the text associated with this control.
Definition control.h:650
virtual void show()
Displays the control to the user.
Definition control.h:989
event< control, event_handler > click
Occurs when the control is clicked.
Definition control.h:1065

◆ 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() {
}

◆ startup_path()

static xtd::ustring 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 += [] {
application::use_wait_cursor(true);
for (auto count = 0; count < 500; ++count) {
application::do_events();
sleep_for(milliseconds(10)); // Simulate work...
}
application::use_wait_cursor(false);
};
}
private:
};
int main() {
}
@ button1
The first button on the message box is the default button.

◆ 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 += [] {
application::use_wait_cursor(true);
for (auto count = 0; count < 500; ++count) {
application::do_events();
sleep_for(milliseconds(10)); // Simulate work...
}
application::use_wait_cursor(false);
};
}
private:
};
int main() {
}

◆ user_app_data_path()

static xtd::ustring 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\company_name\product_name\product_version
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: