Global Key callback for all windows

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

  • Hi,

    You can set one callback for as many windows as you want.

    Here is some code which shows how you could proceed:

    C Source Code

    1. #include "DIALOG.h" // Include almost everything
    2. /*********************************************************************
    3. *
    4. * Defines
    5. *
    6. **********************************************************************
    7. */
    8. #define ID_WINDOW_0 (GUI_ID_USER + 0x00)
    9. #define ID_WINDOW_1 (GUI_ID_USER + 0x01)
    10. #define ID_WINDOW_2 (GUI_ID_USER + 0x02)
    11. #define ID_WINDOW_3 (GUI_ID_USER + 0x03)
    12. /*********************************************************************
    13. *
    14. * Types
    15. *
    16. **********************************************************************
    17. */
    18. typedef struct {
    19. int Id;
    20. char acText[100];
    21. GUI_COLOR Color;
    22. } WIN_DATA;
    23. /*********************************************************************
    24. *
    25. * Static data
    26. *
    27. **********************************************************************
    28. */
    29. /*********************************************************************
    30. *
    31. * Static code
    32. *
    33. **********************************************************************
    34. */
    35. /*********************************************************************
    36. *
    37. * _cbDialog
    38. */
    39. static void _cbWin(WM_MESSAGE * pMsg) {
    40. GUI_RECT Rect;
    41. WIN_DATA * pWinData;
    42. switch (pMsg->MsgId) {
    43. case WM_PAINT:
    44. //
    45. // Receive the user data set in MainTask()
    46. //
    47. WM_GetUserData(pMsg->hWin, &pWinData, sizeof(pWinData));
    48. //
    49. // Get the client area and draw a gradient beginning with the color set in the user data
    50. //
    51. WM_GetClientRect(&Rect);
    52. GUI_DrawGradientH(Rect.x0, Rect.y0, Rect.x1, Rect.y1, pWinData->Color, GUI_DARKGRAY);
    53. //
    54. // Display the text set in userdata
    55. //
    56. GUI_SetTextMode(GUI_TM_TRANS | GUI_TM_XOR);
    57. GUI_DispStringInRect(pWinData->acText, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER);
    58. //
    59. // Distinguish between the different windows using their ID and draw different shapes
    60. //
    61. GUI_SetColor(GUI_MAGENTA);
    62. switch (pWinData->Id) {
    63. case ID_WINDOW_0:
    64. GUI_DrawCircle(Rect.x1 / 2, Rect.y1 / 2, Rect.x1 / 2 - 10);
    65. break;
    66. case ID_WINDOW_1:
    67. GUI_DrawRect(Rect.x0 + 10, Rect.y0 + 10, Rect.x1 - 10, Rect.y1 - 10);
    68. break;
    69. case ID_WINDOW_2:
    70. GUI_DrawEllipse(Rect.x1 / 2, Rect.y1 / 2, Rect.x1 / 2 - 10, Rect.y1 / 2 - 20);
    71. break;
    72. case ID_WINDOW_3:
    73. GUI_DrawRoundedRect(Rect.x0 + 10, Rect.y0 + 10, Rect.x1 - 10, Rect.y1 - 10, 10);
    74. break;
    75. }
    76. break;
    77. default:
    78. WM_DefaultProc(pMsg);
    79. break;
    80. }
    81. }
    82. /*********************************************************************
    83. *
    84. * Public code
    85. *
    86. **********************************************************************
    87. */
    88. /*********************************************************************
    89. *
    90. * MainTask
    91. */
    92. void MainTask(void) {
    93. WM_HWIN hWin;
    94. int i;
    95. static WIN_DATA * pWinData;
    96. static WIN_DATA aWinData[4] = {
    97. { ID_WINDOW_0, "Window 0", GUI_RED },
    98. { ID_WINDOW_1, "Window 1", GUI_GREEN },
    99. { ID_WINDOW_2, "Window 2", GUI_BLUE },
    100. { ID_WINDOW_3, "Window 3", GUI_WHITE }
    101. };
    102. //
    103. // Initialize GUI
    104. //
    105. GUI_Init();
    106. //
    107. // Create first window
    108. //
    109. hWin = WM_CreateWindowAsChild( 10, 10, 100, 100, WM_HBKWIN, WM_CF_SHOW, _cbWin, sizeof(pWinData));
    110. pWinData = &aWinData[0];
    111. WM_SetUserData(hWin, &pWinData, sizeof(pWinData));
    112. //
    113. // Create second window
    114. //
    115. hWin = WM_CreateWindowAsChild(120, 10, 100, 100, WM_HBKWIN, WM_CF_SHOW, _cbWin, sizeof(pWinData));
    116. pWinData = &aWinData[1];
    117. WM_SetUserData(hWin, &pWinData, sizeof(pWinData));
    118. //
    119. // Create third window
    120. //
    121. hWin = WM_CreateWindowAsChild( 10, 120, 100, 100, WM_HBKWIN, WM_CF_SHOW, _cbWin, sizeof(pWinData));
    122. pWinData = &aWinData[2];
    123. WM_SetUserData(hWin, &pWinData, sizeof(pWinData));
    124. //
    125. // Create fourth window
    126. //
    127. hWin = WM_CreateWindowAsChild(120, 120, 100, 100, WM_HBKWIN, WM_CF_SHOW, _cbWin, sizeof(pWinData));
    128. pWinData = &aWinData[3];
    129. WM_SetUserData(hWin, &pWinData, sizeof(pWinData));
    130. while (1) {
    131. GUI_Delay(100);
    132. }
    133. }
    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.