Docking a child window to the parent window

3.9k Views Asked by At

I need my 6 controls (child windows of the main window) to get larger when the main window gets resized by the user (dragging the corners). I thought I could accomplish this by using the MoveWindow function to change the proportions of each child in the main window's WM_SIZE or WM_SIZING function. Doing this made the Debug build go strange (multiple windows, image of the window sticking after exiting, etc). The Release build ran fine but the child windows didn't change when I resized the main window.

I found http://msdn.microsoft.com/en-us/library/ms632598%28v=VS.85%29.aspx#creating_enumerating_etc used a different method of doing this: by enumerating all the child windows, and the enum callback function handling the window resizing through a unique ID assigned to every child. Upon trying this myself, it made no difference to the controls when the main window got resized.

Why isn't this working?

In the main windows switch statement:

case WM_SIZING:
        GetClientRect(hwnd, &hwndRect);
        EnumChildWindows(hwnd, EnumChildProc, (LPARAM)&hwndRect);
        break;

The child enumerator callback function:

BOOL CALLBACK EnumChildProc(HWND hwndChild, LPARAM lParam)
{
    LPRECT hwndRect = (LPRECT)lParam;
    switch(GetWindowLong(hwndChild, GWL_ID))
    {
        case ID_CHILD_LLABEL:
            MoveWindow(hwndChild, 0, 0, (hwndRect->right - hwndRect->left) - 30, 20,     false);
            break;
        case ID_CHILD_LDIR:
            MoveWindow(hwndChild, 12, 20, (hwndRect->right - hwndRect->left) - 40, 20,     false);
            break;
        case ID_CHILD_LLIST:
            MoveWindow(hwndChild, 12, 40, (hwndRect->right - hwndRect->left) - 40,         (hwndRect->bottom - hwndRect->top) - 238, false);
            break;
    }
}
1

There are 1 best solutions below

2
On

From MSDN's article on WM_SIZE: "If the SetScrollPos or MoveWindow function is called for a child window as a result of the WM_SIZE message, the bRedraw or bRepaint parameter should be nonzero to cause the window to be repainted." I suspect the child controls are moving, they simply aren't being repainted.

It might also be worth verifying that your switch cases are actually being hit.

Edit: I missed the obvious. You are responding to WM_SIZING, which indicates that the size of the window is about to (but has not yet) changed. WM_SIZE indicates that the size has changed. If you want to use WM_SIZING, you need to use the rect carried in the lParam, not the results of GetClientRect. Unfortunately the WM_SIZING rect is the rect of the window, not the client area, and is in screen coordinates. Unless you really need to display the resized controls while the user is still performing the resize, it would be much easier to just handle the WM_SIZE message.