CWnd* pParent = this; // parent window CWnd* pChildWnd = pParent->GetWindow(GW_CHILD); // first child window while (pChildWnd != NULL) { // do something with child window handle pChildWnd->m_hWnd pChildWnd = pChildWnd->GetNextWindow(GW_HWNDNEXT); }
CWnd* pWnd = CWnd::GetDesktopWindow(); // top-level window CWnd* pChildWnd = pWnd->GetWindow(GW_HWNDNEXT); // first child window while (pChildWnd != NULL) { // do something with top-level window handle pChildWnd->m_hWnd pChildWnd = pChildWnd->GetNextWindow(GW_HWNDNEXT); }This code retrieves the handle of the desktop window using the GetDesktopWindow function and then iterates through all subsequent top-level windows using the GetNextWindow function with the GW_HWNDNEXT flag, which returns top-level windows in Z-order. Package Library: Microsoft Foundation Classes (MFC) Library.