demo_view_image.c 3.6 KB

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