demo_view_image.c 3.6 KB

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