How to get position of a child window in window coordinates

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

    • Hi,

      To get the position of a window relative to its parent simply subtract the screen position of the parent from the screen position of the child window.

      Try this:

      C Source Code

      1. #include "DIALOG.h"
      2. /*********************************************************************
      3. *
      4. * Static code
      5. *
      6. **********************************************************************
      7. */
      8. /*********************************************************************
      9. *
      10. * _cbWinParent
      11. */
      12. static void _cbWinParent(WM_MESSAGE * pMsg) {
      13. WM_HWIN hChild;
      14. GUI_RECT Rect;
      15. int xPos;
      16. int yPos;
      17. switch (pMsg->MsgId) {
      18. case WM_PAINT:
      19. GUI_SetBkColor(GUI_RED);
      20. GUI_Clear();
      21. hChild = WM_GetFirstChild(pMsg->hWin);
      22. xPos = WM_GetWindowOrgX(hChild) - WM_GetWindowOrgX(pMsg->hWin);
      23. yPos = WM_GetWindowOrgY(hChild) - WM_GetWindowOrgY(pMsg->hWin);
      24. break;
      25. default:
      26. WM_DefaultProc(pMsg);
      27. break;
      28. }
      29. }
      30. /*********************************************************************
      31. *
      32. * _cbBk
      33. */
      34. static void _cbWinChild(WM_MESSAGE * pMsg) {
      35. switch (pMsg->MsgId) {
      36. case WM_PAINT:
      37. GUI_SetBkColor(GUI_GREEN);
      38. GUI_Clear();
      39. break;
      40. default:
      41. WM_DefaultProc(pMsg);
      42. break;
      43. }
      44. }
      45. /*********************************************************************
      46. *
      47. * Public code
      48. *
      49. **********************************************************************
      50. */
      51. /*********************************************************************
      52. *
      53. * MainTask
      54. */
      55. void MainTask(void) {
      56. WM_HWIN hWinParent;
      57. WM_HWIN hWinChild;
      58. GUI_Init();
      59. hWinParent = WM_CreateWindowAsChild(10, 10, 150, 150, WM_HBKWIN, WM_CF_SHOW, _cbWinParent, 0);
      60. hWinChild = WM_CreateWindowAsChild(10, 10, 50, 50, hWinParent, WM_CF_SHOW, _cbWinChild, 0);
      61. while (1) {
      62. GUI_Delay(100);
      63. }
      64. }
      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.