rt-thread/examples/gui/demo_view_ttf.c

99 lines
2.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 程序清单TTF字体显示演示
*
* 这个例子会在创建出的view上进行TTF字体显示的演示
*/
#include "demo_view.h"
#include <rtgui/dc.h>
#include <rtgui/font.h>
#include <rtgui/rtgui_system.h>
#ifdef RTGUI_USING_TTF
static rtgui_font_t *font_16, *font_24, *font_36, *font_48;
/*
* view的事件处理函数
*/
rt_bool_t ttf_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
{
/* 仅对PAINT事件进行处理 */
if (event->type == RTGUI_EVENT_PAINT)
{
struct rtgui_dc* dc;
rtgui_rect_t rect;
rtgui_font_t* saved;
/*
* 因为用的是demo view上面本身有一部分控件所以在绘图时先要让demo view
* 先绘图
*/
rtgui_view_event_handler(widget, event);
/************************************************************************/
/* 下面的是DC的操作 */
/************************************************************************/
/* 获得控件所属的DC */
dc = rtgui_dc_begin_drawing(widget);
/* 如果不能正常获得DC返回如果控件或父控件是隐藏状态DC是获取不成功的 */
if (dc == RT_NULL)
return RT_FALSE;
/* 获得demo view允许绘图的区域 */
demo_view_get_rect(RTGUI_VIEW(widget), &rect);
saved = RTGUI_WIDGET_FONT(widget);
RTGUI_WIDGET_FONT(widget) = font_16;
rtgui_dc_draw_text(dc, "ABCD", &rect);
rect.y1 += 18;
RTGUI_WIDGET_FONT(widget) = font_24;
rtgui_dc_draw_text(dc, "ABCD", &rect);
rect.y1 += 26;
RTGUI_WIDGET_FONT(widget) = font_36;
rtgui_dc_draw_text(dc, "ABCD", &rect);
rect.y1 += 38;
RTGUI_WIDGET_FONT(widget) = font_48;
rtgui_dc_draw_text(dc, "ABCD", &rect);
RTGUI_WIDGET_FONT(widget) = saved;
/* 绘图完成 */
rtgui_dc_end_drawing(dc);
}
else
{
/* 其他事件,调用默认的事件处理函数 */
return rtgui_view_event_handler(widget, event);
}
return RT_FALSE;
}
extern rtgui_font_t* rtgui_freetype_font_create(const char* filename, int bold, int italic, rt_size_t size);
/* 创建用于TTF字体显示演示用的视图 */
rtgui_view_t *demo_view_ttf(rtgui_workbench_t* workbench)
{
rtgui_view_t *view;
font_16 = rtgui_freetype_font_create("d:/arial.ttf", 0, 0, 16);
font_24 = rtgui_freetype_font_create("d:/arial.ttf", 0, 0, 24);
font_36 = rtgui_freetype_font_create("d:/arial.ttf", 0, 0, 36);
font_48 = rtgui_freetype_font_create("d:/HARNGTON.TTF", 0, 0, 72);
view = demo_view(workbench, "TTF 演示");
if (view != RT_NULL)
{
// RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(view)) = white;
/* 设置成自己的事件处理函数 */
rtgui_widget_set_event_handler(RTGUI_WIDGET(view), ttf_event_handler);
}
return view;
}
#endif