How to select correct function when switch windows

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • How to select correct function when switch windows

      When I switch the views, I'm a bit confused about the correct function to use.

      For example, I have a Window0 which has a button leading to Window1. Then, in the Window0's button callback functions regarding message WM_NOTIFICATION_RELEASED, I'm not very clear about which of the following functions to take:
      1. WM_DeleteWindow();
      2. WM_HideWindow();
      3. GUI_EndDialog();
      They both seem to be able to make Window0 disappear and also have methods to Show or Create Window0 again afterward, but which function is the most traditional way in this scenario?

      Thank you.

      The post was edited 1 time, last by Ross Lee ().

    • Hi,

      I would delete the current window and create the new one. This way the memory consumed by one window will get freed. Of course, there can be reasons to not delete a window, e.g. setting up some data in WM_CREATE takes while.

      I barely use GUI_EndDialog(), I always use WM_DeleteWindow() to delete a window.

      WM_InvalidateWindow() is required to redraw a window. Maybe after changing some values to be displayed (e.g. ADC values).

      Here is an example for deleting and invalidating two windows:

      Source Code

      1. #include "DIALOG.h"
      2. /*********************************************************************
      3. *
      4. * Prototypes
      5. *
      6. **********************************************************************
      7. */
      8. static void _cbWin0(WM_MESSAGE * pMsg);
      9. static void _cbWin1(WM_MESSAGE * pMsg);
      10. /*********************************************************************
      11. *
      12. * Static code
      13. *
      14. **********************************************************************
      15. */
      16. /*********************************************************************
      17. *
      18. * _cbWin1
      19. */
      20. static void _cbWin1(WM_MESSAGE * pMsg) {
      21. GUI_RECT Rect;
      22. WM_HWIN hItem;
      23. int Id;
      24. int NCode;
      25. GUI_COLOR aColor[] = { GUI_YELLOW, GUI_BLUE };
      26. static int Index;
      27. switch (pMsg->MsgId) {
      28. case WM_CREATE:
      29. hItem = BUTTON_CreateEx(10, 10, 80, 20, pMsg->hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON0);
      30. BUTTON_SetText(hItem, "Delete Window");
      31. hItem = BUTTON_CreateEx(100, 10, 80, 20, pMsg->hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON1);
      32. BUTTON_SetText(hItem, "Change Color");
      33. break;
      34. case WM_PAINT:
      35. GUI_SetBkColor(aColor[Index]);
      36. GUI_Clear();
      37. WM_GetClientRect(&Rect);
      38. GUI_SetColor(aColor[Index ^ 1]);
      39. GUI_SetFont(GUI_FONT_24B_1);
      40. GUI_DispStringInRect("Window 1", &Rect, GUI_TA_VCENTER | GUI_TA_HCENTER);
      41. break;
      42. case WM_NOTIFY_PARENT:
      43. Id = WM_GetId(pMsg->hWinSrc);
      44. NCode = pMsg->Data.v;
      45. switch (Id) {
      46. case GUI_ID_BUTTON0:
      47. switch (NCode) {
      48. case WM_NOTIFICATION_RELEASED:
      49. WM_DeleteWindow(pMsg->hWin);
      50. WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin0, 0);
      51. break;
      52. }
      53. break;
      54. case GUI_ID_BUTTON1:
      55. switch (NCode) {
      56. case WM_NOTIFICATION_RELEASED:
      57. Index ^= 1;
      58. WM_InvalidateWindow(pMsg->hWin);
      59. break;
      60. }
      61. break;
      62. }
      63. break;
      64. default:
      65. WM_DefaultProc(pMsg);
      66. break;
      67. }
      68. }
      69. /*********************************************************************
      70. *
      71. * _cbWin0
      72. */
      73. static void _cbWin0(WM_MESSAGE * pMsg) {
      74. GUI_RECT Rect;
      75. WM_HWIN hItem;
      76. int Id;
      77. int NCode;
      78. GUI_COLOR aColor[] = { GUI_BLUE, GUI_YELLOW };
      79. static int Index;
      80. switch (pMsg->MsgId) {
      81. case WM_CREATE:
      82. //
      83. // Add some buttons to the window
      84. //
      85. hItem = BUTTON_CreateEx(10, 10, 80, 20, pMsg->hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON0);
      86. BUTTON_SetText(hItem, "Delete Window");
      87. hItem = BUTTON_CreateEx(100, 10, 80, 20, pMsg->hWin, WM_CF_SHOW, 0, GUI_ID_BUTTON1);
      88. BUTTON_SetText(hItem, "Change Color");
      89. break;
      90. case WM_PAINT:
      91. //
      92. // DEraw the window according to the set "Index"
      93. //
      94. GUI_SetBkColor(aColor[Index]);
      95. GUI_Clear();
      96. WM_GetClientRect(&Rect);
      97. GUI_SetColor(aColor[Index ^ 1]);
      98. GUI_SetFont(GUI_FONT_24B_1);
      99. GUI_DispStringInRect("Window 0", &Rect, GUI_TA_VCENTER | GUI_TA_HCENTER);
      100. break;
      101. case WM_NOTIFY_PARENT:
      102. Id = WM_GetId(pMsg->hWinSrc);
      103. NCode = pMsg->Data.v;
      104. switch (Id) {
      105. case GUI_ID_BUTTON0:
      106. switch (NCode) {
      107. case WM_NOTIFICATION_RELEASED:
      108. //
      109. // When button 0 gets released simply delete this window and create a new one.
      110. // When deleting this window also its child windows/widgets are getting deleted.
      111. // Calling WM_DeleteWindow() also works on dialogs.
      112. // Both, deleting and creating will not be performed before calling GUI_Exec()/GUI_Delay().
      113. // Do NOT call GUI_Exec() here, it gets called in the super loop of MainTask().
      114. // Calling of WM_InvalidateWindow() is not required here.
      115. //
      116. WM_DeleteWindow(pMsg->hWin);
      117. WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin1, 0);
      118. break;
      119. }
      120. break;
      121. case GUI_ID_BUTTON1:
      122. switch (NCode) {
      123. case WM_NOTIFICATION_RELEASED:
      124. //
      125. // On release of button 1 we want to change the background and text color.
      126. // Simply change the index used to access the color array and invalidate the window.
      127. // WM_InvalidateWindow() has also no effect before calling GUI_Exec().
      128. //
      129. Index ^= 1;
      130. WM_InvalidateWindow(pMsg->hWin);
      131. break;
      132. }
      133. break;
      134. }
      135. break;
      136. default:
      137. WM_DefaultProc(pMsg);
      138. break;
      139. }
      140. }
      141. /*********************************************************************
      142. *
      143. * Public code
      144. *
      145. **********************************************************************
      146. */
      147. /*********************************************************************
      148. *
      149. * MainTask
      150. */
      151. void MainTask(void) {
      152. GUI_Init();
      153. //
      154. // Avoid flickering, only has an effect when using GUIDRV_Lin and enough buffers.
      155. //
      156. WM_MULTIBUF_Enable(1);
      157. //
      158. // Initially create "window 0"
      159. //
      160. WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin0, 0);
      161. while (1) {
      162. GUI_Delay(100);
      163. }
      164. }
      165. /*************************** End of file ****************************/
      Display All
      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.