demo_view_image.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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) rtgui_image_destroy(image);
  34. rt_memset(image_type, 0, sizeof(image_type));
  35. /* 获得图像的类型 */
  36. if (rt_strstr(path, ".png") != RT_NULL ||
  37. rt_strstr(path, ".PNG") != RT_NULL)
  38. strcat(image_type, "png");
  39. if (rt_strstr(path, ".jpg") != RT_NULL ||
  40. rt_strstr(path, ".JPG") != RT_NULL)
  41. strcat(image_type, "jpeg");
  42. if (rt_strstr(path, ".hdc") != RT_NULL ||
  43. rt_strstr(path, ".HDC") != RT_NULL)
  44. strcat(image_type, "hdc");
  45. /* 如果图像文件有效,创建相应的rtgui_image对象 */
  46. if (image_type[0] != '\0')
  47. image = rtgui_image_create_from_file(image_type, path, RT_TRUE);
  48. }
  49. /* 删除 文件列表 视图 */
  50. rtgui_view_destroy(RTGUI_VIEW(view));
  51. rtgui_view_show(_view, RT_FALSE);
  52. }
  53. /* 演示视图的事件处理函数 */
  54. static rt_bool_t demo_view_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
  55. {
  56. rt_bool_t result;
  57. /* 先调用默认的事件处理函数(这里只关心PAINT事件,但演示视图还有本身的一些控件) */
  58. result = rtgui_view_event_handler(widget, event);
  59. if (event->type == RTGUI_EVENT_PAINT)
  60. {
  61. struct rtgui_dc* dc;
  62. rtgui_rect_t rect;
  63. /* 获得控件所属的DC */
  64. dc = rtgui_dc_begin_drawing(widget);
  65. if (dc == RT_NULL)
  66. /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  67. return RT_FALSE;
  68. /* 获得demo view允许绘图的区域 */
  69. demo_view_get_rect(RTGUI_VIEW(widget), &rect);
  70. /* 获得图像显示区域 */
  71. rect.x1 += 5; rect.x2 -= 5;
  72. rect.y1 += 30;
  73. if (image != RT_NULL)
  74. rtgui_image_blit(image, dc, &rect);
  75. /* 绘图完成 */
  76. rtgui_dc_end_drawing(dc);
  77. }
  78. return result;
  79. }
  80. /* 创建用于显示图像的演示视图 */
  81. rtgui_view_t* demo_view_image(rtgui_workbench_t* workbench)
  82. {
  83. rtgui_rect_t rect;
  84. rtgui_button_t* open_btn;
  85. /* 先创建一个演示视图 */
  86. _view = demo_view(workbench, "图像演示");
  87. if (_view != RT_NULL)
  88. /* 设置默认的事件处理函数到demo_view_event_handler函数 */
  89. rtgui_widget_set_event_handler(RTGUI_WIDGET(_view), demo_view_event_handler);
  90. /* 添加一个按钮 */
  91. demo_view_get_rect(_view, &rect);
  92. rect.x1 += 5; rect.x2 = rect.x1 + 120;
  93. rect.y2 = rect.y1 + 20;
  94. open_btn = rtgui_button_create("打开图像文件");
  95. rtgui_container_add_child(RTGUI_CONTAINER(_view), RTGUI_WIDGET(open_btn));
  96. rtgui_widget_set_rect(RTGUI_WIDGET(open_btn), &rect);
  97. rtgui_button_set_onbutton(open_btn, open_btn_onbutton);
  98. return _view;
  99. }