-
Notifications
You must be signed in to change notification settings - Fork 61
How to draw something on control ?
| xtd | News | Gallery | Examples | Downloads | Documentation | Wiki | Support | Sources | Project | Gammasoft |
There are several ways of for drawing something on control.
The following ways work with all controls on all platforms.
Add event handler to the paint event. This is the most frequent case, as it can be used with or without inheritance from the form class.
#include <xtd/forms/application>
#include <xtd/drawing/brushes>
using namespace xtd;
using namespace xtd::drawing;
using namespace xtd::windows::forms;
auto main()->int {
auto form1 = form {};
form1.paint += [&](object& sender, paint_event_args& e) {
e.graphics().fill_ellipse(brushes::dodger_blue(), e.clip_rectangle());
};
application::run(form1);
}
Overload the on_paint method. This case can only be used by inheritance from the form class.
#include <xtd/forms/application>
#include <xtd/drawing/brushes>
using namespace xtd;
using namespace xtd::drawing;
using namespace xtd::windows::forms;
class form_main : public form {
protected:
void on_paint(paint_event_args& e) override {
form::on_paint(e);
e.graphics().fill_ellipse(brushes::dodger_blue(), client_rectangle());
}
};
auto main()->int {
application::run(form_main {});
}
Overload the wnd_proc method. This case can only be used by inheritance from the form class.
#include <xtd/forms/application>
#include <xtd/drawing/brushes>
using namespace xtd;
using namespace xtd::drawing;
using namespace xtd::windows::forms;
class form_main : public form {
protected:
void wnd_proc(message& message) override {
form::wnd_proc(message);
if (message.msg() == WM_PAINT) {
auto graphics = create_graphics();
graphics.fill_ellipse(brushes::dodger_blue(), client_rectangle());
}
}
};
auto main()->int {
application::run(form_main {});
}
Create an image from a file, from resources or create your own image and add it to the control as a background image with the background_image method and modify its layout with background_image_layout.
#include <xtd/forms/application>
#include <xtd/drawing/brushes>
using namespace xtd;
using namespace xtd::drawing;
using namespace xtd::windows::forms;
auto background_image(const drawing::size& size)->image {
auto background_image = bitmap {size};
auto g = graphics::from_image(background_image);
g.fill_ellipse(brushes::dodger_blue(), 0, 0, background_image.width(), background_image.height());
return background_image;
}
auto main()->int {
auto form1 = form {};
form1.background_image(background_image({1000, 1000}));
form1.background_image_layout(xtd::forms::image_layout::stretch);
application::run(form1);
}
Specify your own style with the style_sheet method. This is used when you don't want to use the full theme management.
#include <xtd/forms/application>
using namespace xtd;
using namespace xtd::windows::forms;
auto main()->int {
auto form1 = form {};
form1.style_sheet(
"form {"
" border-radius: 50%;"
" margin: 0px 0px 0px 0px;"
" background-color: dodgerblue;"
"}"
);
application::run(form1);
}
The style sheet is currently under development. Only a few controls work with this style sheet: button, control, form, label, link_label, panel, status_bar, status_panel, tab_page, toggle_button, tool_bar, tool_bar_button, user_control.
Choose your preferred method.
If you simply wish to change the control's background color (usually achieved by WM_ERASEBKGND), you can use the background_color method.
© 2024 Gammasoft.