xtd 0.2.0
Loading...
Searching...
No Matches
wiggly.cpp

The Wiggly example shows how to animate a user control using timer and timer::tick event. In addition, the example demonstrates how to use graphics::measure_string to determine the size of text on screen.

Windows

macOS

Gnome

#include <xtd/drawing/system_colors>
#include <xtd/forms/application>
#include <xtd/forms/form>
#include <xtd/forms/text_box>
#include <xtd/forms/timer>
#include <xtd/forms/user_control>
using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::drawing;
using namespace xtd::forms;
namespace wiggly_example {
class wiggly : public user_control {
public:
wiggly() {
back_color(system_colors::window());
font({font(), font().size() + 12});
timer.interval(60_ms);
timer.tick += event_handler(*this, &wiggly::on_timer_tick);
timer.enabled(true);
}
protected:
void on_paint(paint_event_args& e) override {
static const auto sins = list {0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38};
auto pos = point {(e.clip_rectangle().size().width() - as<int>(e.graphics().measure_string(text(), font()).width())) / 2, (e.clip_rectangle().size().height() - as<int>(e.graphics().measure_string(text(), font()).height())) / 2};
auto wiggly_text = as<std::u32string>(text());
for (auto i = 0_z; i < wiggly_text.length(); i++) {
auto index = (step + i) % sins.size();
e.graphics().draw_string(string::format("{}", wiggly_text[i]), font(), solid_brush {color::from_hsb(360.0f / sins.size() * index, 1.0f, 0.75f)}, point::subtract(pos, point(0, sins[index] * font().height() / 400)));
pos.x(pos.x() + as<int>(e.graphics().measure_string(string::format("{}", wiggly_text[i]), font()).width()));
}
}
private:
void on_timer_tick(object& sender, const event_args& e) {
step++;
invalidate();
}
int step = 0;
};
class form1 : public form {
public:
form1() {
text("Wiggly");
client_size({330, 130});
controls().push_back_range({wiggly, text_box});
text_box.location({20, 90});
wiggly.text(text_box.text());
};
text_box.text("Habemus papam");
text_box.anchor(anchor_styles::left | anchor_styles::bottom | anchor_styles::right);
wiggly.bounds({20, 20, 290, 60});
wiggly.anchor(anchor_styles::top | anchor_styles::left | anchor_styles::right | anchor_styles::bottom);
wiggly.text(text_box.text());
}
private:
wiggly_example::wiggly wiggly;
};
}
auto main() -> int {
application::run(wiggly_example::form1 {});
}
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:71
Defines a particular format for text, including font face, size, and style attributes....
Definition font.hpp:45
float size() const noexcept
Gets the em-size of this xtd::drawing::font measured in the units specified by the unit property.
Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional ...
Definition point.hpp:54
Defines a xtd::drawing::brush of a single color. Brushes are used to fill graphics shapes,...
Definition solid_brush.hpp:30
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition event_args.hpp:18
virtual int32 width() const noexcept
Gets the width of the control.
event< control, event_handler > text_changed
Occurs when the value of the xtd::forms::control::text property changes.
Definition control.hpp:1851
virtual drawing::point location() const noexcept
Gets the coordinates of the upper-left corner of the control relative to the upper-left corner of its...
virtual anchor_styles anchor() const noexcept
Gets the edges of the container to which a control is bound and determines how a control is resized w...
bool focus()
Sets input focus to the control.
Represents a window or dialog box that makes up an application's user interface.
Definition form.hpp:54
Provides data for the xtd::forms::control::paint event.
Definition paint_event_args.hpp:30
Represents a standard Windows text box.
Definition text_box.hpp:31
const xtd::string & text() const noexcept override
Gets the text associated with this control.
Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in...
Definition timer.hpp:38
virtual time_span interval() const noexcept
Gets the time, in milliseconds, before the tick event is raised relative to the last occurrence of th...
virtual bool enabled() const noexcept
Gets whether the timer is running.
event< timer, event_handler > tick
Occurs when the specified timer interval has elapsed and the timer is enabled.
Definition timer.hpp:130
Represents a standard Windows user control.
Definition user_control.hpp:27
generic_event_handler<> event_handler
Represents the method that will handle an event that has no event data.
Definition event_handler.hpp:32
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:15
The xtd::drawing namespace provides access to GDI+ basic graphics functionality. More advanced functi...
Definition brush.hpp:18
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition xtd_about_box.hpp:12
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10