The Size of the Client Area

If you experiment with existing Windows applications, you'll find that window sizes can vary widely. At the most (assuming the window does not have a menu or scroll bars), the window can be maximized, and the client area will occupy the entire screen except for the caption bar. The minimum size of the window can be quite small, sometimes almost nonexistent, eliminating the client area.

One common method for determining the size of a window's client area is to process the WM_SIZE message within your window procedure. Windows sends a WM_SIZE message to a window procedure whenever the size of the window changes. The lParam variable passed to the window procedure contains the width of the client area in the low word and the height in the high word. The code to process this message looks like this:

static short cxClient, cyClient ;

[other program lines]

case WM_SIZE :

cxClient = LOWORD (lParam) ;

cyClient = HIWORD (lParam) ;

break ;

The LOWORD and HIWORD macros are defined in WINDOWS.H. Like cxChar and cyChar, the cxClient and cyClient variables are defined as static inside the window procedure because they are used later when processing other messages.

The WM_SIZE message will eventually be followed by a WM_PAINT message. Why? Because when we define the window class, we specify that the class style is:

CS_HREDRAW | CS_VREDRAW

This class style tells Windows to force a repaint if either the horizontal or vertical size changes.

You can calculate the number of full lines of text displayable within the client area with the formula:

cyClient / cyChar

This may be 0 if the height of the client area is too small to display a full character. Similarly, the approximate number of lowercase characters you can display horizontally within the client area is equal to:

cxClient / cxChar

If you determine cxChar and cyChar during a WM_CREATE message, don't worry about dividing by 0 in these calculations. Your window procedure receives a WM_CREATE message when WinMain calls CreateWindow. The first WM_SIZE message comes a little later when WinMain calls ShowWindow, at which point cxChar and cyChar have already been assigned positive values.

Knowing the size of the window's client area is the first step in providing a way for the user to move the text within the client area if the client area is not large enough to hold everything. If you're familiar with other Windows applications that have similar requirements, you probably know what we need: This is a job for scroll bars.