demo_view_dc.c 4.9 KB
Newer Older
B
bernard.xiong 已提交
1 2 3 4 5 6
/*
 * 程序清单:DC操作演示
 *
 * 这个例子会在创建出的view上进行DC操作的演示
 */

B
bernard.xiong 已提交
7
#include "demo_view.h"
8
#include <rtgui/dc.h>
B
bernard.xiong 已提交
9 10 11
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/label.h>
#include <rtgui/widgets/slider.h>
B
bernard.xiong@gmail.com 已提交
12 13 14 15 16 17
#include <rtgui/image_hdc.h>

#include "play.hdh"
#include "stop.hdh"
struct rtgui_image_hdcmm play_image = RTGUI_IMAGE_HDC_DEF(2, 0x1c, 0x16, play_hdh);
struct rtgui_image_hdcmm stop_image = RTGUI_IMAGE_HDC_DEF(2, 0x1c, 0x16, stop_hdh);
B
bernard.xiong 已提交
18

B
bernard.xiong 已提交
19 20 21
/*
 * view的事件处理函数
 */
B
bernard.xiong 已提交
22 23
rt_bool_t dc_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
{
B
bernard.xiong 已提交
24
	/* 仅对PAINT事件进行处理 */
B
bernard.xiong 已提交
25 26 27 28
	if (event->type == RTGUI_EVENT_PAINT)
	{
		struct rtgui_dc* dc;
		rtgui_rect_t rect;
B
bernard.xiong 已提交
29 30
		const int vx[] = {20, 50, 60, 45, 60, 20};
		const int vy[] = {150, 50, 90, 60, 45, 50};
B
bernard.xiong 已提交
31

B
bernard.xiong 已提交
32 33 34 35
		/*
		 * 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view
		 * 先绘图
		 */
B
bernard.xiong 已提交
36 37 38
		rtgui_view_event_handler(widget, event);

		/************************************************************************/
B
bernard.xiong 已提交
39
		/* 下面的是DC的操作                                                     */
B
bernard.xiong 已提交
40 41 42 43
		/************************************************************************/

		/* 获得控件所属的DC */
		dc = rtgui_dc_begin_drawing(widget);
B
bernard.xiong 已提交
44 45
		/* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
		if (dc == RT_NULL)
B
bernard.xiong 已提交
46 47 48
			return RT_FALSE;

		/* 获得demo view允许绘图的区域 */
B
bernard.xiong@gmail.com 已提交
49
		demo_view_get_logic_rect(RTGUI_VIEW(widget), &rect);
B
bernard.xiong 已提交
50

51
		RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_BOTTOM | RTGUI_ALIGN_CENTER_HORIZONTAL;
B
bernard.xiong 已提交
52
		/* 显示GUI的版本信息 */
B
bernard.xiong 已提交
53 54 55 56 57 58
#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@gmail.com 已提交
59 60 61 62 63 64 65 66 67 68
		{
			rtgui_rect_t rect = {0, 0, 0x1c, 0x16};
			rtgui_rect_moveto(&rect, 80, 80);
			rtgui_image_blit((rtgui_image_t*)&play_image, dc, &rect);

			rect.x1 = 0; rect.y1 = 0;
			rect.x2 = 0x1c; rect.y2 = 0x16;
			rtgui_rect_moveto(&rect, 130, 80);
			rtgui_image_blit((rtgui_image_t*)&stop_image, dc, &rect);
		}
B
bernard.xiong 已提交
69
		/* 绘制一个圆形 */
70
		RTGUI_DC_FC(dc) = red;
B
bernard.xiong 已提交
71 72 73
		rtgui_dc_draw_circle(dc, rect.x1 + 10, rect.y1 + 10, 10);

		/* 填充一个圆形 */
74
		RTGUI_DC_FC(dc) = green;
B
bernard.xiong 已提交
75
		rtgui_dc_fill_circle(dc, rect.x1 + 30, rect.y1 + 10, 10);
B
bernard.xiong@gmail.com 已提交
76
#if 0
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
		/* 画一个圆角矩形 */
		rect.x1 = 150;
		rect.y1 = 180;
		rect.x2 = 210;
		rect.y2 = 260;
		RTGUI_DC_FC(dc) = RTGUI_RGB(25, 70, 150);
		rtgui_dc_draw_round_rect(dc, &rect, 10);

		rect.x1 = 160;
		rect.y1 = 190;
		rect.x2 = 200;
		rect.y2 = 250;
		RTGUI_DC_FC(dc) = RTGUI_RGB(170, 7, 80);
		rtgui_dc_fill_round_rect(dc, &rect, 7);
		
92
		/* 画一个圆弧 */
93
		RTGUI_DC_FC(dc) = RTGUI_RGB(250, 120, 120);
B
bernard.xiong 已提交
94
		rtgui_dc_draw_arc(dc, rect.x1 + 120, rect.y1 + 60, 30, 0, 120);
95 96 97 98
		
		/* 画一个扇形圆环 */
		RTGUI_DC_FC(dc) = RTGUI_RGB(150, 23, 100);
		rtgui_dc_draw_annulus(dc, 180, 170, 30, 50, 210, 330);
B
bernard.xiong 已提交
99

B
bernard.xiong 已提交
100
		/* 多边形 */
101
		RTGUI_DC_FC(dc) = blue;
B
bernard.xiong 已提交
102 103
		rtgui_dc_draw_polygon(dc, vx, vy, 6);

B
bernard.xiong@gmail.com 已提交
104 105 106
#endif
		RTGUI_DC_FC(dc) = blue;

B
bernard.xiong 已提交
107
		/* 绘制不同的边框 */
108 109 110 111
		{
			rtgui_rect_t rect = {0, 0, 16, 16};
			rtgui_rect_moveto(&rect, 30, 120);

B
bernard.xiong 已提交
112 113 114
			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_RAISE);
			rect.x1 += 20;
			rect.x2 += 20 + 50;
115
			rtgui_dc_draw_text(dc, "raise", &rect);
B
bernard.xiong 已提交
116 117 118 119
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
120 121

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SIMPLE);
B
bernard.xiong 已提交
122 123
			rect.x1 += 20;
			rect.x2 += 20 + 50;
124
			rtgui_dc_draw_text(dc, "simple", &rect);
B
bernard.xiong 已提交
125 126 127 128
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
129 130

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
B
bernard.xiong 已提交
131 132
			rect.x1 += 20;
			rect.x2 += 20 + 50;
133
			rtgui_dc_draw_text(dc, "sunken", &rect);
B
bernard.xiong 已提交
134 135 136 137
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
138 139

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX);
B
bernard.xiong 已提交
140 141
			rect.x1 += 20;
			rect.x2 += 20 + 50;
142
			rtgui_dc_draw_text(dc, "box", &rect);
B
bernard.xiong 已提交
143 144 145 146
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
147 148

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_STATIC);
B
bernard.xiong 已提交
149 150
			rect.x1 += 20;
			rect.x2 += 20 + 50;
151
			rtgui_dc_draw_text(dc, "static", &rect);
B
bernard.xiong 已提交
152 153 154 155
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
156 157

			rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_EXTRA);
B
bernard.xiong 已提交
158 159
			rect.x1 += 20;
			rect.x2 += 20 + 50;
160
			rtgui_dc_draw_text(dc, "extera", &rect);
B
bernard.xiong 已提交
161 162 163 164
			rect.x1 -= 20;
			rect.x2 -= 20 + 50;
			rect.y1 += 20;
			rect.y2 += 20;
165
		}
B
bernard.xiong 已提交
166

B
bernard.xiong 已提交
167 168 169 170 171
		/* 绘图完成 */
		rtgui_dc_end_drawing(dc);
	}
	else
	{
B
bernard.xiong 已提交
172
		/* 其他事件,调用默认的事件处理函数 */
B
bernard.xiong 已提交
173 174 175 176 177 178
		return rtgui_view_event_handler(widget, event);
	}

	return RT_FALSE;
}

B
bernard.xiong 已提交
179
/* 创建用于DC操作演示用的视图 */
B
bernard.xiong 已提交
180 181 182 183 184
rtgui_view_t *demo_view_dc(rtgui_workbench_t* workbench)
{
	rtgui_view_t *view;

	view = demo_view(workbench, "DC Demo");
B
bernard.xiong 已提交
185
	if (view != RT_NULL)
B
bernard.xiong 已提交
186
		/* 设置成自己的事件处理函数 */
B
bernard.xiong 已提交
187
		rtgui_widget_set_event_handler(RTGUI_WIDGET(view), dc_event_handler);
B
bernard.xiong 已提交
188 189 190

	return view;
}