这一篇来演示一下lvgl8下msgbox的使用

(1)msgbox弹框出现时,背景变透明度降低

(2)点击"Close"按键关闭msgbox

(3)给msgbox设置了样式

还是通过codeblock来模拟代码的运行,代码如下:

static void msgbox_event_callback(lv_event_t * event)
{
    lv_event_code_t code = lv_event_get_code(event);
    lv_obj_t * msgbox = lv_event_get_current_target(event);

    if ((code == LV_EVENT_VALUE_CHANGED) && (msgbox != NULL))
    {
        const char * txt = lv_msgbox_get_active_btn_text(msgbox);
        if(strcmp(txt, "Close") == 0)
        {
            lv_msgbox_close(msgbox);
        }
    }
}


void lv_msgbox_show()
{
    static const char *button_array[] ={"OK", "Close", ""};

    static lv_style_t msgbox_style;
    lv_style_init(&msgbox_style); // 初始化按钮样式
    lv_style_set_radius(&msgbox_style, 0); // 设置样式圆角弧度

    // 创建一个msgbox不带关闭按钮
    lv_obj_t * msg_box = lv_msgbox_create(NULL, "Warning", "This is a message box with two buttons.", button_array, false);
    if (msg_box != NULL)
    {
        lv_obj_add_event_cb(msg_box, msgbox_event_callback, LV_EVENT_ALL, NULL);
        lv_obj_align(msg_box, LV_ALIGN_CENTER, 0, 0);
        lv_obj_add_style(msg_box, &msgbox_style, 0);

        lv_obj_t * background_obj = lv_obj_get_parent(msg_box);
        if (background_obj != NULL)
        {
            lv_obj_set_style_bg_opa(background_obj, LV_OPA_70, 0);
            lv_obj_set_style_bg_color(background_obj, lv_palette_lighten(LV_PALETTE_GREY, 1), 0);
        }
    }
}


void lv_msgbox_test()
{
    static lv_style_t obj_style;
    lv_style_init(&obj_style); // 初始化按钮样式
    lv_style_set_radius(&obj_style, 0); // 设置样式圆角弧度

     lv_obj_t * obj = lv_obj_create(lv_scr_act());
     if (obj != NULL)
     {
        lv_obj_set_style_bg_color(obj, lv_palette_main(LV_PALETTE_RED), 0);
        lv_obj_set_size(obj, 800, 480);
        lv_obj_add_style(obj, &obj_style, 0);
     }

     lv_msgbox_show();
}

运行效果:

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐