|
@@ -1,3 +1,9 @@
|
|
|
+/*
|
|
|
+ * 程序清单:DC上显示图像演示
|
|
|
+ *
|
|
|
+ * 这个例子会在创建出的view上显示图像
|
|
|
+ */
|
|
|
+
|
|
|
#include "demo_view.h"
|
|
|
#include <rtgui/widgets/button.h>
|
|
|
#include <rtgui/widgets/filelist_view.h>
|
|
@@ -5,26 +11,29 @@
|
|
|
static rtgui_image_t* image = RT_NULL;
|
|
|
static rtgui_view_t* _view = RT_NULL;
|
|
|
|
|
|
+/* 打开按钮的回调函数 */
|
|
|
static void open_btn_onbutton(rtgui_widget_t* widget, struct rtgui_event* event)
|
|
|
{
|
|
|
- /* create a file list view */
|
|
|
rtgui_filelist_view_t *view;
|
|
|
rtgui_workbench_t *workbench;
|
|
|
rtgui_rect_t rect;
|
|
|
|
|
|
+ /* 获得顶层的workbench */
|
|
|
workbench = RTGUI_WORKBENCH(rtgui_widget_get_toplevel(widget));
|
|
|
rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
|
|
|
|
|
|
+ /* WIN32平台上和真实设备上的初始路径处理 */
|
|
|
#ifdef _WIN32
|
|
|
view = rtgui_filelist_view_create(workbench, "d:\\", "*.*", &rect);
|
|
|
#else
|
|
|
view = rtgui_filelist_view_create(workbench, "/", "*.*", &rect);
|
|
|
#endif
|
|
|
+ /* 模态显示一个文件列表视图,以提供给用户选择图像文件 */
|
|
|
if (rtgui_view_show(RTGUI_VIEW(view), RT_TRUE) == RTGUI_MODAL_OK)
|
|
|
{
|
|
|
char path[32], image_type[8];
|
|
|
|
|
|
- /* set label */
|
|
|
+ /* 设置文件路径的标签 */
|
|
|
rtgui_filelist_get_fullpath(view, path, sizeof(path));
|
|
|
if (image != RT_NULL) rtgui_image_destroy(image);
|
|
|
|
|
@@ -32,15 +41,16 @@ static void open_btn_onbutton(rtgui_widget_t* widget, struct rtgui_event* event)
|
|
|
|
|
|
/* 获得图像的类型 */
|
|
|
if (rt_strstr(path, ".png") != RT_NULL ||
|
|
|
- rt_strstr(path, ".PNG") != RT_NULL)
|
|
|
+ rt_strstr(path, ".PNG") != RT_NULL)
|
|
|
strcat(image_type, "png");
|
|
|
if (rt_strstr(path, ".jpg") != RT_NULL ||
|
|
|
- rt_strstr(path, ".JPG") != RT_NULL)
|
|
|
+ rt_strstr(path, ".JPG") != RT_NULL)
|
|
|
strcat(image_type, "jpeg");
|
|
|
if (rt_strstr(path, ".hdc") != RT_NULL ||
|
|
|
- rt_strstr(path, ".HDC") != RT_NULL)
|
|
|
+ rt_strstr(path, ".HDC") != RT_NULL)
|
|
|
strcat(image_type, "hdc");
|
|
|
|
|
|
+ /* 如果图像文件有效,创建相应的rtgui_image对象 */
|
|
|
if (image_type[0] != '\0')
|
|
|
image = rtgui_image_create_from_file(image_type, path, RT_TRUE);
|
|
|
}
|
|
@@ -50,11 +60,12 @@ static void open_btn_onbutton(rtgui_widget_t* widget, struct rtgui_event* event)
|
|
|
rtgui_view_show(_view, RT_FALSE);
|
|
|
}
|
|
|
|
|
|
+/* 演示视图的事件处理函数 */
|
|
|
static rt_bool_t demo_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
|
|
|
{
|
|
|
rt_bool_t result;
|
|
|
|
|
|
- /* 用默认的事件处理函数 */
|
|
|
+ /* 先调用默认的事件处理函数(这里只关心PAINT事件,但演示视图还有本身的一些控件) */
|
|
|
result = rtgui_view_event_handler(widget, event);
|
|
|
|
|
|
if (event->type == RTGUI_EVENT_PAINT)
|
|
@@ -64,7 +75,8 @@ static rt_bool_t demo_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *
|
|
|
|
|
|
/* 获得控件所属的DC */
|
|
|
dc = rtgui_dc_begin_drawing(widget);
|
|
|
- if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
|
|
|
+ if (dc == RT_NULL)
|
|
|
+ /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
|
|
|
return RT_FALSE;
|
|
|
|
|
|
/* 获得demo view允许绘图的区域 */
|
|
@@ -72,7 +84,7 @@ static rt_bool_t demo_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *
|
|
|
|
|
|
/* 获得图像显示区域 */
|
|
|
rect.x1 += 5; rect.x2 -= 5;
|
|
|
- rect.y1 += 30;
|
|
|
+ rect.y1 += 30;
|
|
|
|
|
|
if (image != RT_NULL)
|
|
|
rtgui_image_blit(image, dc, &rect);
|
|
@@ -84,15 +96,19 @@ static rt_bool_t demo_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+/* 创建用于显示图像的演示视图 */
|
|
|
rtgui_view_t* demo_view_image(rtgui_workbench_t* workbench)
|
|
|
{
|
|
|
rtgui_rect_t rect;
|
|
|
rtgui_button_t* open_btn;
|
|
|
|
|
|
+ /* 先创建一个演示视图 */
|
|
|
_view = demo_view(workbench, "图像演示");
|
|
|
if (_view != RT_NULL)
|
|
|
+ /* 设置默认的事件处理函数到demo_view_event_handler函数 */
|
|
|
rtgui_widget_set_event_handler(RTGUI_WIDGET(_view), demo_view_event_handler);
|
|
|
|
|
|
+ /* 添加一个按钮 */
|
|
|
demo_view_get_rect(_view, &rect);
|
|
|
rect.x1 += 5; rect.x2 = rect.x1 + 120;
|
|
|
rect.y2 = rect.y1 + 20;
|