Microsoft KB Archive/130697

{|
 * width="100%"|

How to Minimize Memory Allocations for New TreeView Control

 * }

Q130697

-

The information in this article applies to:


 * Microsoft Win32 Software Development Kit (SDK), versions 3.51, 4.0

-

SUMMARY
You can use the new TreeView Common Control to display a hierarchical list of items. This new control is available for Win32-based applications running under Windows NT or Windows 95. Applications typically use this control to display a list of items like directories or files on a given drive. Each node in a TreeView control allocates about 40 bytes. If the TreeView control displays a lot of items, applications can easily consume large amounts of memory, which slows down other applications.

Below are some techniques applications can use to minimize memory allocations for TreeView controls.

MORE INFORMATION
TreeView controls maintain internal data structures for every node added to the control. This data structure along with image lists associated with items and text strings for items can drain the physical memory available on the system.

Applications that need to display thousands of items or nodes in the TreeView control can be more proficient about memory allocations by doing the following:


 * 1) When inserting an item into a TreeView control, ensure that the pszText member of the TV_ITEM is not the actual string that needs to be displayed, but is the value LPSTR_TEXTCALLBACK. If a string pointer is passed, the control stores that string internally by allocating memory for it. When this flag is specified, the parent window of the control is responsible for storing the name (string). The string in most cases can be generated dynamically. In this case, the TreeView control sends the parent window a TVN_GETDISPINFO notification message when it needs the item text for displaying, sorting, or editing and sends a TVN_SETDISPINFO notification message when the item text changes.
 * 2) Fill the TreeView nodes on demand. One way to really minimize memory usage in a TreeView control is to fill in only the visible nodes. The TV_ITEM struct's cChildren member can be put to good use for this purpose. This is used as a flag to indicate whether the item has associated child items. It is 1 if the item has one or more child items; otherwise, it is 0 (zero). When inserting visible items into the TreeView control, set this cChildren member to 1 if that node will have child items under it. Do not insert the child items. When the user clicks the node, the application receives a TVN_ITEMEXPANDING with NM_TREEVIEW.action set to TVE_EXPAND. Insert the child items at that point. Then when the user clicks the same node again (to collapse the node), the applciation receives a TVN_ITEMEXPANDED with NM_TREEVIEW.action set to TVE_COLLAPSE. At that time, collapse the node and all its child items by calling TreeView_Expand (hWndTrevview, hItem, TVE_COLLAPSE|TVE_COLLAPSERESET). This frees up the memory used up by all the children or child items.
 * 3) If the application uses different icons for each child item in the TreeView control, specify the I_IMAGECALLBACK value for the iImage and iSeletedImage members of the TV_ITEM Structure. This way, the control doesn't have to store these images for every child item - thereby reducing the memory requirements for the control as a whole.

Additional query words: 4.00 usage user styles

Keywords :

Issue type :

Technology : kbWin32SDKSearch kbAudDeveloper kbSDKSearch kbWin32sSearch kbWin32SDK400 kbWin32SDK351