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.
#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>
namespace wiggly_example {
public:
wiggly() {
back_color(system_colors::window());
}
protected:
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.
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);
}
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
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.
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 actions_system_images.hpp:11
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10