1
0

demo_view_image.c 3.4 KB

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