demo_view_instrument_panel.c 3.6 KB

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