窗口 (lv_win)

概述

窗口是一个类似容器的对象,由一个带有标题和按钮的头部以及一个内容区域组成。

部件和样式

窗口由其他小部件构建,因此可以查看它们的文档以了解详细信息:

用法

创建窗口

lv_win_create(parent, header_height) 创建一个带有空头部的窗口。

标题和按钮

可以使用 lv_win_add_title(win, "标题") 向头部添加任意数量的文本(但通常只有一个)。

可以使用 lv_win_add_btn(win, icon, btn_width) 向窗口的头部添加控制按钮。icon 可以是任何图像来源,btn_width 是按钮的宽度。

标题和按钮将按照函数调用的顺序添加。因此,添加一个按钮、一个文本和两个其他按钮将导致左侧有一个按钮,中间是标题,右侧有两个按钮。 标题的宽度设置为占据头部的所有剩余空间。换句话说,它会将标题之后添加的所有按钮推到右侧。

获取部件

lv_win_get_header(win) 返回指向头部的指针,lv_win_get_content(win) 返回指向可以向其中添加窗口内容的内容容器的指针。

事件

窗口不发送特殊事件,但可以手动将事件添加到 lv_win_add_btn 的返回值。

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

按键

窗口不处理任何按键

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

示例

Simple window

C code  

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


static void event_handler(lv_event_t * e)
{
    lv_obj_t * obj = lv_event_get_target(e);
    LV_LOG_USER("Button %d clicked", (int)lv_obj_get_index(obj));
}

void lv_example_win_1(void)
{
    lv_obj_t * win = lv_win_create(lv_scr_act(), 40);
    lv_obj_t * btn;
    btn = lv_win_add_btn(win, LV_SYMBOL_LEFT, 40);
    lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);

    lv_win_add_title(win, "A title");

    btn = lv_win_add_btn(win, LV_SYMBOL_RIGHT, 40);
    lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);

    btn = lv_win_add_btn(win, LV_SYMBOL_CLOSE, 60);
    lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);

    lv_obj_t * cont = lv_win_get_content(win);  /*Content can be added here*/
    lv_obj_t * label = lv_label_create(cont);
    lv_label_set_text(label, "This is\n"
                      "a pretty\n"
                      "long text\n"
                      "to see how\n"
                      "the window\n"
                      "becomes\n"
                      "scrollable.\n"
                      "\n"
                      "\n"
                      "Some more\n"
                      "text to be\n"
                      "sure it\n"
                      "overflows. :)");


}

#endif

MicroPython code  

 GitHub Simulator
def event_handler(e):
    code = e.get_code()
    obj = e.get_target()
    if code == lv.EVENT.CLICKED:
        print("Button {:d} clicked".format(obj.get_child_id()))


win = lv.win(lv.scr_act(), 60)
btn1 = win.add_btn(lv.SYMBOL.LEFT, 40)
btn1.add_event_cb(event_handler, lv.EVENT.ALL, None)
win.add_title("A title")
btn2=win.add_btn(lv.SYMBOL.RIGHT, 40)
btn2.add_event_cb(event_handler, lv.EVENT.ALL, None)
btn3 = win.add_btn(lv.SYMBOL.CLOSE, 60)
btn3.add_event_cb(event_handler, lv.EVENT.ALL, None)

cont = win.get_content()  # Content can be added here
label = lv.label(cont)
label.set_text("""This is
a pretty
long text
to see how
the window
becomes
scrollable.


We need
quite some text
and we will
even put
some more
text to be
sure it
overflows.
""")

API

Functions

lv_obj_t *lv_win_create(lv_obj_t *parent, lv_coord_t header_height)
lv_obj_t *lv_win_add_title(lv_obj_t *win, const char *txt)
lv_obj_t *lv_win_add_btn(lv_obj_t *win, const void *icon, lv_coord_t btn_w)
lv_obj_t *lv_win_get_header(lv_obj_t *win)
lv_obj_t *lv_win_get_content(lv_obj_t *win)

Variables

const lv_obj_class_t lv_win_class
struct lv_win_t

Public Members

lv_obj_t obj