rtgui_demo.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <rtthread.h>
  10. #include <rtgui/rtgui.h>
  11. #include <rtgui/rtgui_system.h>
  12. #include <rtgui/rtgui_app.h>
  13. #include <rtgui/widgets/window.h>
  14. #include <rtgui/dc.h>
  15. #include "finsh.h"
  16. #include "rtgui_demo.h"
  17. #define DEBUG
  18. #ifdef DEBUG
  19. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  20. #else
  21. #define DEBUG_PRINTF(...)
  22. #endif
  23. #ifdef PKG_USING_GUIENGINE
  24. struct rtgui_win *main_win;
  25. rt_bool_t dc_event_handler(struct rtgui_object *object, rtgui_event_t *event);
  26. static void rt_gui_demo_entry(void *parameter)
  27. {
  28. struct rtgui_app *app;
  29. //struct rtgui_dc *dc;
  30. DEBUG_PRINTF("gui demo entry\n");
  31. /* create gui app */
  32. app = rtgui_app_create("gui_demo");
  33. if (app == RT_NULL)
  34. {
  35. DEBUG_PRINTF("rtgui_app_create faild\n");
  36. return;
  37. }
  38. /* create main window */
  39. main_win = rtgui_mainwin_create(RT_NULL,
  40. "UiWindow", RTGUI_WIN_STYLE_NO_TITLE | RTGUI_WIN_STYLE_NO_BORDER);
  41. if (main_win == RT_NULL)
  42. {
  43. DEBUG_PRINTF("main_win is null\n");
  44. rtgui_app_destroy(app);
  45. return;
  46. }
  47. rtgui_object_set_event_handler(RTGUI_OBJECT(main_win), dc_event_handler);
  48. DEBUG_PRINTF("rtgui_win_show\n");
  49. rtgui_win_show(main_win, RT_FALSE);
  50. DEBUG_PRINTF("rtgui_app_run\n");
  51. rtgui_app_run(app);
  52. DEBUG_PRINTF("rtgui_win_destroy\n");
  53. rtgui_win_destroy(main_win);
  54. DEBUG_PRINTF("rtgui_app_destroy\n");
  55. rtgui_app_destroy(app);
  56. }
  57. rt_bool_t dc_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  58. {
  59. struct rtgui_widget *widget = RTGUI_WIDGET(object);
  60. if (event->type == RTGUI_EVENT_PAINT)
  61. {
  62. struct rtgui_dc *dc;
  63. rtgui_rect_t rect;
  64. rt_kprintf("\r\n RTGUI_EVENT_PAINT \r\n");
  65. rtgui_win_event_handler(RTGUI_OBJECT(widget), event);
  66. rtgui_widget_get_rect(widget, &rect);
  67. DEBUG_PRINTF("widget react x1: %d, y1: %d, x2: %d, y2: %d\r\n",
  68. rect.x1, rect.y1, rect.x2, rect.y2);
  69. dc = rtgui_dc_begin_drawing(widget);
  70. if(dc == RT_NULL)
  71. {
  72. DEBUG_PRINTF("\r\n dc is null \r\n");
  73. return RT_FALSE;
  74. }
  75. rtgui_dc_draw_line(dc,0,0,240,320);
  76. rtgui_dc_draw_line(dc,0,320,240,0);
  77. //rtgui_dc_draw_text(dc, __DATE__, &rect);
  78. rtgui_dc_draw_text_stroke(dc, __DATE__, &rect, HIGH_LIGHT, BLUE);
  79. rect.y1 += 20;
  80. rect.y2 += 20;
  81. rtgui_dc_draw_text_stroke(dc, __TIME__, &rect, HIGH_LIGHT, BLACK);
  82. rtgui_dc_end_drawing(dc, RT_TRUE);
  83. }
  84. return RT_FALSE;
  85. }
  86. int rt_gui_demo_init(void)
  87. {
  88. rt_thread_t tid;
  89. tid = rt_thread_create("mygui",
  90. rt_gui_demo_entry, RT_NULL,
  91. 2048, 25, 10);
  92. if (tid != RT_NULL)
  93. rt_thread_startup(tid);
  94. return 0;
  95. }
  96. #endif /* PKG_USING_GUIENGINE */