Home > MFC, Windows API > Moving child windows of a window at one go!

Moving child windows of a window at one go!

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

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

How to call:

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
Advertisements
  1. Mostafa
    February 2, 2010 at 11:44 am

    Thanks a a lot.

  2. October 17, 2007 at 1:10 pm

    Sure, but can’t say when. 🙂

  3. October 17, 2007 at 1:04 pm

    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

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: