INF: Using Window Extra Bytes in Custom Controls

ID Number: Q67249

3.00 3.10

WINDOWS

Summary:

In the Microsoft Windows environment, custom controls are often

designed to store data about their state, color, or position. This

data can be stored directly in window extra bytes associated with the

custom control window or in a local or a global memory object.

If a control uses window extra bytes directly, Windows allocates

memory from the user heap, which is a scarce system resource shared by

all applications. In contrast, the LocalAlloc function allocates

memory from the heap of the application or the dynamic-link library

(DLL) in which the control is implemented.

If six or fewer bytes of data are required for each instance of a

custom control, the demand placed on the system user heap is not

large. In contrast, each window in Windows version 3.0 requires at

least 62 bytes of user heap; therefore, using a small number of window

extra bytes is easily justified.

If the control requires more than six bytes of data, the control class

should set the number of window extra bytes to the size of one HANDLE

data type. When the control is created, call the LocalAlloc function

to allocate enough memory to hold the data associated with the

control. Store the handle that LocalAlloc returns in the window extra

bytes.

More Information:

Depending on the amount of storage required, storing a handle to a

local memory block in the window extra bytes is usually advantageous.

This method requires calling the LocalAlloc function once at

initialization time and calling the LocalLock and LocalUnlock

functions each time the control processes a message that refers to or

modifies any status information. Calling LocalLock and LocalUnlock is

much faster than calling GetWindowWord to access each item of data.

Storing a memory handle in the window extra bytes also provides the

control the option of using a global memory block if a very large

amount of data must be stored.

Using an allocated memory also allows the application to vary the

amount of data stored for each custom control by storing the size of

the structure or an index that describes the structure at the

beginning of the block of memory.

Additional reference words: 3.00 3.10