[LVGL] How to draw a rectangle using LVGL?

How to draw a rectangle using LVGL?

I try to use this function.
But he seems to be ineffective.

  • Draw a rectangle
  • @param cords_p the coordinates of the rectangle
  • @param mask_p the rectangle will be drawn only in this mask
  • @param style_p pointer to a style
    void lv_draw_rect(const lv_area_t *cords_p, const lv_area_t *mask_p, const lv_style_t *style_p);


is there a particular purpose to this?

Does there need to be?

1 Like

yes,I want to make a visual sensor recognition interface .

I’d have to more thoroughly examine the docs, but I believe lv_draw_rect is used by objects, not by you the end programmer.

Here’s some example code copied straight from their docs about the basic object:

/*Create a simple base object*/
lv_obj_t * obj1;
obj1 = lv_obj_create(lv_scr_act(), NULL);
lv_obj_set_size(obj1, 150, 40);
lv_obj_set_style(obj1, &lv_style_plain_color);
lv_obj_align(obj1, NULL, LV_ALIGN_IN_TOP_MID, 0, 40);

/*Copy the previous object and enable drag*/
lv_obj_t * obj2;
obj2 = lv_obj_create(lv_scr_act(), obj1);
lv_obj_set_style(obj2, &lv_style_pretty_color);
lv_obj_set_drag(obj2, true);
lv_obj_align(obj2, NULL, LV_ALIGN_CENTER, 0, 0);

static lv_style_t style_shadow;
lv_style_copy(&style_shadow, &lv_style_pretty);
style_shadow.body.shadow.width = 6;
style_shadow.body.radius = LV_RADIUS_CIRCLE;

/*Copy the previous object (drag is already enabled)*/
lv_obj_t * obj3;
obj3 = lv_obj_create(lv_scr_act(), obj2);
lv_obj_set_style(obj3, &style_shadow);
lv_obj_align(obj3, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, -40);

Not sure where those styles come from, but I’m sure you can explore how styles work on the docs.

Drawing simple objects in LVGL seems hard, I was trying to do that a few weeks ago, figured I was going to have to write code for things like rectangle, circle etc. Here’s one test I did.

lv_obj_t *
drawRectangle( int x, int y, int width, int height, lv_color_t color ) {
  lv_obj_t * obj1 = lv_obj_create(lv_scr_act(), NULL);

  lv_style_t *style1 = (lv_style_t *)malloc( sizeof( lv_style_t ));
  lv_style_copy(style1, &lv_style_plain_color);    /*Copy a built-in style to initialize the new style*/
  style1->body.empty = 1;
  style1->body.border.color = color;
  style1->body.border.width = 1;
  style1->body.border.part = LV_BORDER_FULL;

  lv_obj_set_style(obj1, style1);
  lv_obj_set_pos(obj1, x, y);
  lv_obj_set_size(obj1, width, height);

  return obj1;

void opcontrol() {
  lv_obj_t *obj = drawRectangle( 20, 20, 100, 100, LV_COLOR_YELLOW);

  while (true) {

I want to make a visual sensor recognition interface, if you can not customize the drawing, then this function will not be able to achieve.

obj1 = lv_obj_create(lv_scr_act(), NULL);
I have a BTNM object on the outer layer that I can’t “use lv_scr_act()”
I want to transplant your visual test page on VCS to PROS.

this is you example code.

Then use the BTNM object as the parent.

@jpearman His plan is effective.
Thank you very much, too, for what your team has done for vexer.