1
0

demo_view_image.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * 程序清单:DC上显示图像演示
  3. *
  4. * 这个例子会在创建出的container上显示图像
  5. */
  6. #include "demo_view.h"
  7. #include <rtgui/widgets/button.h>
  8. #include <rtgui/widgets/filelist_view.h>
  9. #include <string.h>
  10. static rtgui_image_t *image = RT_NULL;
  11. static rtgui_container_t *_container = RT_NULL;
  12. #if defined(RTGUI_USING_DFS_FILERW) || defined(RTGUI_USING_STDIO_FILERW)
  13. /* 打开按钮的回调函数 */
  14. static void open_btn_onbutton(rtgui_widget_t *widget, struct rtgui_event *event)
  15. {
  16. rtgui_filelist_view_t *filelist;
  17. struct rtgui_rect rect = {0, 100, 240, 280};
  18. /* WIN32平台上和真实设备上的初始路径处理 */
  19. #ifdef _WIN32
  20. filelist = rtgui_filelist_view_create("e:\\", "*.*", &rect);
  21. #else
  22. filelist = rtgui_filelist_view_create("/", "*.*", &rect);
  23. #endif
  24. /* 模态显示一个文件列表视图,以提供给用户选择图像文件 */
  25. if (rtgui_widget_show(RTGUI_WIDGET(filelist), RT_TRUE) == RTGUI_MODAL_OK)
  26. {
  27. char path[32], image_type[8];
  28. /* 设置文件路径的标签 */
  29. rtgui_filelist_view_get_fullpath(filelist, path, sizeof(path));
  30. if (image != RT_NULL)
  31. {
  32. rtgui_image_destroy(image);
  33. image = RT_NULL;
  34. }
  35. rt_memset(image_type, 0, sizeof(image_type));
  36. /* 获得图像的类型 */
  37. if (rt_strstr(path, ".bmp") != RT_NULL ||
  38. rt_strstr(path, ".BMP") != RT_NULL)
  39. strcat(image_type, "bmp");
  40. if (rt_strstr(path, ".png") != RT_NULL ||
  41. rt_strstr(path, ".PNG") != RT_NULL)
  42. strcat(image_type, "png");
  43. if (rt_strstr(path, ".jpg") != RT_NULL ||
  44. rt_strstr(path, ".JPG") != RT_NULL)
  45. strcat(image_type, "jpeg");
  46. if (rt_strstr(path, ".hdc") != RT_NULL ||
  47. rt_strstr(path, ".HDC") != RT_NULL)
  48. strcat(image_type, "hdc");
  49. /* 如果图像文件有效,创建相应的rtgui_image对象 */
  50. if (image_type[0] != '\0')
  51. image = rtgui_image_create_from_file(image_type, path, RT_TRUE);
  52. }
  53. /* 删除 文件列表 视图 */
  54. rtgui_container_destroy(RTGUI_CONTAINER(filelist));
  55. rtgui_container_show(_container, RT_FALSE);
  56. }
  57. /* 演示视图的事件处理函数 */
  58. static rt_bool_t demo_view_event_handler(rtgui_widget_t *widget, rtgui_event_t *event)
  59. {
  60. rt_bool_t result;
  61. /* 先调用默认的事件处理函数(这里只关心PAINT事件,但演示视图还有本身的一些控件) */
  62. result = rtgui_container_event_handler(widget, event);
  63. if (event->type == RTGUI_EVENT_PAINT)
  64. {
  65. struct rtgui_dc *dc;
  66. rtgui_rect_t rect;
  67. /* 获得控件所属的DC */
  68. dc = rtgui_dc_begin_drawing(widget);
  69. if (dc == RT_NULL)
  70. /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  71. return RT_FALSE;
  72. /* 获得demo container允许绘图的区域 */
  73. demo_view_get_rect(RTGUI_CONTAINER(widget), &rect);
  74. /* 获得图像显示区域 */
  75. rect.x1 += 5;
  76. rect.x2 -= 5;
  77. rect.y1 += 30;
  78. if (image != RT_NULL)
  79. rtgui_image_blit(image, dc, &rect);
  80. /* 绘图完成 */
  81. rtgui_dc_end_drawing(dc);
  82. }
  83. return result;
  84. }
  85. /* 创建用于显示图像的演示视图 */
  86. rtgui_container_t *demo_view_image(void)
  87. {
  88. rtgui_rect_t rect;
  89. rtgui_button_t *open_btn;
  90. /* 先创建一个演示视图 */
  91. _container = demo_view("图像演示");
  92. if (_container != RT_NULL)
  93. /* 设置默认的事件处理函数到demo_view_event_handler函数 */
  94. rtgui_object_set_event_handler(RTGUI_WIDGET(_container), demo_view_event_handler);
  95. /* 添加一个按钮 */
  96. demo_view_get_rect(_container, &rect);
  97. rect.x1 += 5;
  98. rect.x2 = rect.x1 + 120;
  99. rect.y2 = rect.y1 + 20;
  100. open_btn = rtgui_button_create("打开图像文件");
  101. rtgui_container_add_child(RTGUI_CONTAINER(_container), RTGUI_WIDGET(open_btn));
  102. rtgui_widget_set_rect(RTGUI_WIDGET(open_btn), &rect);
  103. rtgui_button_set_onbutton(open_btn, open_btn_onbutton);
  104. return _container;
  105. }
  106. #endif