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.
Loading...
Searching...
No Matches
user_control.cpp

The following code example demonstrate the use of user control.

Windows

macOS

Gnome

#include <vector>
#include <xtd/xtd>
using namespace std;
using namespace xtd;
using namespace xtd::drawing;
using namespace xtd::forms;
namespace examples {
enum class operation_status {
none,
running,
succeed,
failed
};
class operation : public user_control {
public:
operation() {size_ = default_size();}
operation_status status() const {return status_;}
void status(operation_status status) {
if (status_ != status) {
status_ = status;
invalidate();
}
}
protected:
void on_paint(paint_event_args& e) override {
user_control::on_paint(e);
auto status_rectangle = rectangle {1, 1, 28, 28};
e.graphics().fill_rectangle(solid_brush(color::dark(back_color())), status_rectangle);
auto status_string = std::map<operation_status, ustring> {{operation_status::none, u8""}, {operation_status::running, u8"☞"}, {operation_status::succeed, u8"√"}, {operation_status::failed, u8"X"}}[status()];
auto status_brush = solid_brush(std::map<operation_status, color> {{operation_status::none, fore_color()}, {operation_status::running, color::yellow}, {operation_status::succeed, color::green}, {operation_status::failed, color::red}}[status()]);
e.graphics().draw_string(status_string, drawing::font(default_font(), 18, font_style::italic), status_brush, status_rectangle, string_format().alignment(string_alignment::center).line_alignment(string_alignment::center));
e.graphics().draw_line(pen(color::dark(color::dark(back_color()))), status_rectangle.location(), point(status_rectangle.width(), 1));
e.graphics().draw_line(pen(color::dark(color::dark(back_color()))), status_rectangle.location(), point(1, status_rectangle.height()));
e.graphics().draw_line(pen(color::light(color::light(back_color()))), point(1, status_rectangle.height()), point(status_rectangle.width(), status_rectangle.height()));
e.graphics().draw_line(pen(color::light(color::light(back_color()))), point(status_rectangle.width(), 1), point(status_rectangle.width(), status_rectangle.height()));
e.graphics().draw_string(text(), default_font(), system_brushes::control_text(), 35, e.graphics().measure_string(text(), default_font()).height() / 2);
}
drawing::size default_size() const override {return {150, 30};}
private:
operation_status status_ = operation_status::none;
};
class form1 : public form {
public:
form1() {
text("User control example");
operations[0].parent(*this);
operations[0].status(operation_status::succeed);
operations[0].text("First operation");
operations[0].location({5, 5});
operations[0].click += event_handler(*this, &form1::on_operation_click);
operations[1].parent(*this);
operations[1].status(operation_status::failed);
operations[1].text("Second operation");
operations[1].location({5, 35});
operations[1].click += event_handler(*this, &form1::on_operation_click);
operations[2].parent(*this);
operations[2].status(operation_status::running);
operations[2].text("Third operation");
operations[2].location({5, 65});
operations[2].click += event_handler(*this, &form1::on_operation_click);
operations[3].parent(*this);
operations[3].status(operation_status::none);
operations[3].text("Fourth operation");
operations[3].location({5, 95});
operations[3].click += event_handler(*this, &form1::on_operation_click);
operations[4].parent(*this);
operations[4].status(operation_status::none);
operations[4].text("Fifth operation");
operations[4].location({5, 125});
operations[4].click += event_handler(*this, &form1::on_operation_click);
}
private:
void on_operation_click(object& sender, const event_args& e) {
examples::operation& operation = as<examples::operation&>(sender);
switch (operation.status()) {
case operation_status::none: operation.status(operation_status::running); break;
case operation_status::running: operation.status(operation_status::succeed); break;
case operation_status::succeed: operation.status(operation_status::failed); break;
case operation_status::failed: operation.status(operation_status::none); break;
}
}
vector<operation> operations {5};
};
}
int main() {
application::run(examples::form1());
}
Defines a particular format for text, including font face, size, and style attributes....
Definition: font.h:39
Defines an object used to draw lines and curves. This class cannot be inherited.
Definition: pen.h:29
Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional ...
Definition: point.h:48
Stores a set of four integers that represent the location and size of a rectangle.
Definition: rectangle.h:25
Stores an ordered pair of integers, which specify a height and width.
Definition: size.h:25
Defines a xtd::drawing::brush of a single color. Brushes are used to fill graphics shapes,...
Definition: solid_brush.h:22
Encapsulates text layout information (such as alignment, orientation and tab stops) display manipulat...
Definition: string_format.h:25
xtd::drawing::string_alignment alignment() const
Gets horizontal alignment of the string.
Definition: string_format.h:74
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition: event_args.h:18
Represents a window or dialog box that makes up an application's user interface.
Definition: form.h:40
Provides data for the paint event.
Definition: paint_event_args.h:26
Represents a standard Windows user control.
Definition: user_control.h:21
generic_event_handler<> event_handler
Represents the method that will handle an event that has no event data.
Definition: event_handler.h:33
The xtd::drawing namespace provides access to GDI+ basic graphics functionality. More advanced functi...
Definition: bitmap.h:11
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