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

demonstrates the use of xtd::forms::form controls with mouse_down, mouse_move and paint events.

Windows

macOS

Gnome

#include <xtd/drawing/pens>
#include <xtd/forms/application>
#include <xtd/forms/button>
#include <xtd/forms/form>
#include <xtd/forms/label>
#include <xtd/forms/message_box>
#include <xtd/forms/numeric_up_down>
#include <xtd/forms/open_file_dialog>
#include <xtd/forms/panel>
#include <xtd/forms/track_bar>
using namespace xtd;
using namespace xtd::collections::generic;
using namespace xtd::drawing;
using namespace xtd::forms;
namespace painting_example {
class form1 : public form {
public:
form1() {
text("Painting example");
client_size({640, 480});
panel_colors_container.parent(*this);
panel_colors_container.border_style(forms::border_style::fixed_single);
panel_colors_container.location({10, 10});
panel_colors_container.client_size({512, 32});
auto panel_color = new_ptr<panel>();
panel_color->parent(panel_colors_container);
panel_color->size({32, 32});
panel_color->back_color(color);
panel_color->dock(dock_style::left);
panel_color->click += event_handler {*this, &form1::choose_current_color};
panel_colors.push_back(panel_color);
}
current_color = panel_colors[panel_colors.size() - 1]->back_color();
panel_colors[panel_colors.size() - 1]->border_style(forms::border_style::fixed_single);
button_clear.parent(*this);
button_clear.text("Clear");
button_clear.location({542, 13});
button_clear.click += [&] {
picture = bitmap {picture.width(), picture.height()};
panel_painting.invalidate();
};
button_open.parent(*this);
button_open.text("Open...");
button_open.location({542, 47});
button_open.click += [&] {
bitmap new_picture(ofd.file_name());
if (new_picture.width() > 128 || new_picture.height() > 128)
message_box::show(*this, "The size of the image must be less than or equal to 128 x 128 pixels.", "Painting example", message_box_buttons::ok, message_box_icon::error);
else {
picture = new_picture;
panel_painting.size({picture.width() * zoom, picture.height() * zoom});
panel_painting.invalidate();
}
}
};
label_zoom.parent(*this);
label_zoom.auto_size(false);
label_zoom.location({10, 60});
label_zoom.text("Zoom");
track_bar_zoom.parent(*this);
track_bar_zoom.auto_size(false);
track_bar_zoom.location({60, 55});
track_bar_zoom.anchor(anchor_styles::top | anchor_styles::left);
track_bar_zoom.set_range(1, 50);
track_bar_zoom.tick_style(forms::tick_style::none);
track_bar_zoom.value(zoom);
track_bar_zoom.value_changed += [&] {
zoom = track_bar_zoom.value();
numeric_up_down_zoom.value(zoom);
panel_painting.size({picture.width() * zoom, picture.height() * zoom});
panel_painting.invalidate();
};
track_bar_zoom.size({390, 25});
numeric_up_down_zoom.parent(*this);
numeric_up_down_zoom.location({470, 55});
numeric_up_down_zoom.set_range(1, 50);
numeric_up_down_zoom.value(zoom);
numeric_up_down_zoom.value_changed += [&] {
zoom = as<int>(numeric_up_down_zoom.value());
track_bar_zoom.value(zoom);
panel_painting.size({picture.width() * zoom, picture.height() * zoom});
panel_painting.invalidate();
};
numeric_up_down_zoom.width(52);
panel_main.parent(*this);
panel_main.auto_scroll(true);
panel_main.border_style(forms::border_style::fixed_single);
panel_main.location({10, 90});
panel_main.size({620, 380});
panel_painting.parent(panel_main);
panel_painting.back_color(color::white_smoke);
panel_painting.size({picture.width() * zoom, picture.height() * zoom});
panel_painting.mouse_down += [&](object & sender, const mouse_event_args & e) {
if (e.x() / zoom >= 0 && e.x() / zoom < picture.width() && e.y() / zoom >= 0 && e.y() / zoom < picture.height()) {
picture.set_pixel(e.x() / zoom, e.y() / zoom, e.button() == mouse_buttons::left ? current_color : color::from_argb(0, 0, 0, 0));
panel_painting.invalidate(rectangle(e.x() / zoom * zoom, e.y() / zoom * zoom, zoom, zoom));
}
};
panel_painting.mouse_move += [&](object & sender, const mouse_event_args & e) {
if (e.button() == mouse_buttons::left && e.x() / zoom >= 0 && e.x() / zoom < picture.width() && e.y() / zoom >= 0 && e.y() / zoom < picture.height()) {
picture.set_pixel(e.x() / zoom, e.y() / zoom, current_color);
panel_painting.invalidate(rectangle(e.x() / zoom * zoom, e.y() / zoom * zoom, zoom, zoom));
}
};
panel_painting.paint += [&](object & sender, paint_event_args & e) {
for (auto y = 0; y < panel_painting.client_size().height; y += zoom)
for (auto x = 0; x < panel_painting.client_size().width; x += zoom)
if (picture.get_pixel(x / zoom, y / zoom) != color::from_argb(0, 0, 0, 0))
e.graphics().fill_rectangle(solid_brush(picture.get_pixel(x / zoom, y / zoom)), x, y, zoom, zoom);
if (zoom > 3) {
for (auto index = 0; index < panel_painting.client_size().width; index += zoom)
e.graphics().draw_line(pens::light_blue(), index, 0, index, panel_painting.client_size().height);
for (auto index = 0; index < panel_painting.client_size().height; index += zoom)
e.graphics().draw_line(pens::light_blue(), 0, index, panel_painting.client_size().width, index);
}
};
}
private:
void choose_current_color(object& sender, const event_args& e) {
for (auto panel : panel_colors)
current_color = as<control>(sender).back_color();
}
int zoom = 20;
drawing::color current_color;
bitmap picture {32, 32};
panel panel_colors_container;
list<ptr<panel>> panel_colors;
button button_clear;
button button_open;
panel panel_main;
panel panel_painting;
label label_zoom;
track_bar track_bar_zoom;
numeric_up_down numeric_up_down_zoom;
};
}
auto main() -> int {
application::run(painting_example::form1 {});
}
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
Encapsulates a GDI+ bitmap, which consists of the pixel data for a graphics image and its attributes....
Definition bitmap.hpp:26
Represents an ARGB (alpha, red, green, blue) color.
Definition color.hpp:46
static const xtd::drawing::color dark_cyan
Gets a system-defined color that has an ARGB value of 0xFF008B8B. This field is constant.
Definition color.hpp:125
static const xtd::drawing::color yellow
Gets a system-defined color that has an ARGB value of 0xFFFFFF00. This field is constant.
Definition color.hpp:476
static const xtd::drawing::color gray
Gets a system-defined color that has an ARGB value of 0xFF808080. This field is constant.
Definition color.hpp:209
static const xtd::drawing::color white
Gets a system-defined color that has an ARGB value of 0xFFFFFFFF. This field is constant.
Definition color.hpp:470
static const xtd::drawing::color dark_blue
Gets a system-defined color that has an ARGB value of 0xFF00008B. This field is constant.
Definition color.hpp:122
static const xtd::drawing::color green
Gets a system-defined color that has an ARGB value of 0xFF008000. This field is constant.
Definition color.hpp:212
static const xtd::drawing::color dark_red
Gets a system-defined color that has an ARGB value of 0xFF8B0000. This field is constant.
Definition color.hpp:152
static const xtd::drawing::color red
Gets a system-defined color that has an ARGB value of 0xFFFF0000. This field is constant.
Definition color.hpp:401
static const xtd::drawing::color blue
Gets a system-defined color that has an ARGB value of 0xFF0000FF. This field is constant.
Definition color.hpp:86
static const xtd::drawing::color dark_green
Gets a system-defined color that has an ARGB value of 0xFF006400. This field is constant.
Definition color.hpp:134
static const xtd::drawing::color dark_magenta
Gets a system-defined color that has an ARGB value of 0xFF8B008B. This field is constant.
Definition color.hpp:140
static const xtd::drawing::color brown
Gets a system-defined color that has an ARGB value of 0xFFA52A2A. This field is constant.
Definition color.hpp:92
static xtd::drawing::color from_argb(uint32 argb) noexcept
Creates a xtd::drawing::color class from a 32-bit ARGB value.
static const xtd::drawing::color cyan
Gets a system-defined color that has an ARGB value of 0xFF00FFFF. This field is constant.
Definition color.hpp:119
static const xtd::drawing::color black
Gets a system-defined color that has an ARGB value of 0xFF000000. This field is constant.
Definition color.hpp:80
static const xtd::drawing::color white_smoke
Gets a system-defined color that has an ARGB value of 0xFFF5F5F5. This field is constant.
Definition color.hpp:473
static const xtd::drawing::color magenta
Gets a system-defined color that has an ARGB value of 0xFFFF00FF. This field is constant.
Definition color.hpp:296
static const xtd::drawing::color dark_gray
Gets a system-defined color that has an ARGB value of 0xFFA9A9A9. This field is constant.
Definition color.hpp:131
int32 height() const noexcept
Gets the height, in pixels, of this image.
int32 width() const noexcept
Gets the width, in pixels, of this image.
const drawing::size & size() const noexcept
Gets the width and height, in pixels, of this image.
static xtd::drawing::pen light_blue()
A system-defined pen object with a width of 1.
Defines a xtd::drawing::brush of a single color. Brushes are used to fill graphics shapes,...
Definition solid_brush.hpp:29
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 Windows button control.
Definition button.hpp:49
xtd::forms::dialog_result show_dialog()
Runs a common dialog box with a default owner.
intptr handle() const override
Gets the window handle that the control is bound to.
virtual const xtd::string & file_name() const noexcept
Gets a string containing the file name selected in the file dialog box.
Represents a window or dialog box that makes up an application's user interface.
Definition form.hpp:54
Represents a standard Windows label.
Definition label.hpp:38
static dialog_result show(const iwin32_window &owner)
Displays a message box in front of the specified window.
Provides data for the xtd::forms::control::mouse_up, xtd::forms::control::mouse_down,...
Definition mouse_event_args.hpp:34
Represents a standard Windows numeric up down.
Definition numeric_up_down.hpp:34
Displays a standard dialog box that prompts the user to open a file. This class cannot be inherited.
Definition open_file_dialog.hpp:30
Provides data for the xtd::forms::control::paint event.
Definition paint_event_args.hpp:30
Used to group collections of controls.
Definition panel.hpp:32
virtual forms::border_style border_style() const noexcept
Gets the border style for the control.
Represents a standard Windows track bar.
Definition track_bar.hpp:34
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
@ zoom
The ZOOM key.
Definition console_key.hpp:304
@ y
The Y key.
Definition console_key.hpp:136
@ x
The X key.
Definition console_key.hpp:134
@ e
The E key.
Definition console_key.hpp:96
@ error
The message box contains a symbol consisting of white X in a circle with a red background.
Definition message_dialog_icon.hpp:26
@ 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
@ ok
The dialog box return value is OK (usually sent from a button labeled OK).
Definition dialog_result.hpp:47
@ none
Defines no border.
Definition border_style.hpp:24
@ fixed_single
A single-line border. Same as xtd::forms::border_style::solid.
Definition border_style.hpp:56
@ left
The control's left edge is docked to the left edge of its containing control.
Definition dock_style.hpp:31
@ none
No tick marks appear in the control.
Definition tick_style.hpp:24
@ ok
The message box contains an OK button.
Definition message_dialog_buttons.hpp:24
@ 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
@ left
The left mouse button was pressed.
Definition mouse_buttons.hpp:26
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
Stores a set of four integers that represent the location and size of a rectangle.
Definition rectangle.hpp:44