Calculating Free Memory

When Windows sends FREEMEM a WM_TIMER message, FREEMEM must determine the amount of free memory. Like the About box in the Program Manager and File Manager, FREEMEM gets a free memory value by calling GetFreeSpace with a parameter of 0. The GetFreeSpace function is new in Windows 3. Prior to Windows 3, Windows programs used GlobalCompact with a parameter of 0 to obtain the largest block of contiguous free memory in the system, which is not nearly as useful as the total amount of free memory. (Windows memory management is covered in detail in Chapter 7.)

If the free memory value has changed since the last GetFreeSpace call, FREEMEM invalidates the client area to generate a WM_PAINT message. FREEMEM processes WM_PAINT messages by calling DrawText, a convenient function for simple word-wrapped text. FREEMEM converts the free memory in bytes to a floating-point value in megabytes, and sprintf stores it formatted to two decimal places.