demo_view_dc.c 3.6 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "demo_view.h"
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/label.h>
#include <rtgui/widgets/slider.h>

rt_bool_t dc_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
{
	if (event->type == RTGUI_EVENT_PAINT)
	{
		struct rtgui_dc* dc;
		rtgui_rect_t rect;
		rt_uint32_t vx[] = {20, 50, 60, 45, 60, 20};
		rt_uint32_t vy[] = {150, 50, 90, 60, 45, 50};

B
bernard.xiong 已提交
15
		/* 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view先绘图 */
B
bernard.xiong 已提交
16 17 18
		rtgui_view_event_handler(widget, event);

		/************************************************************************/
B
bernard.xiong 已提交
19
		/* 下面的是DC的处理                                             */
B
bernard.xiong 已提交
20 21 22 23 24 25 26 27 28 29
		/************************************************************************/

		/* 获得控件所属的DC */
		dc = rtgui_dc_begin_drawing(widget);
		if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
			return RT_FALSE;

		/* 获得demo view允许绘图的区域 */
		demo_view_get_rect(RTGUI_VIEW(widget), &rect);

B
bernard.xiong 已提交
30 31 32 33 34 35 36
		rtgui_dc_set_textalign(dc, RTGUI_ALIGN_BOTTOM | RTGUI_ALIGN_CENTER_HORIZONTAL);
#ifdef RTGUI_USING_SMALL_SIZE
		rtgui_dc_draw_text(dc, "RT-Thread/GUI小型版本", &rect);
#else
		rtgui_dc_draw_text(dc, "RT-Thread/GUI标准版本", &rect);
#endif

B
bernard.xiong 已提交
37 38 39 40 41 42 43 44
		/* 绘制一个圆形 */
		rtgui_dc_set_color(dc, red);
		rtgui_dc_draw_circle(dc, rect.x1 + 10, rect.y1 + 10, 10);

		/* 填充一个圆形 */
		rtgui_dc_set_color(dc, green);
		rtgui_dc_fill_circle(dc, rect.x1 + 30, rect.y1 + 10, 10);

45
		/* 画一个圆弧 */
B
bernard.xiong 已提交
46 47 48
		rtgui_dc_set_color(dc, RTGUI_RGB(250, 120, 120));
		rtgui_dc_draw_arc(dc, rect.x1 + 120, rect.y1 + 60, 30, 0, 120);

B
bernard.xiong 已提交
49 50 51 52
		/* 多边形 */
		rtgui_dc_set_color(dc, blue);
		rtgui_dc_draw_polygon(dc, vx, vy, 6);

B
bernard.xiong 已提交
53
		/* 绘制不同的边框 */
54 55 56 57
		{
			rtgui_rect_t rect = {0, 0, 16, 16};
			rtgui_rect_moveto(&rect, 30, 120);

B
bernard.xiong 已提交
58 59 60
			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_RAISE);
			rect.x1 += 20;
			rect.x2 += 20 + 50;
61
			rtgui_dc_draw_text(dc, "raise", &rect);
B
bernard.xiong 已提交
62 63 64 65
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
66 67

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SIMPLE);
B
bernard.xiong 已提交
68 69
			rect.x1 += 20;
			rect.x2 += 20 + 50;
70
			rtgui_dc_draw_text(dc, "simple", &rect);
B
bernard.xiong 已提交
71 72 73 74
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
75 76

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
B
bernard.xiong 已提交
77 78
			rect.x1 += 20;
			rect.x2 += 20 + 50;
79
			rtgui_dc_draw_text(dc, "sunken", &rect);
B
bernard.xiong 已提交
80 81 82 83
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
84 85

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX);
B
bernard.xiong 已提交
86 87
			rect.x1 += 20;
			rect.x2 += 20 + 50;
88
			rtgui_dc_draw_text(dc, "box", &rect);
B
bernard.xiong 已提交
89 90 91 92
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
93 94

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_STATIC);
B
bernard.xiong 已提交
95 96
			rect.x1 += 20;
			rect.x2 += 20 + 50;
97
			rtgui_dc_draw_text(dc, "static", &rect);
B
bernard.xiong 已提交
98 99 100 101
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
102 103

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_EXTRA);
B
bernard.xiong 已提交
104 105
			rect.x1 += 20;
			rect.x2 += 20 + 50;
106
			rtgui_dc_draw_text(dc, "extra", &rect);
B
bernard.xiong 已提交
107 108 109 110
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
111
		}
B
bernard.xiong 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		/* 绘图完成 */
		rtgui_dc_end_drawing(dc);
	}
	else
	{
		/* 调用默认的事件处理函数 */
		return rtgui_view_event_handler(widget, event);
	}

	return RT_FALSE;
}

rtgui_view_t *demo_view_dc(rtgui_workbench_t* workbench)
{
	rtgui_view_t *view;

	view = demo_view(workbench, "DC Demo");
B
bernard.xiong 已提交
129 130
	if (view != RT_NULL)
		rtgui_widget_set_event_handler(RTGUI_WIDGET(view), dc_event_handler);
B
bernard.xiong 已提交
131 132 133

	return view;
}