- 变量定义
static lv_obj_t* mbox, * info; static const char welcom_info[] = { "Welcome to the modal message box demo!\n" "Press the button to display a message box." }; static const char in_msg_info[] = {"Notice that you cannot touch " "the button again while the message box is open."};
- modal message消息框按键点击回调函数
static void mbox_evnet_handler(lv_obj_t* obj, lv_event_t event) { if (event == LV_EVENT_DELETE && obj == mbox) { lv_obj_del_async(lv_obj_get_parent(mbox)); //异步删除message box的父窗口 mbox = NULL; //消息框对象置成空指针 lv_label_set_text(info, welcom_info); //设置文本 printf("LV_EVENT_DELETE\n"); } else if (event == LV_EVENT_VALUE_CHANGED) { lv_mbox_start_auto_close(mbox, 0); //销废message box printf("LV_EVENT_VALUE_CHANGED\n"); } }
- 按键点击回调函数,用来弹出模态消息框测试用
static void btn_event_handler(lv_obj_t* obj, lv_event_t event) { if (event == LV_EVENT_CLICKED) //按键已点击 { static lv_style_t modal_style; /*Create a full-screen background*/ lv_style_copy(&modal_style, &lv_style_plain_color); /*Set the background's style*/ modal_style.body.main_color = LV_COLOR_BLACK; modal_style.body.grad_color = LV_COLOR_BLACK; modal_style.body.opa = LV_OPA_50; /*Create a base object for the modal background*/ lv_obj_t* obj = lv_obj_create(lv_scr_act(), NULL); lv_obj_set_style(obj, &modal_style); lv_obj_set_pos(obj, 0, 0); lv_obj_set_size(obj, LV_HOR_RES, LV_VER_RES); lv_obj_set_opa_scale_enable(obj, true); static const char* btns2[] = { "OK","Cancel","" }; mbox = lv_mbox_create(obj, NULL); //创建消息对话框对象 lv_mbox_add_btns(mbox, btns2); lv_mbox_set_text(mbox, "Hello world!"); //设置消息文本 lv_obj_align(mbox, NULL, LV_ALIGN_CENTER, 0, 0); lv_obj_set_event_cb(mbox, mbox_evnet_handler); //设置消息对话框按键回调 /*Fade the message box in with an animation*/ lv_anim_t a; lv_anim_init(&a); lv_anim_set_time(&a, 500, 0); lv_anim_set_values(&a, LV_OPA_TRANSP, LV_OPA_COVER); lv_anim_set_exec_cb(&a, obj, (lv_anim_exec_xcb_t)lv_obj_set_opa_scale); lv_anim_create(&a); lv_label_set_text(info, in_msg_info); //label文本显示更新 lv_obj_align(info, NULL, LV_ALIGN_CENTER, 5, -5); } }
- 按键实现
void message_box_demo2(void) { /*Create a button,then set its position and event callback*/ lv_obj_t* btn = lv_btn_create(lv_scr_act(), NULL); lv_obj_set_size(btn, 200, 60); lv_obj_set_event_cb(btn, btn_event_handler); lv_obj_align(btn, NULL, LV_ALIGN_IN_TOP_LEFT, 20, 20); /*Create a label on the button*/ lv_obj_t* label = lv_label_create(btn, NULL); lv_label_set_text(label, "Display a message box!"); /*Create an informative label on the screen*/ info = lv_label_create(lv_scr_act(), NULL); lv_label_set_text(info, welcom_info); //显示信息 lv_label_set_long_mode(info, LV_LABEL_LONG_BREAK); lv_obj_set_width(info, LV_HOR_RES - 10); lv_obj_align(info, NULL, LV_ALIGN_IN_BOTTOM_LEFT, 5, -5); }
- 调用message_box_demo2函数,编译运行