demo_gui_benchmark.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <rtgui/dc.h>
  2. #include <rtgui/dc_hw.h>
  3. #include <rtgui/kbddef.h>
  4. #include <rtgui/rtgui_system.h>
  5. #include <rtgui/widgets/view.h>
  6. #include <stdlib.h>
  7. #include "demo_view.h"
  8. #if RT_VERSION == 4
  9. #define RAND(x1, x2) ((rand() % (x2 - x1)) + x1)
  10. static rtgui_view_t* view = RT_NULL;
  11. static int running = 0;
  12. void _onidle(PVOID wdt, rtgui_event_t *event)
  13. {
  14. rtgui_color_t color;
  15. rtgui_rect_t rect, draw_rect;
  16. struct rtgui_dc *dc;
  17. /* 获得控件所属的DC */
  18. dc = rtgui_dc_begin_drawing(view);
  19. if (dc == RT_NULL) return ;
  20. rtgui_widget_get_rect(view, &rect);
  21. rtgui_rect_inflate(&rect, -5);
  22. rect.y1 += 35;
  23. draw_rect.x1 = RAND(rect.x1, rect.x2);
  24. draw_rect.y1 = RAND(rect.y1, rect.y2);
  25. draw_rect.x2 = RAND(draw_rect.x1, rect.x2);
  26. draw_rect.y2 = RAND(draw_rect.y1, rect.y2);
  27. color = RTGUI_RGB(rand() % 255, rand() % 255, rand() % 255);
  28. RTGUI_WIDGET_BACKGROUND(view) = color;
  29. rtgui_dc_fill_rect(dc, &draw_rect);
  30. /* 绘图完成 */
  31. rtgui_dc_end_drawing(dc);
  32. }
  33. void _draw_default(PVOID wdt, rtgui_event_t* event)
  34. {
  35. struct rtgui_dc* dc;
  36. rtgui_widget_t* widget = (rtgui_widget_t*)wdt;
  37. rtgui_rect_t rect;
  38. /* 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view先绘图 */
  39. rtgui_view_event_handler(widget, event);
  40. /* 获得控件所属的DC */
  41. dc = rtgui_dc_begin_drawing(widget);
  42. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  43. return ;
  44. /* 获得demo view允许绘图的区域 */
  45. rtgui_widget_get_rect(widget, &rect);
  46. rtgui_rect_inflate(&rect, -5);
  47. rect.y1 += 35;
  48. /* 擦除所有 */
  49. RTGUI_WIDGET_BACKGROUND(widget) = default_background;
  50. rtgui_dc_fill_rect(dc, &rect);
  51. /* 显示提示 */
  52. rtgui_dc_draw_text(dc, "按鼠标键开始/停止测试...", &rect);
  53. /* 绘图完成 */
  54. rtgui_dc_end_drawing(dc);
  55. }
  56. rt_bool_t benchmark_event_handler(PVOID wdt, rtgui_event_t *event)
  57. {
  58. rtgui_widget_t *widget = (rtgui_widget_t*)wdt;
  59. if (event->type == RTGUI_EVENT_PAINT)
  60. {
  61. _draw_default(widget, event);
  62. }
  63. else if (event->type == RTGUI_EVENT_MOUSE_BUTTON)
  64. {
  65. rtgui_event_mouse_t *emouse = (rtgui_event_mouse_t*)event;
  66. if (emouse->button & RTGUI_MOUSE_BUTTON_DOWN)
  67. {
  68. if (running)
  69. {
  70. /* stop */
  71. rtgui_thread_set_onidle(RT_NULL);
  72. _draw_default(widget, event);
  73. }
  74. else
  75. {
  76. /* run */
  77. rtgui_thread_set_onidle(_onidle);
  78. }
  79. running = !running;
  80. }
  81. return RT_TRUE;
  82. }
  83. else
  84. {
  85. /* 调用默认的事件处理函数 */
  86. return rtgui_view_event_handler(widget, event);
  87. }
  88. return RT_FALSE;
  89. }
  90. rtgui_view_t *demo_gui_benchmark(rtgui_view_t* parent_view)
  91. {
  92. srand(100);
  93. view = demo_view_create(parent_view, "绘图测试");
  94. rtgui_widget_set_event_handler(view, benchmark_event_handler);
  95. return view;
  96. }
  97. #endif