demo_view_instrument_panel.c 3.7 KB

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