xtd 0.2.0
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 = text().to_u32string();
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});
text_box.text_changed += [&] {
wiggly.text(text_box.text());
};
text_box.text("Habemus papam");
text_box.width(290);
text_box.focus();
wiggly.bounds({20, 20, 290, 60});
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:80
static xtd::drawing::color from_hsb(float hue, float saturation, float brightness) noexcept
Creates a xtd::drawing::color class from the three HSV component (hue, saturation,...
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.
Defines a xtd::drawing::brush of a single color. Brushes are used to fill graphics shapes,...
Definition solid_brush.hpp:29
static xtd::drawing::color window()
Gets a xtd::drawing::color structure that is the color of the background in the client area of a wind...
Represents the base class for classes that contain event data, and provides a value to use for events...
Definition event_args.hpp:18
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.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
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
@ text_box
The system-defined color of the accent color (macos specific. On other platform is same as window).
Definition known_color.hpp:501
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
@ i
The I key.
Definition console_key.hpp:104
@ e
The E key.
Definition console_key.hpp:96
@ height
Specifies that the height of the control is defined.
Definition bounds_specified.hpp:34
@ width
Specifies that the width of the control is defined.
Definition bounds_specified.hpp:32
@ bottom
Bind control edges to the bottom of its container.
Definition anchor_styles.hpp:25
@ right
Bind control edges to the right of its container.
Definition anchor_styles.hpp:29
@ left
Bind control edges to the left of its container.
Definition anchor_styles.hpp:27
@ top
Bind control edges to the top of its container.
Definition anchor_styles.hpp:23
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::drawing namespace provides access to GDI+ basic graphics functionality. More advanced functi...
Definition actions_system_images.hpp:10
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition texts.hpp:217
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Represents an ordered pair of integer x- and y-coordinates that defines a point in a two-dimensional ...
Definition point.hpp:54
static point subtract(const point &pt, const size &sz) noexcept
Returns the result of subtracting specified xtd::drawing::size from the specified xtd::drawing::point...