demo_gui_image.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * 程序清单:DC上显示图像演示
  3. *
  4. * 这个例子会在创建出的view上显示图像
  5. */
  6. #include "demo_view.h"
  7. #include <rtgui/widgets/button.h>
  8. #include <rtgui/widgets/filelist_view.h>
  9. #include <string.h>
  10. #if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
  11. rtgui_filelist_view_t *demo_fview;
  12. static rtgui_view_t *image_view = RT_NULL;
  13. static rtgui_image_t *demo_image = RT_NULL;
  14. /* 打开文件处理函数,这里只处理图像文件 */
  15. rt_bool_t demo_image_fview_on_item(PVOID wdt, rtgui_event_t *event)
  16. {
  17. rtgui_filelist_view_t *fview = wdt;
  18. if(fview == RT_NULL) return RT_FALSE;
  19. if(fview->pattern != RT_NULL && fview->items != RT_NULL)
  20. {
  21. char path[32], image_type[8];
  22. /* 设置文件路径的标签 */
  23. rtgui_filelist_view_get_fullpath(fview, path, sizeof(path));
  24. if (demo_image != RT_NULL)
  25. {
  26. rtgui_image_destroy(demo_image);
  27. demo_image = RT_NULL;
  28. }
  29. rt_memset(image_type, 0, sizeof(image_type));
  30. /* 获得图像的类型 */
  31. if (rt_strstr(path, ".bmp") != RT_NULL ||
  32. rt_strstr(path, ".BMP") != RT_NULL)
  33. strcat(image_type, "bmp");
  34. if (rt_strstr(path, ".png") != RT_NULL ||
  35. rt_strstr(path, ".PNG") != RT_NULL)
  36. strcat(image_type, "png");
  37. if (rt_strstr(path, ".jpg") != RT_NULL ||
  38. rt_strstr(path, ".JPG") != RT_NULL)
  39. strcat(image_type, "jpeg");
  40. if (rt_strstr(path, ".hdc") != RT_NULL ||
  41. rt_strstr(path, ".HDC") != RT_NULL)
  42. strcat(image_type, "hdc");
  43. /* 如果图像文件有效,创建相应的rtgui_image对象 */
  44. if (image_type[0] != '\0')
  45. demo_image = rtgui_image_create_from_file(image_type, path, RT_TRUE);
  46. }
  47. rtgui_widget_update(image_view);
  48. return RT_TRUE;
  49. }
  50. /* 返回按钮的回调函数 */
  51. static void back_btn_onbutton(PVOID wdt, rtgui_event_t* event)
  52. {
  53. if(demo_fview != RT_NULL)
  54. {
  55. rtgui_filelist_view_goto_topfolder(demo_fview);
  56. }
  57. }
  58. /* 打开按钮的回调函数 */
  59. static void open_btn_onbutton(PVOID wdt, rtgui_event_t* event)
  60. {
  61. if(demo_fview != RT_NULL)
  62. {
  63. rtgui_filelist_view_on_enter(demo_fview);
  64. }
  65. }
  66. /* 演示视图的事件处理函数 */
  67. static rt_bool_t demo_gui_image_handler(PVOID wdt, rtgui_event_t *event)
  68. {
  69. rtgui_widget_t* widget = wdt;
  70. if(event->type == RTGUI_EVENT_PAINT)
  71. {
  72. struct rtgui_dc* dc;
  73. rtgui_rect_t rect;
  74. /* 获得控件所属的DC */
  75. dc = rtgui_dc_begin_drawing(widget);
  76. if(dc == RT_NULL) return RT_FALSE;
  77. /* 获得demo view允许绘图的区域 */
  78. rtgui_widget_get_rect(widget, &rect);
  79. /* 清除背景 */
  80. rtgui_dc_fill_rect(dc, &rect);
  81. /* 绘制图片 */
  82. if(demo_image != RT_NULL) rtgui_image_blit(demo_image, dc, &rect);
  83. /* 绘图完成 */
  84. rtgui_dc_end_drawing(dc);
  85. }
  86. else
  87. return rtgui_view_event_handler(widget, event);
  88. return RT_TRUE;
  89. }
  90. /* 创建用于显示图像的演示视图 */
  91. rtgui_view_t* demo_gui_image(rtgui_view_t* parent_view)
  92. {
  93. rtgui_button_t* button;
  94. rtgui_view_t *view;
  95. /* 先创建一个演示视图 */
  96. view = demo_view_create(parent_view, "图像演示");
  97. /* 创建一个文件浏览列表 */
  98. #ifdef _WIN32
  99. demo_fview = rtgui_filelist_view_create(view, "d:\\", "*.hdc", 5, 32, 200, 68);
  100. #else
  101. demo_fview = rtgui_filelist_view_create(view, "/", "*.hdc", 5, 32, 200, 68);
  102. #endif
  103. demo_fview->on_item = demo_image_fview_on_item;
  104. /* 添加一个返回按钮,浏览文件夹时,用于返回上一级目录 */
  105. button = rtgui_button_create(view, "back", 5, 102, 40, 24);
  106. rtgui_button_set_onbutton(button, back_btn_onbutton);
  107. /* 添加一个打开按钮,浏览文件夹是,用于进入下一级目录,或者打开文件 */
  108. button = rtgui_button_create(view, "open", 5, 130, 40, 24);
  109. rtgui_button_set_onbutton(button, open_btn_onbutton);
  110. /* 创建一个视图,用于显示图片 */
  111. image_view = rtgui_view_create(view, "image_view", 50, 102, 160, 120);
  112. if(image_view == RT_NULL) return RT_NULL;
  113. /* 给image_view设置一个事件处理句柄 */
  114. rtgui_widget_set_event_handler(image_view, demo_gui_image_handler);
  115. return view;
  116. }
  117. #endif