demo_view_dc_buffer.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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(rtgui_widget_t* widget, rtgui_event_t *event)
  17. {
  18. /* 仅对PAINT事件进行处理 */
  19. if (event->type == RTGUI_EVENT_PAINT)
  20. {
  21. struct rtgui_dc* dc;
  22. rtgui_rect_t rect;
  23. /*
  24. * 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view
  25. * 先绘图
  26. */
  27. rtgui_view_event_handler(widget, event);
  28. /* 获得控件所属的DC */
  29. dc = rtgui_dc_begin_drawing(widget);
  30. /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  31. if (dc == RT_NULL)
  32. return RT_FALSE;
  33. /* 获得demo view允许绘图的区域 */
  34. demo_view_get_logic_rect(RTGUI_VIEW(widget), &rect);
  35. rect.x1 += 10;
  36. rect.y1 += 10;
  37. rtgui_dc_blit(dc_buffer, NULL, dc, &rect);
  38. /* 绘图完成 */
  39. rtgui_dc_end_drawing(dc);
  40. }
  41. else
  42. {
  43. /* 其他事件,调用默认的事件处理函数 */
  44. return rtgui_view_event_handler(widget, event);
  45. }
  46. return RT_FALSE;
  47. }
  48. /* 创建用于DC Buffer操作演示用的视图 */
  49. rtgui_view_t *demo_view_dc_buffer(rtgui_workbench_t* workbench)
  50. {
  51. rtgui_view_t *view;
  52. if (dc_buffer == RT_NULL)
  53. {
  54. rtgui_rect_t rect = {0, 0, 50, 50};
  55. /* 创建 DC Buffer,长 50,宽 50 */
  56. dc_buffer = rtgui_dc_buffer_create(50, 50);
  57. RTGUI_DC_FC(dc_buffer) = blue;
  58. rtgui_dc_fill_rect(dc_buffer, &rect);
  59. RTGUI_DC_FC(dc_buffer) = red;
  60. rtgui_dc_draw_circle(dc_buffer, 25, 25, 10);
  61. }
  62. view = demo_view(workbench, "缓冲DC演示");
  63. if (view != RT_NULL)
  64. /* 设置成自己的事件处理函数 */
  65. rtgui_widget_set_event_handler(RTGUI_WIDGET(view), dc_buffer_event_handler);
  66. return view;
  67. }