LVGL学习之路——LVGL模拟器使用之日历控件的建立

关于如何创建lvgl模拟器和如何插入自己的程序我已经在上一章说过了,可以看看上一章的如何使用button。这一章主要的就是上代码
test.c

#include "../../lv_examples.h"
#include "test.h"
#include <Windows.h>

lv_obj_t* calendar;

static void event_handler(lv_obj_t* obj, lv_event_t event)
{
    if (event == LV_EVENT_VALUE_CHANGED) {
        static lv_calendar_date_t date_before = { 0 };
        if (date_before.year != 0)
        {
            lv_calendar_set_highlighted_dates(calendar, &date_before, 0);   // 除了第一次之外每次点击都会取消前一次的高亮
        }
        lv_calendar_date_t* date = lv_calendar_get_pressed_date(obj);
        if (date) {
            printf("Clicked date: %d.%02d.%02d\n", date->year, date->month, date->day);
            date_before.year = date->year;
            date_before.month = date->month;
            date_before.day = date->day;
            lv_calendar_set_highlighted_dates(calendar, &date_before, 1);   // 设置点击日期的高亮
        }
    }
}

static void lv_ex_calendar_1(void)
{
    calendar = lv_calendar_create(lv_scr_act(), NULL);    // 创建日历控件
    lv_obj_set_size(calendar, 235, 235);                            // 改变控件大小(控件字体会挤在一起)
    lv_obj_set_style_local_text_font(calendar, LV_CALENDAR_PART_DATE, LV_STATE_DEFAULT, lv_theme_get_font_small()); //缩小字体
    lv_obj_align(calendar, NULL, LV_ALIGN_CENTER, 0, 0);            // 改变位置
    lv_obj_set_event_cb(calendar, event_handler);                   // 增加显示回调函数

    lv_calendar_date_t today;
    today.year = 2020;
    today.month = 11;
    today.day = 19;

    lv_calendar_set_today_date(calendar, &today);                   // 设置今天的日期(会高亮不会跳转)
    lv_calendar_set_showed_date(calendar, &today);                  // 跳转到该日期显示

    /*Highlight a few days*/
    static lv_calendar_date_t highlighted_days;       /*Only its pointer will be saved so should be static*/
    highlighted_days.year = 2020;
    highlighted_days.month = 11;
    highlighted_days.day = 11;

    lv_calendar_set_highlighted_dates(calendar, &highlighted_days, 1);  // 设置日期为双十一的时候高亮
}


void test(void)
{
    lv_ex_calendar_1();
}

test.h

#pragma once

void test(void); // 对外只开放test

main.c

#include <stdlib.h>
#include <Windows.h>
#include <SDL.h>
#include "lvgl/lvgl.h"
#include "lv_drivers/display/monitor.h"
#include "lv_drivers/indev/mouse.h"
#include "lv_drivers/indev/keyboard.h"
#include "lv_examples/lv_examples.h"

static void hal_init(void);
static int tick_thread(void *data);

static lv_indev_t * kb_indev;

int main(int argc, char** argv)
{
    lv_init();
    hal_init();
    test();		// 只有这里是新增的

    while (1) {
        /* Periodically call the lv_task handler.
        * It could be done in a timer interrupt or an OS task too.*/
        lv_task_handler();
        Sleep(10);       /*Just to let the system breathe */
    }

    return 0;
}


static void hal_init(void)
{
    monitor_init();

    static lv_disp_buf_t disp_buf1;
    static lv_color_t buf1_1[LV_HOR_RES_MAX * 120];
    lv_disp_buf_init(&disp_buf1, buf1_1, NULL, LV_HOR_RES_MAX * 120);

    lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);            /*Basic initialization*/
    disp_drv.buffer = &disp_buf1;
    disp_drv.flush_cb = monitor_flush;
    lv_disp_drv_register(&disp_drv);
    mouse_init();
    lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);          /*Basic initialization*/
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = mouse_read;         /*This function will be called periodically (by the library) to get the mouse position and state*/
    lv_indev_drv_register(&indev_drv);
#if USE_KEYBOARD
    lv_indev_drv_t kb_drv;
    lv_indev_drv_init(&kb_drv);
    kb_drv.type = LV_INDEV_TYPE_KEYPAD;
    kb_drv.read_cb = keyboard_read;
    kb_indev = lv_indev_drv_register(&kb_drv);
#endif
    SDL_CreateThread(tick_thread, "tick", NULL);
}

static int tick_thread(void *data)
{
    while (1) {
        lv_tick_inc(5);
        SDL_Delay(5);   /*Sleep for 1 millisecond*/
    }

    return 0;
}

由于相比上一章我们这里对外接口统一成test()所以以后就不用改main.c和test.h文件,只需要把程序丢到test.c种的test()函数种就ok了。

结果截图

截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值