demo_gui_dc_buffer.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * 程序清单:DC Buffer演示
  3. *
  4. * 这个例子会在创建出的view上进行DC Buffer的演示
  5. */
  6. #include <rtgui/event.h>
  7. #include <rtgui/widgets/view.h>
  8. #include <rtgui/rtgui_system.h>
  9. #include <rtgui/widgets/label.h>
  10. #include <rtgui/widgets/slider.h>
  11. #include <rtgui/image.h>
  12. #include "demo_view.h"
  13. static struct rtgui_dc *dc_buffer;
  14. /*
  15. * view的事件处理函数
  16. */
  17. static rt_bool_t dc_buffer_event_handler(PVOID wdt, rtgui_event_t *event)
  18. {
  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_view_event_handler(wdt, event);
  29. /* 获得控件所属的DC */
  30. dc = rtgui_dc_begin_drawing(wdt);
  31. /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  32. if (dc == RT_NULL)
  33. return RT_FALSE;
  34. rtgui_widget_get_rect(wdt, &rect);
  35. rect.x1 += 10;
  36. rect.y1 += 40;
  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(wdt, event);
  45. }
  46. return RT_FALSE;
  47. }
  48. /* 创建用于DC Buffer操作演示用的视图 */
  49. rtgui_view_t *demo_gui_dc_buffer(rtgui_view_t* parent_view)
  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_create(parent_view, "缓冲DC演示");
  63. if (view == RT_NULL) return RT_NULL;
  64. /* 设置成自己的事件处理函数 */
  65. rtgui_widget_set_event_handler(view, dc_buffer_event_handler);
  66. return view;
  67. }