Resizing the Windows

When I finished creating the windows, I had to find an easy way to resize the application's main window. In the C version, I used the handy DeferWindowPos function to resize all the windows at the same time. For those who are new to Win32, DeferWindowPos updates a structure that contains multiple window positions. You use this function as you would use the window enumeration functions—that is, you begin, defer, and end. This code illustrates how I resized all the windows:

BOOL ResizeWindows (HWND hwnd)
{
RECT rcl;
HDWP hdwp;

// Get the client area of the parent window.
GetClientRect (hwnd, &rcl);

// You will defer all the application's windows.
hdwp = BeginDeferWindowPos (NUM_WINDOWS);

// First, reset the size of the status bar.
DeferWindowPos (hdwp, g_Listing.hWndStatus, NULL, 0, 0,
rcl.right - rcl.left, 20, SWP_NOZORDER | SWP_NOMOVE);

// Next, reset the size of the toolbar.
DeferWindowPos (hdwp, g_Listing.hWndToolbar, NULL, 0, 0,
rcl.right - rcl.left, 20, SWP_NOZORDER | SWP_NOMOVE);

// Next, reset the size of the tree view control.
DeferWindowPos (hdwp, g_Listing.hWndTreeView, NULL, 0, 0,
(rcl.right - rcl.left ) / 4, rcl.bottom - rcl.top - 46,
SWP_NOZORDER | SWP_NOMOVE);

// Last, reset the size of the list view control.
DeferWindowPos (hdwp, g_Listing.hWndListView, NULL,
(rcl.right - rcl.left ) / 4, 27,
(rcl.right - rcl.left) - ((rcl.right - rcl.left) / 4),
rcl.bottom - rcl.top - 46,
SWP_NOZORDER);

return EndDeferWindowPos (hdwp);
}

In MFCEXP, I added a handler for the WM_SIZE message, OnSize, to set the window positions for all the windows. The CToolBarCtrl class includes AutoSize, a special member function that resizes the toolbar to fit within the parent window.

void CMfcexpView::OnSize (UINT nType, int cx, int cy)
{
CView::OnSize (nType, cx, cy);

// Resize the toolbar.
m_Toolbar.AutoSize ();

// Resize the status bar.
// Make it fit along the bottom of the client area.
m_StatusBar.MoveWindow (0, cx - 10, cy, cy - 10);

// Set the rectangle for each part of the status bar.
// Make each 1/2 the width of the client area.
int aWidths [2];

aWidths [0] = cx / 2;
aWidths [1] = -1;
m_StatusBar.SetParts (2, aWidths);

m_TreeCtl.MoveWindow (0, 25, cx / 4, cy - 45);

m_ListCtl.MoveWindow (cx / 4, 25, cx, cy - 45);
}