demo_view_dc.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "demo_view.h"
  2. #include <rtgui/rtgui_system.h>
  3. #include <rtgui/widgets/label.h>
  4. #include <rtgui/widgets/slider.h>
  5. rt_bool_t dc_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
  6. {
  7. if (event->type == RTGUI_EVENT_PAINT)
  8. {
  9. struct rtgui_dc* dc;
  10. rtgui_rect_t rect;
  11. rt_uint32_t vx[] = {20, 50, 60, 45, 60, 20};
  12. rt_uint32_t vy[] = {150, 50, 90, 60, 45, 50};
  13. /* 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让它先绘图 */
  14. rtgui_view_event_handler(widget, event);
  15. /************************************************************************/
  16. /* 下面的是DC的处理 */
  17. /************************************************************************/
  18. /* 获得控件所属的DC */
  19. dc = rtgui_dc_begin_drawing(widget);
  20. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  21. return RT_FALSE;
  22. /* 获得demo view允许绘图的区域 */
  23. demo_view_get_rect(RTGUI_VIEW(widget), &rect);
  24. /* 绘制一个圆形 */
  25. rtgui_dc_set_color(dc, red);
  26. rtgui_dc_draw_circle(dc, rect.x1 + 10, rect.y1 + 10, 10);
  27. /* 填充一个圆形 */
  28. rtgui_dc_set_color(dc, green);
  29. rtgui_dc_fill_circle(dc, rect.x1 + 30, rect.y1 + 10, 10);
  30. /* 多边形 */
  31. rtgui_dc_set_color(dc, blue);
  32. rtgui_dc_draw_polygon(dc, vx, vy, 6);
  33. /* 绘图完成 */
  34. rtgui_dc_end_drawing(dc);
  35. }
  36. else
  37. {
  38. /* 调用默认的事件处理函数 */
  39. return rtgui_view_event_handler(widget, event);
  40. }
  41. return RT_FALSE;
  42. }
  43. rtgui_view_t *demo_view_dc(rtgui_workbench_t* workbench)
  44. {
  45. rtgui_view_t *view;
  46. view = demo_view(workbench, "DC Demo");
  47. rtgui_widget_set_event_handler(RTGUI_WIDGET(view), dc_event_handler);
  48. return view;
  49. }