demo_view_dc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include "demo_view.h"
  2. #include <rtgui/rtgui_system.h>
  3. #include <rtgui/widgets/label.h>
  4. #include <rtgui/widgets/slider.h>
  5. static const rt_uint8_t boxChecked[7] = {0x02, 0x06, 0x8E, 0xDC, 0xF8, 0x70, 0x20};
  6. static const rt_uint8_t sysMin[2] = {0x7E, 0x7E};
  7. static const rt_uint8_t sysMax[18] = {0x0F, 0xF8, 0x0F, 0xF8, 0x08, 0x08,
  8. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  9. 0x08, 0x08, 0x08, 0x08, 0x0F, 0xF8
  10. };
  11. static const rt_uint8_t sysNormal[18] = {0x03, 0xF0, 0x03, 0xF0, 0x02, 0x10,
  12. 0x0F, 0xD0, 0x0F, 0xD0, 0x08, 0x70,
  13. 0x08, 0x40, 0x08, 0x40, 0x0F, 0xC0
  14. };
  15. static const rt_uint8_t sysClose[14] = {0x06, 0x18, 0x03, 0x30, 0x01, 0xE0, 0x00,
  16. 0xC0, 0x01, 0xE0, 0x03, 0x30, 0x06, 0x18
  17. };
  18. static const rt_uint8_t viewSYS[28] = {0xFF, 0x1F, 0x3C, 0x06, 0x1C, 0x04, 0x1C,
  19. 0x0C, 0x0E, 0x08, 0x0E, 0x18, 0x07, 0x10,
  20. 0x07, 0x30, 0x03, 0xA0, 0x03, 0xE0, 0x01,
  21. 0xC0, 0x01, 0xC0, 0x00, 0x80, 0x00, 0x80
  22. };
  23. rt_bool_t dc_event_handler(rtgui_widget_t* widget, rtgui_event_t *event)
  24. {
  25. if (event->type == RTGUI_EVENT_PAINT)
  26. {
  27. struct rtgui_dc* dc;
  28. rtgui_rect_t rect;
  29. rt_uint32_t vx[] = {20, 50, 60, 45, 60, 20};
  30. rt_uint32_t vy[] = {150, 50, 90, 60, 45, 50};
  31. /* 因为用的是demo view,上面本身有一部分控件,所以在绘图时先要让demo view先绘图 */
  32. rtgui_view_event_handler(widget, event);
  33. /************************************************************************/
  34. /* 下面的是DC的处理 */
  35. /************************************************************************/
  36. /* 获得控件所属的DC */
  37. dc = rtgui_dc_begin_drawing(widget);
  38. if (dc == RT_NULL) /* 如果不能正常获得DC,返回(如果控件或父控件是隐藏状态,DC是获取不成功的) */
  39. return RT_FALSE;
  40. /* 获得demo view允许绘图的区域 */
  41. demo_view_get_rect(RTGUI_VIEW(widget), &rect);
  42. rtgui_dc_set_textalign(dc, RTGUI_ALIGN_BOTTOM | RTGUI_ALIGN_CENTER_HORIZONTAL);
  43. #ifdef RTGUI_USING_SMALL_SIZE
  44. rtgui_dc_draw_text(dc, "RT-Thread/GUI小型版本", &rect);
  45. #else
  46. rtgui_dc_draw_text(dc, "RT-Thread/GUI标准版本", &rect);
  47. #endif
  48. /* 绘制一个圆形 */
  49. rtgui_dc_set_color(dc, red);
  50. rtgui_dc_draw_circle(dc, rect.x1 + 10, rect.y1 + 10, 10);
  51. /* 填充一个圆形 */
  52. rtgui_dc_set_color(dc, green);
  53. rtgui_dc_fill_circle(dc, rect.x1 + 30, rect.y1 + 10, 10);
  54. /* 画一个圆弧 */
  55. rtgui_dc_set_color(dc, RTGUI_RGB(250, 120, 120));
  56. rtgui_dc_draw_arc(dc, rect.x1 + 120, rect.y1 + 60, 30, 0, 120);
  57. /* 多边形 */
  58. rtgui_dc_set_color(dc, blue);
  59. rtgui_dc_draw_polygon(dc, vx, vy, 6);
  60. {
  61. rtgui_rect_t item_rect = {0, 0, 14, 14};
  62. rt_uint16_t item_size = 14;
  63. rtgui_rect_moveto(&item_rect, 100, 100);
  64. rtgui_dc_set_color(dc, RTGUI_RGB(0x66, 0x66, 0x66));
  65. rtgui_dc_draw_circle(dc, item_rect.x1 + item_size/2, item_rect.y1 + item_size/2, item_size/2);
  66. rtgui_dc_set_color(dc, RTGUI_RGB(0xff, 0xFF, 0xFF));
  67. rtgui_dc_draw_circle(dc, item_rect.x1 + item_size/2, item_rect.y1 + item_size/2, item_size/2 - 1);
  68. rtgui_dc_set_color(dc, RTGUI_RGB(0x00, 0x00, 0x00));
  69. rtgui_dc_fill_circle(dc, item_rect.x1 + item_size/2, item_rect.y1 + item_size/2, item_size/2 - 3);
  70. }
  71. {
  72. rtgui_rect_t item_rect = {0, 0, 14, 14};
  73. rt_uint16_t item_size = 14;
  74. rtgui_rect_moveto(&item_rect, 120, 120);
  75. rtgui_dc_set_color(dc, RTGUI_RGB(0x66, 0x66, 0x66));
  76. rtgui_dc_draw_circle(dc, item_rect.x1 + item_size/2, item_rect.y1 + item_size/2, item_size/2);
  77. rtgui_dc_set_color(dc, RTGUI_RGB(0xff, 0xFF, 0xFF));
  78. rtgui_dc_draw_circle(dc, item_rect.x1 + item_size/2, item_rect.y1 + item_size/2, item_size/2 - 1);
  79. rtgui_dc_set_color(dc, RTGUI_RGB(0x00, 0x00, 0x00));
  80. }
  81. {
  82. int x, y;
  83. x = 150; y = 150;
  84. rtgui_dc_set_color(dc, black);
  85. rtgui_dc_draw_byte(dc, x, y, 7, boxChecked);
  86. y += 7 + 2;
  87. rtgui_dc_draw_byte(dc, x, y, 2, sysMin);
  88. y += 2 + 2;
  89. rtgui_dc_draw_word(dc, x, y, 9, sysMax);
  90. y += 9 + 2;
  91. rtgui_dc_draw_word(dc, x, y, 9, sysNormal);
  92. y += 9 + 2;
  93. rtgui_dc_draw_word(dc, x, y, 7, sysClose);
  94. y += 7 + 2;
  95. rtgui_dc_draw_word(dc, x, y, 14, viewSYS);
  96. y += 14 + 2;
  97. }
  98. {
  99. rtgui_rect_t rect = {0, 0, 16, 16};
  100. rtgui_rect_moveto(&rect, 30, 120);
  101. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_RAISE);
  102. rect.x1 += 20; rect.x2 += 20 + 50;
  103. rtgui_dc_draw_text(dc, "raise", &rect);
  104. rect.x1 -= 20; rect.x2 -= 20 + 50;
  105. rect.y1 += 20; rect.y2 += 20;
  106. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SIMPLE);
  107. rect.x1 += 20; rect.x2 += 20 + 50;
  108. rtgui_dc_draw_text(dc, "simple", &rect);
  109. rect.x1 -= 20; rect.x2 -= 20 + 50;
  110. rect.y1 += 20; rect.y2 += 20;
  111. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_SUNKEN);
  112. rect.x1 += 20; rect.x2 += 20 + 50;
  113. rtgui_dc_draw_text(dc, "sunken", &rect);
  114. rect.x1 -= 20; rect.x2 -= 20 + 50;
  115. rect.y1 += 20; rect.y2 += 20;
  116. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_BOX);
  117. rect.x1 += 20; rect.x2 += 20 + 50;
  118. rtgui_dc_draw_text(dc, "box", &rect);
  119. rect.x1 -= 20; rect.x2 -= 20 + 50;
  120. rect.y1 += 20; rect.y2 += 20;
  121. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_STATIC);
  122. rect.x1 += 20; rect.x2 += 20 + 50;
  123. rtgui_dc_draw_text(dc, "static", &rect);
  124. rect.x1 -= 20; rect.x2 -= 20 + 50;
  125. rect.y1 += 20; rect.y2 += 20;
  126. rtgui_dc_draw_border(dc, &rect, RTGUI_BORDER_EXTRA);
  127. rect.x1 += 20; rect.x2 += 20 + 50;
  128. rtgui_dc_draw_text(dc, "extra", &rect);
  129. rect.x1 -= 20; rect.x2 -= 20 + 50;
  130. rect.y1 += 20; rect.y2 += 20;
  131. }
  132. /* 绘图完成 */
  133. rtgui_dc_end_drawing(dc);
  134. }
  135. else
  136. {
  137. /* 调用默认的事件处理函数 */
  138. return rtgui_view_event_handler(widget, event);
  139. }
  140. return RT_FALSE;
  141. }
  142. rtgui_view_t *demo_view_dc(rtgui_workbench_t* workbench)
  143. {
  144. rtgui_view_t *view;
  145. view = demo_view(workbench, "DC Demo");
  146. if (view != RT_NULL)
  147. rtgui_widget_set_event_handler(RTGUI_WIDGET(view), dc_event_handler);
  148. return view;
  149. }