Message box(消息框) (lv_msgbox)
Overview(概述)
显示原文
The Message boxes act as pop-ups. They are built from a background container, a title, an optional close button, a text and optional buttons.
The text will be broken into multiple lines automatically and the height will be set automatically to include the text and the buttons.
The message box can be modal (blocking clicks on the rest of the screen) or not modal.
消息框是一个弹出窗口。其是由背景容器、标题、可选的关闭按钮、文本和可选按钮构建的 。
过长的文本会自动分解为多行,高度会自动适应文本和按钮。
消息框可以是模态的(阻止对屏幕其余部分的单击) 或者不是模态的。
Parts and Styles(部分和样式)
显示原文
The message box is built from other widgets, so you can check these widgets' documentation for details.
Background: lv_obj.h
Close button: lv_button.h
Title and text: lv_label.h
Buttons: lv_buttonmatrix.h
消息框是由多种控件组成的,下面是各个控件(组成)对应的文档:
背景: lv_obj.h
关闭按钮: lv_button.h
标题和文本:lv_label.h
Usage(用法)
Create a message box(创建消息框)
显示原文
lv_msgbox_create(parent, title, txt, btn_txts[], add_close_btn) creates a message box.
If parent
is NULL
the message box will be modal. title
and
txt
are strings for the title and the text. btn_txts[]
is an
array with the buttons' text.
E.g. const char * btn_txts[] = {"Ok", "Cancel", NULL}.
add_close_btn
can be true
or false
to add/don't add a close button.
可以通过函数 lv_msgbox_create(parent) 创建一个消息框。
如果参数 parent
为 NULL
,那么创建出来的消息框是模态的,反之是非模态的。
通过函数 lv_msgbox_add_title(lv_obj_t *obj, const char *title) 添加标题。
通过函数 lv_msgbox_add_text(lv_obj_t *obj, const char *title) 添加中间的提示文本。
通过函数 lv_msgbox_add_close_button(lv_obj_t * obj) 添加位于右上角的关闭按钮。
通过函数 lv_msgbox_add_footer_button(lv_obj_t *obj, const char *text) 添加位于底部的用户按钮,可以多次调用,默认会自动按照添加的先后顺序从左到右排列这些按钮。
Get the parts(获取各个组成部分)
显示原文
The building blocks of the message box can be obtained using the following functions:
消息框的各个组成部分(控件)可以通过以下接口获取:
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_add_footer_button(lv_obj_t * mbox); // 存放用户按键的容器
Close the message box(关闭消息框)
如果你要删除消息框,不建议直接调用 lv_obj_delete 函数进行操作,而是使用 lv_msgbox_close 函数
显示原文
lv_msgbox_close(msgbox) closes (deletes) the message box.
lv_msgbox_close(msgbox) 关闭(删除)消息框。
Events(事件)
显示原文
LV_EVENT_VALUE_CHANGED
is sent by the buttons if one of them is clicked.LV_OBJ_FLAG_EVENT_BUBBLE
is enabled on the buttons so you can add events to the message box itself. In the event handler, lv_event_get_target(e) will return the button matrix and lv_event_get_current_target(e) will return the message box. lv_msgbox_get_active_button(msgbox) and lv_msgbox_get_active_button_text(msgbox) can be used to get the index and text of the clicked button.
Learn more about Events(事件).
如果其中一个按钮被点击将会触发
LV_EVENT_VALUE_CHANGED
事件按钮可以使能
LV_OBJ_FLAG_EVENT_BUBBLE
事件冒泡,之后您可以在消息框本身的事件处理程序中处理事件lv_event_get_target(e) 将返回按钮 ,lv_event_get_current_target(e) 将返回消息框
lv_msgbox_get_active_button(msgbox) 和 lv_msgbox_get_active_button_text(msgbox) 可用于获取被点击的按钮的索引和文本。
详细了解更多 Events(事件)。
Keys(按键)
显示原文
Keys have effect on the close button and button matrix. You can add them manually to a group if required.
Learn more about Keys(按键).
按键对关闭按钮和用户按钮有影响。如果需要您可以手动添加到组中。
了解有关 Keys(按键) 的更多信息。
Example
Simple Message box
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_MSGBOX && LV_BUILD_EXAMPLES
static void event_cb(lv_event_t * e)
{
lv_obj_t * btn = lv_event_get_target(e);
lv_obj_t * label = lv_obj_get_child(btn, 0);
LV_UNUSED(label);
LV_LOG_USER("Button %s clicked", lv_label_get_text(label));
}
void lv_example_msgbox_1(void)
{
lv_obj_t * mbox1 = lv_msgbox_create(NULL);
lv_msgbox_add_title(mbox1, "Hello");
lv_msgbox_add_text(mbox1, "This is a message box with two buttons.");
lv_msgbox_add_close_button(mbox1);
lv_obj_t * btn;
btn = lv_msgbox_add_footer_button(mbox1, "Apply");
lv_obj_add_event_cb(btn, event_cb, LV_EVENT_CLICKED, NULL);
btn = lv_msgbox_add_footer_button(mbox1, "Cancel");
lv_obj_add_event_cb(btn, event_cb, LV_EVENT_CLICKED, NULL);
return;
}
#endif