消息框 (lv_msgbox)

概述

消息框充当弹出窗口。 它由一个背景容器、一个标题、一个可选的关闭按钮、一段文本和可选的按钮组成。

文本将自动换行,并且高度会自动调整以包含文本和按钮。

消息框可以是模态(阻止点击屏幕的其他部分)或非模态。

部件和样式

消息框由其他小部件构建,因此可以查看这些小部件的文档以了解详细信息。

用法

创建消息框

lv_msgbox_create(parent, title, txt, btn_txts[], add_close_btn) 创建一个消息框。

如果 parentNULL,消息框将是模态的。titletxt 是标题和文本的字符串。 btn_txts[] 是一个包含按钮文本的数组。例如:const char * btn_txts[] = {"确定", "取消", NULL}add_close_btn 可以是 truefalse,用于添加或不添加关闭按钮。

获取部件

可以使用以下函数获取消息框的构建块:

lv_obj_t * lv_msgbox_get_title(lv_obj_t * mbox);
lv_obj_t * lv_msgbox_get_close_btn(lv_obj_t * mbox);
lv_obj_t * lv_msgbox_get_text(lv_obj_t * mbox);
lv_obj_t * lv_msgbox_get_btns(lv_obj_t * mbox);

关闭消息框

lv_msgbox_close(msgbox) 关闭(删除)消息框。

事件

  • LV_EVENT_VALUE_CHANGED 当点击按钮时发送。LV_OBJ_FLAG_EVENT_BUBBLE 在按钮上启用,因此可以将事件添加到消息框本身。 在事件处理程序中,lv_event_get_target(e) 将返回按钮矩阵,lv_event_get_current_target(e) 将返回消息框。可以使用 lv_msgbox_get_active_btn(msgbox)lv_msgbox_get_active_btn_text(msgbox) 获取点击按钮的索引和文本。

了解更多关于事件的信息。

按键

按键对关闭按钮和按钮矩阵有效。如果需要,可以手动将它们添加到组中。

了解更多关于按键的信息。

示例

Simple Message box

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_MSGBOX && LV_BUILD_EXAMPLES

static void event_cb(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_current_target(e);
    LV_LOG_USER("Button %s clicked", lv_msgbox_get_active_btn_text(obj));
}

void lv_example_msgbox_1(void)
{
    static const char * btns[] = {"Apply", "Close", ""};

    lv_obj_t * mbox1 = lv_msgbox_create(NULL, "Hello", "This is a message box with two buttons.", btns, true);
    lv_obj_add_event_cb(mbox1, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_center(mbox1);
}

#endif

MicroPython code  

 GitHub Simulator
def event_cb(e):
    mbox = e.get_current_target()
    print("Button %s clicked" % mbox.get_active_btn_text())

btns = ["Apply", "Close", ""]

mbox1 = lv.msgbox(lv.scr_act(), "Hello", "This is a message box with two buttons.", btns, True)
mbox1.add_event_cb(event_cb, lv.EVENT.VALUE_CHANGED, None)
mbox1.center()


API

Functions

lv_obj_t *lv_msgbox_create(lv_obj_t *parent, const char *title, const char *txt, const char *btn_txts[], bool add_close_btn)

Create a message box object

Parameters
  • parent -- pointer to parent or NULL to create a full screen modal message box

  • title -- the title of the message box

  • txt -- the text of the message box

  • btn_txts -- the buttons as an array of texts terminated by an "" element. E.g. {"btn1", "btn2", ""}

  • add_close_btn -- true: add a close button

Returns

pointer to the message box object

lv_obj_t *lv_msgbox_get_title(lv_obj_t *obj)
lv_obj_t *lv_msgbox_get_close_btn(lv_obj_t *obj)
lv_obj_t *lv_msgbox_get_text(lv_obj_t *obj)
lv_obj_t *lv_msgbox_get_content(lv_obj_t *obj)
lv_obj_t *lv_msgbox_get_btns(lv_obj_t *obj)
uint16_t lv_msgbox_get_active_btn(lv_obj_t *mbox)

Get the index of the selected button

Parameters

mbox -- message box object

Returns

index of the button (LV_BTNMATRIX_BTN_NONE: if unset)

const char *lv_msgbox_get_active_btn_text(lv_obj_t *mbox)
void lv_msgbox_close(lv_obj_t *mbox)
void lv_msgbox_close_async(lv_obj_t *mbox)

Variables

const lv_obj_class_t lv_msgbox_class
const lv_obj_class_t lv_msgbox_content_class
const lv_obj_class_t lv_msgbox_backdrop_class
struct lv_msgbox_t

Public Members

lv_obj_t obj
lv_obj_t *title
lv_obj_t *close_btn
lv_obj_t *content
lv_obj_t *text
lv_obj_t *btns