Moving child windows of a window at one go!

Here is a function that moves all child windows of a window at one go! 🙂

[sourcecode language=’cpp’]void MoveChildWindows( HWND hParentWindow, const POINT& ptOffset )
{
   // Parent window should be valid
   if( !hParentWindow || !::IsWindow( hParentWindow ))
   {
      ASSERT( FALSE );
      return;
   }

   // Get first child window
   HWND hWnd = ::GetWindow( hParentWindow, GW_CHILD );
   while( hWnd )
   {
      // Window rectangle
      ::RECT rcRect;
      ::GetWindowRect( hWnd, &rcRect );

// Offset rectangle
      ::OffsetRect( &rcRect, ptOffset.x, ptOffset.y );

// Convert to parent co-ordinates from window co-ordinates
      ::MapWindowPoints( ::GetDesktopWindow(), hParentWindow, (LPPOINT)&rcRect, 2 );

      // Move window
      ::SetWindowPos( hWnd,
                      NULL,
                      rcRect.left,
                      rcRect.top,
                      0,
                      0,
                      SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE );

      // Get next window which is a sibling of this window
      hWnd = ::GetWindow( hWnd, GW_HWNDNEXT );
   }// End while
}// End MoveChildWindows[/sourcecode]

How to call:

[sourcecode language=’cpp’]POINT ptOffset = { -10, -10 };
MoveChildWindows( GetSafeHwnd(), ptOffset ); // Move all child windows left and up
ptOffset.x = 20, ptOffset.y = 20
MoveChildWindows( GetSafeHwnd(), ptOffset ); // Move all child windows right and down[/sourcecode]

4 thoughts on “Moving child windows of a window at one go!

  1. Good post!

    How about a follow up which uses DeferWindowPos? It would also be cool to see the windows animate or glide smoothly to the new location.. like your to-do list has any more room

Appreciate your comments...