demo_view_dc_buffer.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * 程序清单:DC Buffer演示
  3. *
  4. * 这个例子会在创建出的view上进行DC Buffer的演示
  5. */
  6. #include "demo_view.h"
  7. #include <rtgui/rtgui_system.h>
  8. #include <rtgui/widgets/label.h>
  9. #include <rtgui/widgets/slider.h>
  10. #include <rtgui/image.h>
  11. static rtgui_image_t *background;
  12. static struct rtgui_dc *dc_buffer;
  13. /*
  14. * view的事件处理函数
  15. */
  16. static rt_bool_t dc_buffer_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  17. {
  18. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  19. /* 仅对PAINT事件进行处理 */
  20. if (event->type == RTGUI_EVENT_PAINT)
  21. {
  22. struct rtgui_dc *dc;
  23. rtgui_rect_t rect;
  24. /*
  25. * 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view
  26. * 先绘图
  27. */
  28. rtgui_container_event_handler(object, event);
  29. /* 获得控件所属的DC */
  30. dc = rtgui_dc_begin_drawing(widget);
  31. /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  32. if (dc == RT_NULL)
  33. return RT_FALSE;
  34. /* 获得demo view允许绘图的区域 */
  35. demo_view_get_logic_rect(RTGUI_CONTAINER(widget), &rect);
  36. rect.x1 += 10;
  37. rect.y1 += 10;
  38. rtgui_dc_blit(dc_buffer, NULL, dc, &rect);
  39. /* 绘图完成 */
  40. rtgui_dc_end_drawing(dc);
  41. }
  42. else
  43. {
  44. /* 其他事件,调用默认的事件处理函数 */
  45. return rtgui_container_event_handler(object, event);
  46. }
  47. return RT_FALSE;
  48. }
  49. /* 创建用于DC Buffer操作演示用的视图 */
  50. rtgui_container_t *demo_view_dc_buffer()
  51. {
  52. rtgui_container_t *view;
  53. if (dc_buffer == RT_NULL)
  54. {
  55. rtgui_rect_t rect = {0, 0, 50, 50};
  56. /* 创建 DC Buffer,长 50,宽 50 */
  57. dc_buffer = rtgui_dc_buffer_create(50, 50);
  58. RTGUI_DC_FC(dc_buffer) = blue;
  59. rtgui_dc_fill_rect(dc_buffer, &rect);
  60. RTGUI_DC_FC(dc_buffer) = red;
  61. rtgui_dc_draw_circle(dc_buffer, 25, 25, 10);
  62. }
  63. view = demo_view("缓冲DC演示");
  64. if (view != RT_NULL)
  65. /* 设置成自己的事件处理函数 */
  66. rtgui_object_set_event_handler(RTGUI_OBJECT(view), dc_buffer_event_handler);
  67. return view;
  68. }