Skip to content

How to draw something on control ?

Gammasoft edited this page Nov 12, 2023 · 4 revisions

| 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.

paint event

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);
}

on_paint method

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 {});
}

wnd_proc method

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 {});
}

background_image method

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);
}

Using style_sheet

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);
}

Remarks

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.

Remarks

If you simply wish to change the control's background color (usually achieved by WM_ERASEBKGND), you can use the background_color method.

See