demo_window.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/rtgui_system.h>
  3. #include <rtgui/widgets/window.h>
  4. #include <rtgui/widgets/label.h>
  5. static struct rtgui_timer *timer;
  6. static struct rtgui_label* label;
  7. static struct rtgui_win* msgbox;
  8. static rt_uint8_t label_text[80];
  9. static int cnt = 5;
  10. void diag_close(struct rtgui_timer* timer, void* parameter)
  11. {
  12. sprintf(label_text, "closed then %d second!", cnt);
  13. rtgui_label_set_text(label, label_text);
  14. rtgui_widget_update(RTGUI_WIDGET(label));
  15. if (cnt == 0)
  16. {
  17. rtgui_win_destroy(msgbox);
  18. rtgui_timer_stop(timer);
  19. rtgui_timer_destory(timer);
  20. }
  21. cnt --;
  22. }
  23. void window_demo()
  24. {
  25. rt_mq_t mq;
  26. rt_thread_t tid;
  27. rt_uint32_t user_data;
  28. struct rtgui_rect rect = {50, 50, 200, 200};
  29. tid = rt_thread_self();
  30. if (tid == RT_NULL) return; /* can't use in none-scheduler environement */
  31. user_data = tid->user_data;
  32. /* create gui message queue */
  33. mq = rt_mq_create("msgbox", 256, 4, RT_IPC_FLAG_FIFO);
  34. /* register message queue on current thread */
  35. rtgui_thread_register(rt_thread_self(), mq);
  36. msgbox = rtgui_win_create(RT_NULL, "Information", &rect, RTGUI_WIN_STYLE_DEFAULT);
  37. if (msgbox != RT_NULL)
  38. {
  39. struct rtgui_box* box = rtgui_box_create(RTGUI_VERTICAL, RT_NULL);
  40. cnt = 5;
  41. sprintf(label_text, "closed then %d second!", cnt);
  42. label = rtgui_label_create(label_text);
  43. rtgui_win_set_box(msgbox, box);
  44. RTGUI_WIDGET(label)->align = RTGUI_ALIGN_CENTER_HORIZONTAL |
  45. RTGUI_ALIGN_CENTER_VERTICAL;
  46. rtgui_widget_set_miniwidth(RTGUI_WIDGET(label),130);
  47. rtgui_box_append(box, RTGUI_WIDGET(label));
  48. rtgui_box_layout(box);
  49. rtgui_win_show(msgbox, RT_FALSE);
  50. }
  51. timer = rtgui_timer_create(200, RT_TIMER_FLAG_PERIODIC, diag_close, RT_NULL);
  52. rtgui_timer_start(timer);
  53. rtgui_win_event_loop(msgbox);
  54. rtgui_thread_deregister(rt_thread_self());
  55. /* remove RTGUI message queue */
  56. rt_mq_delete(mq);
  57. /* recover user data */
  58. tid->user_data = user_data;
  59. }
  60. void window_demo_init()
  61. {
  62. rt_thread_t tid;
  63. tid = rt_thread_create("win",
  64. window_demo, RT_NULL,
  65. 2048, 25, 10);
  66. if (tid != RT_NULL) rt_thread_startup(tid);
  67. }
  68. #ifdef RT_USING_FINSH
  69. #include <finsh.h>
  70. void win_demo()
  71. {
  72. window_demo();
  73. }
  74. FINSH_FUNCTION_EXPORT(win_demo, a window demo)
  75. #endif