demo_view_instrument_panel.c 3.6 KB
Newer Older
R
 
richard.lion.heart.gl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * 程序清单:DC操作演示
 *
 * 这个例子会在创建出的view上进行DC操作的演示
 */

#include "demo_view.h"
#include <rtgui/dc.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/label.h>
#include <rtgui/widgets/slider.h>
#include <rtgui/image_hdc.h>
#include <math.h>

/*
 * view的事件处理函数
 */
rt_bool_t instrument_panel_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
{
	char ac[4];
	int i;
	int x0 = 120;
	int y0 = 170;
	int x, y;
	int default_color;
	
	/* 仅对PAINT事件进行处理 */
	if (event->type == RTGUI_EVENT_PAINT)
	{
		struct rtgui_dc* dc;
		rtgui_rect_t rect;
32 33
		const int arrowx[] = {120+75, 120+75, 120+85};
		const int arrowy[] = {170-5,  170+5,  170};
R
 
richard.lion.heart.gl 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

		/*
		 * 因为用的是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);

		RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_BOTTOM | RTGUI_ALIGN_CENTER_HORIZONTAL;
		/* 显示GUI的版本信息 */
#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


	RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_CENTER_VERTICAL | RTGUI_ALIGN_CENTER_HORIZONTAL;
	RTGUI_DC_FC(dc) = blue;
	rect.y2 = 270;
	rtgui_dc_draw_text(dc, "rtgui-panel", &rect);

	for(i = 0; i < 6; i++)
	{
		rtgui_dc_draw_arc(dc, x0, y0, 117-i, 150, 30);
	}

	RTGUI_DC_FC(dc) = black;

	RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_LEFT;
	for(i = 0; i <= 23; i++)
	{
		if(i < 12)
		{
			x = x0 + 105 * cos((150 + i * 10) * 3.1415926 / 180);
			y = y0 + 105 * sin((150 + i * 10) * 3.1415926 / 180);
			rect.x1 = x;
			rect.y1 = y;
			rect.x2 = rect.x1 + 12 * 3;
			rect.y2 = rect.y1 + 12;
86
			rt_sprintf(ac, "%d", 10 * i);
R
 
richard.lion.heart.gl 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
			rtgui_dc_draw_text(dc, ac, &rect);	
		}
		else
		{
			RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_RIGHT;
	
			x = x0 + 105 * cos((160 + i * 10) * 3.1415926 / 180);
			y = y0 + 105 * sin((160 + i * 10) * 3.1415926 / 180);

			rect.x1 = x  - 12 * 3;
			rect.y1 = y;
			rect.x2 = rect.x1 + 12 * 3;
			rect.y2 = rect.y1 + 12;
100
			rt_sprintf(ac, "%d", 10 * i);
R
 
richard.lion.heart.gl 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
			rtgui_dc_draw_text(dc, ac, &rect);
		}

		x = x0 + 107 * cos((150 + i * 10) * 3.1415926 / 180);
		y = y0 + 107 * sin((150 + i * 10) * 3.1415926 / 180);
		rtgui_dc_fill_circle(dc, x, y, 3);
	}
	RTGUI_DC_FC(dc) = RTGUI_RGB(166, 0, 166);
	rtgui_dc_fill_circle(dc, x0, y0, 3);
	RTGUI_DC_FC(dc) = RTGUI_RGB(120, 141, 30);
	rtgui_dc_draw_circle(dc, x0, y0, 5);

	default_color = RTGUI_DC_BC(dc);
	RTGUI_DC_BC(dc) = red;
	rect.x1 = x0 + 7;
	rect.y1 = y0 - 1;
	rect.x2 = x0 + 75;
	rect.y2 = y0 + 1;
	rtgui_dc_fill_rect(dc, &rect);

	RTGUI_DC_BC(dc) = default_color;

	rtgui_dc_fill_polygon(dc, arrowx, arrowy, 3);

		/* 绘图完成 */
		rtgui_dc_end_drawing(dc);
	}
	else
	{
		/* 其他事件,调用默认的事件处理函数 */
		return rtgui_view_event_handler(widget, event);
	}

	return RT_FALSE;
}

/* 创建用于DC操作演示用的视图 */
rtgui_view_t *demo_view_instrument_panel(rtgui_workbench_t* workbench)
{
	rtgui_view_t *view;

	view = demo_view(workbench, "instrument panel Demo");
	if (view != RT_NULL)
		/* 设置成自己的事件处理函数 */
		rtgui_widget_set_event_handler(RTGUI_WIDGET(view), instrument_panel_event_handler);

	return view;
}