demo_view_instrument_panel.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * 程序清单:DC操作演示
  3. *
  4. * 这个例子会在创建出的container上进行DC操作的演示
  5. */
  6. #include "demo_view.h"
  7. #include <rtgui/dc.h>
  8. #include <rtgui/rtgui_system.h>
  9. #include <rtgui/widgets/label.h>
  10. #include <rtgui/widgets/slider.h>
  11. #include <rtgui/image_hdc.h>
  12. #include <math.h>
  13. /*
  14. * container的事件处理函数
  15. */
  16. rt_bool_t instrument_panel_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  17. {
  18. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  19. char ac[4];
  20. int i;
  21. int x0 = 120;
  22. int y0 = 170;
  23. int x, y;
  24. int default_color;
  25. /* 仅对PAINT事件进行处理 */
  26. if (event->type == RTGUI_EVENT_PAINT)
  27. {
  28. struct rtgui_dc *dc;
  29. rtgui_rect_t rect;
  30. const int arrowx[] = {120 + 75, 120 + 75, 120 + 85};
  31. const int arrowy[] = {170 - 5, 170 + 5, 170};
  32. /*
  33. * 因为用的是demo container,上面本身有一部分控件,所以在绘图时先要让demo container
  34. * 先绘图
  35. */
  36. rtgui_container_event_handler(RTGUI_OBJECT(widget), event);
  37. /************************************************************************/
  38. /* 下面的是DC的操作 */
  39. /************************************************************************/
  40. /* 获得控件所属的DC */
  41. dc = rtgui_dc_begin_drawing(widget);
  42. /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  43. if (dc == RT_NULL)
  44. return RT_FALSE;
  45. /* 获得demo container允许绘图的区域 */
  46. demo_view_get_rect(RTGUI_CONTAINER(widget), &rect);
  47. RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_BOTTOM | RTGUI_ALIGN_CENTER_HORIZONTAL;
  48. /* 显示GUI的版本信息 */
  49. #ifdef RTGUI_USING_SMALL_SIZE
  50. rtgui_dc_draw_text(dc, "RT-Thread/GUI小型版本", &rect);
  51. #else
  52. rtgui_dc_draw_text(dc, "RT-Thread/GUI标准版本", &rect);
  53. #endif
  54. RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_CENTER_VERTICAL | RTGUI_ALIGN_CENTER_HORIZONTAL;
  55. RTGUI_DC_FC(dc) = blue;
  56. rect.y2 = 270;
  57. rtgui_dc_draw_text(dc, "rtgui-panel", &rect);
  58. for (i = 0; i < 6; i++)
  59. {
  60. rtgui_dc_draw_arc(dc, x0, y0, 117 - i, 150, 30);
  61. }
  62. RTGUI_DC_FC(dc) = black;
  63. RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_LEFT;
  64. for (i = 0; i <= 23; i++)
  65. {
  66. if (i < 12)
  67. {
  68. x = x0 + 105 * cos((150 + i * 10) * 3.1415926 / 180);
  69. y = y0 + 105 * sin((150 + i * 10) * 3.1415926 / 180);
  70. rect.x1 = x;
  71. rect.y1 = y;
  72. rect.x2 = rect.x1 + 12 * 3;
  73. rect.y2 = rect.y1 + 12;
  74. rt_sprintf(ac, "%d", 10 * i);
  75. rtgui_dc_draw_text(dc, ac, &rect);
  76. }
  77. else
  78. {
  79. RTGUI_DC_TEXTALIGN(dc) = RTGUI_ALIGN_RIGHT;
  80. x = x0 + 105 * cos((160 + i * 10) * 3.1415926 / 180);
  81. y = y0 + 105 * sin((160 + i * 10) * 3.1415926 / 180);
  82. rect.x1 = x - 12 * 3;
  83. rect.y1 = y;
  84. rect.x2 = rect.x1 + 12 * 3;
  85. rect.y2 = rect.y1 + 12;
  86. rt_sprintf(ac, "%d", 10 * i);
  87. rtgui_dc_draw_text(dc, ac, &rect);
  88. }
  89. x = x0 + 107 * cos((150 + i * 10) * 3.1415926 / 180);
  90. y = y0 + 107 * sin((150 + i * 10) * 3.1415926 / 180);
  91. rtgui_dc_fill_circle(dc, x, y, 3);
  92. }
  93. RTGUI_DC_FC(dc) = RTGUI_RGB(166, 0, 166);
  94. rtgui_dc_fill_circle(dc, x0, y0, 3);
  95. RTGUI_DC_FC(dc) = RTGUI_RGB(120, 141, 30);
  96. rtgui_dc_draw_circle(dc, x0, y0, 5);
  97. default_color = RTGUI_DC_BC(dc);
  98. RTGUI_DC_BC(dc) = red;
  99. rect.x1 = x0 + 7;
  100. rect.y1 = y0 - 1;
  101. rect.x2 = x0 + 75;
  102. rect.y2 = y0 + 1;
  103. rtgui_dc_fill_rect(dc, &rect);
  104. RTGUI_DC_BC(dc) = default_color;
  105. rtgui_dc_fill_polygon(dc, arrowx, arrowy, 3);
  106. /* 绘图完成 */
  107. rtgui_dc_end_drawing(dc);
  108. }
  109. else
  110. {
  111. /* 其他事件,调用默认的事件处理函数 */
  112. return rtgui_container_event_handler(RTGUI_OBJECT(widget), event);
  113. }
  114. return RT_FALSE;
  115. }
  116. /* 创建用于DC操作演示用的视图 */
  117. rtgui_container_t *demo_view_instrument_panel(void)
  118. {
  119. rtgui_container_t *container;
  120. container = demo_view("instrument panel Demo");
  121. if (container != RT_NULL)
  122. /* 设置成自己的事件处理函数 */
  123. rtgui_object_set_event_handler(RTGUI_OBJECT(container), instrument_panel_event_handler);
  124. return container;
  125. }