A list-view control is a window that displays a collection of items; each item consists of an icon and a label. List-view controls provide several ways to arrange and display items. For example, additional information about each item can be displayed in columns to the right of the icon and label. The following illustration shows a window with a list-view control in icon view.

To create a list-view control, call the
Normally you will use the resource editor to create a list view control.
List-view controls display items in four different views. The control's window style specifies the current view. Additional window styles specify the alignment of items and control-specific features. The following table describes the four views.
| View Name | Description |
|---|---|
| Icon view | Specified by the |
| Small icon view | Specified by the |
| List view | Specified by the |
| Report view | Specified by the |
You can change the view type after you create a list-view control. To
retrieve and change the window style, use the
You can control the way items are arranged in icon or small icon view by
specifying either the
You can change the alignment after you create a list-view control. To
determine the current alignment, use the
Additional window styles provide other options, such as whether a user can
edit labels or select more than one item at a time. For a complete list, see
The extended list-view control styles provide options such as check boxes,
flat scroll bars, grid lines, and hot-tracking. For a complete list, see
There are two messages that set and retrieve extended style information,
A virtual list view is a list-view control that has the
A virtual list-view control maintains very little item information itself.
Except for the item selection and focus information, the owner of the control
must manage all item information. Other processes request item information from
the owner by using
Because this type of list control is intended for large data sets, it is
recommended that you cache requested item data to improve retrieval performance.
The list view provides a cache-hinting mechanism to assist in optimizing the
cache. The hint is implemented in the form of an
You create virtual list-view controls using the CreateWindow or CreateWindowEx function, specifying the LVS_OWNERDATA window style as part of the dwStyle function parameter.
You can use the LVS_OWNERDATA style in combination with most other window
styles, except the
All four of the list-view styles—icon, small icon, list, and report
view—support the LVS_OWNERDATA style. List-view controls that have the
LVS_OWNERDATA style do not store any item-specific information. Therefore, the
only valid item state flags that you can apply to an item are
Most list-view control messages and the associated macros are fully supported. However, some messages have limitations or are unsupported when you use the LVS_OWNERDATA style. The following table summarizes the affected messages.
| Message | Limitation |
|---|---|
| Does not support the LVA_SNAPTOGRID style. | |
| Sets the item count to zero and clears all internal selection variables, but it does not actually delete any items. It makes a notification callback. | |
| Is supported for selection integrity only and does not actually delete an item. | |
| Returns only focus and selection states (that is, those states stored by the list-view control). | |
| Does not support the list-view search criteria LVNI_CUT, LVNI_HIDDEN, or LVNI_DROPHILITED. All other criteria are supported. | |
| Is not supported. | |
| Is supported for selection integrity only. | |
| Is not supported. To set the item state, use the |
|
| Sets the number of items currently in the list. If the list-view control sends a notification that requests data for any item up to the maximum set, the owner must be prepared to supply that data. The message parameters support virtual list-view controls. | |
| Is not supported. | |
| Allows only the selection and focus states to be changed for the item. | |
| Is not supported. It is the application's responsibility to maintain the item texts. | |
| Is not supported. | |
| Is not supported. It is the application's responsibility to present the items in the desired order. |
List-view controls with the LVS_OWNERDATA style send the same notification
messages as other list-view controls and two additional ones: LVN_ODCACHEHINT
and
| LVN_GETDISPINFO | A virtual list-view control maintains very little item information on its own. As a result, it often sends the LVN_GETDISPINFO notification message to request item information. This message is handled in much the same way as callback items in a standard list control. Because the number of items supported by the control can be very large, caching item data improves performance. When handling LVN_GETDISPINFO, the owner of the control first attempts to supply requested item information from the cache (for more information, see Cache Management). If the requested item is not cached, the owner must be prepared to supply the information by other means. |
| LVN_ODCACHEHINT | A virtual list view sends the LVN_ODCACHEHINT notification message to assist in optimizing the cache. The notification message provides inclusive index values for a range of items that it recommends be cached. Upon receiving the notification message, the owner must be prepared to load the cache with item information for the requested range so that the information will be readily available when an LVN_GETDISPINFO message is sent. |
| LVN_ODFINDITEM | The LVN_ODFINDITEM notification message is sent by a virtual
list-view control when the control needs the owner to find a particular
callback item. The notification message is sent when the list-view
control receives quick key access or when it receives an |
A list-view control with the LVS_OWNERDATA style produces a large number of LVN_GETDISPINFO
notification messages and, to assist in optimizing the cache, an LVN_ODCACHEHINT
message. LVN_ODCACHEHINT messages provide information about the
recommended items to include within the cache. These messages are sent as
The NMLVCACHEHINT structure includes two integer members, iFrom and iTo, that represent the inclusive endpoints of a range of items that most likely will be needed. The owner must be prepared to load the cache with the item information for each of the items within the recommended range.
The list control often needs item information for the first item (offset 0). The LVN_ODCACHEHINT notification message might not always include item 0, but it must always be included in the cache.
The last items in the list are accessed often. Therefore, the owner might want to keep a second cache that includes the items at the end of the list. The requested range from LVN_ODCACHEHINT can be checked against the end cache to make it available automatically instead of reloading the same end range each time.
List-view controls support working areas, which are rectangular virtual areas that the list-view control uses to arrange its items. A working area is not a window and cannot have a visible border. By default, the list-view control has no working areas. By creating a working area, you can create an empty border on the left, top, or right of the items or cause a horizontal scroll bar to be displayed when there normally would not be one.
The number of working areas can be retrieved with the
Changing the working area has no effect on list-view controls that have the LVS_LIST or LVS_REPORT view, but the working areas will be maintained when the view type is changed. With the LVS_ICON and LVS_SMALLICON views, the working area can be modified to change the way the items are displayed. Making the width of the working area (right - left) greater than the client width of the control causes the items to be wrapped at that width and the horizontal scroll bar to be displayed. Making the width of the working area narrower than the width of the control's client area causes the items to be wrapped within the working area and not the client area. Setting the left or top member to a positive value causes the items to be displayed starting at the working area, creating an empty space between the edge of the control and the items. An empty space can also be created between the right edge of the control and the items by making the width of the working area less than the client width of the control.
By default, a list-view control does not display item images. To display item images, you must create image lists and associate them with the control. A list-view control can have three image lists:
The full-sized and small icon image lists can also contain overlay images, which are designed to be drawn transparently over the item icons.
To use overlay images in a list-view control:
If a state image list is specified, a list-view control reserves space to the left of each item's icon for a state image.
To associate a state image with an item, use the
By default, when a list-view control is destroyed, it destroys the image
lists assigned to it. However, if a list-view control has the
Each item in a list-view control has an icon, a label, a current state, and an application-defined value. By using list-view messages, you can add, modify, and delete items as well as retrieve information about items.
Each item can have one or more subitems. A subitem is a string that, in report view, is displayed in a column separate from the item's icon and label. To specify the text of a subitem, use the LVM_SETITEMTEXT or LVM_SETITEM message. All items in a list-view control have the same number of subitems. The number of subitems is determined by the number of columns in the list-view control. When you add a column to a list-view control, you specify its associated subitem index.
The LVITEM structure defines a list-view item or subitem. The iItem member is the zero-based index of the item. The iSubItem member is the one-based index of a subitem or zero if the structure contains information about an item. Additional members specify the item's text, icon, state, and item data. Item data is an application-defined value associated with a list-view item.
To add an item to a list-view control, use the LVM_INSERTITEM message,
specifying the address of an LVITEM structure. Before adding multiple
items, you can send the control an LVM_SETITEMCOUNT message, specifying
the number of items the control will ultimately contain. This message enables
the list-view control to reallocate its internal data structures only once
rather than every time you add an item. You can determine the number of items in
a list-view control by using the
To change the attributes of a list-view item, use the LVM_SETITEM message, specifying the address of an LVITEM structure. The mask member of this structure specifies the item attributes you want to change. For example, to change only the text of an item or subitem, use the LVM_SETITEMTEXT message.
To retrieve information about a list-view item, use the
To delete a list-view item, use the LVM_DELETEITEM message. You can delete all items in a list-view control by using the LVM_DELETEALLITEMS message.
An item's state is a value that specifies the item's availability, indicates user actions, or otherwise reflects the item's status. A list-view control changes some state bits, such as when the user selects an item. An application might change other state bits to disable or hide the item or to specify an overlay image or state image. For more information about overlay images and state images, see List-View Image Lists.
An item's state is specified by the state member of the LVITEM structure. When you specify or change an item's state, the stateMask member specifies which state bits you need to change. You can change an item's state by using the LVM_SETITEMSTATE message. You can specify an item's state when you create it or when you change its attributes by using the LVM_SETITEM message. To determine an item's current state, use the LVM_GETITEMSTATE or LVM_GETITEM message.
To set an item's overlay image, the stateMask member of the LVITEM structure must include the LVIS_OVERLAYMASK value, and the state member must include the one-based index of the overlay image shifted left 8 bits by using the INDEXTOOVERLAYMASK macro. The index can be zero to specify no overlay image.
To set an item's state image, the stateMask member of the LVITEM structure must include the LVIS_STATEIMAGEMASK value, and the state member must include the one-based index of the state image shifted left 12 bits by using the INDEXTOSTATEIMAGEMASK macro. The index can be zero to specify no state image.
For each of its items, a list-view control typically stores the label text, the image list index of the item's icons, and a set of bit flags for the item's state. You can define callback items or change the control's callback mask to indicate that the application, rather than the control, stores some or all of this information. You might want to use callbacks if your application stores some of this information.
A callback item in a list-view control is an item for which the application stores the text or icon index, or both. You can define callback items when you send the LVM_INSERTITEM message to add an item to the list-view control. If the application stores the text for the item or subitem, set the pszText member of the item's LVITEM structure to LPSTR_TEXTCALLBACK. If the application stores the icon index for an item, set the iImage member of the item's LVITEM structure to I_IMAGECALLBACK.
The callback mask of a list-view control is a set of bit flags that
specify the item states for which the application, rather than the control,
stores the current data. The callback mask applies to all of the control's
items, unlike the callback item designation, which applies to a specific item.
The callback mask is zero by default, meaning that the list-view control stores
all item state information. After creating a list-view control and initializing
its items, you can send the LVM_SETCALLBACKMASK message to change the
callback mask. To retrieve the current callback mask, send the
When a list-view control must display or sort a list-view item for which the
application stores callback information, the control sends the LVN_GETDISPINFO
notification message to the control's parent window. This message specifies an
If the list-view control detects a change in an item's callback information,
such as a change in the text, icon, or state information, the control sends an
If you change a callback item's attributes or state bits, you use the
By effectively using callback items and the callback mask, you can ensure that each item attribute is maintained in only one place. Doing this can simplify your application, but the only space saved is the memory that would otherwise be required to store item labels and subitem text.
Every list-view item has a position and size, which you can retrieve and set using messages. You can also determine which item, if any, is at a specified position. The position of list-view items is specified in view coordinates, which are client coordinates offset by the scroll position.
To retrieve and set an item's position, use the
You can determine which item, if any, is at a particular location by using
the
To retrieve the bounding rectangle for a list item or only for its icon or
label, use the
You can use list-view messages to arrange and sort items and to find items based on their attributes or positions. Arranging repositions items to align on a grid, but the indexes of the items do not change. Sorting changes the sequence of items (and their corresponding indexes) and then repositions them accordingly. You can arrange items only in icon and small icon views, but you can sort items in any view. To find items, you send list-view messages that specify an item location or property.
To arrange items, use the LVM_ARRANGE message. You can ensure that items are arranged at all times by specifying the LVS_AUTOARRANGE window style.
To sort items, use the LVM_SORTITEMS message. When you sort using this message, you specify an application-defined callback function that the list-view control calls to compare the relative order of any two items. The control passes to the comparison function the item data associated with each of the two items. The item data is the value that was specified in the lParam member of the item's LVITEM structure when it was inserted into the list. By specifying the appropriate item data and supplying an appropriate comparison function, you can sort items by their label, by any subitem, or by any other property. Note that sorting items does not reorder the corresponding subitems. When items are reordered, their corresponding subitems are carried with them; that is, whole rows are kept together. To order the columns separately from one another, detaching the subitems from their items, you must regenerate the columns after sorting using LVM_SETITEM.
You can ensure that a list-view control is always sorted by specifying the LVS_SORTASCENDING or LVS_SORTDESCENDING window style. Controls with these styles use the label text of the items to sort them in ascending or descending order. You cannot supply a comparison function when using these window styles. If a list-view control has either of these styles, an LVM_INSERTITEM message will fail if you try to insert an item that has LPSTR_TEXTCALLBACK as the pszText member of its LVITEM structure.
You can find a list-view item with specific properties by using the LVM_FINDITEM message. You can find a list-view item that is in a specified state and has a specified relationship to a given item by using the LVM_GETNEXTITEM message. For example, you can retrieve the next selected item to the right of a specified item.
Columns control the way items and their subitems are displayed in report
view. Each column has a title and width and is associated with a specific
subitem; subitem zero is the item's icon and label. The attributes of a column
are defined by an
To add a column to a list-view control, use the
Unless the LVS_NOCOLUMNHEADER window style is specified, column headers
appear in report view. The user can click a column header, causing an
List-view controls can display images next to column titles. To implement this feature, specify the LVCF_IMAGE value and assign the index of the image to the iImage member in the LVCOLUMN structure.
List-view controls can set the order in which columns are displayed. To implement this feature, specify the LVCF_ORDER value and assign the column order to the iOrder member in the LVCOLUMN structure. The column order is zero-based and is in left-to-right order. For example, zero indicates the leftmost column.
Unless the
In icon view or small icon view, the current scroll position is defined by
the view origin. The view origin is the set of coordinates, relative to
the visible area of the list-view control, that correspond to the view
coordinates (0, 0). To retrieve the current view origin, use the
In list or report view, the current scroll position is defined by the top
index. The top index is the index of the first visible item in the list-view
control. To retrieve the current top index, use the
You can use the
The
To scroll a list-view control by a specific amount, use the
A list-view control that has the
When label editing begins, an
One use for an LVN_BEGINLABELEDIT notification handler is to control
which labels the user can edit. To prevent label editing, return a nonzero
value. To customize label editing, have the notification handler retrieve a
handle to the edit control by sending an
When label editing is canceled or completed, a list-view control sends its
parent window an
An application can retrieve and set three colors for a list-view control.
| Color | Messages used to retrieve and set colors |
|---|---|
| Text color | |
| Text background color | |
| Window background color |
To customize the appearance of a list-view control more significantly, use
The list-view control in the updated version of the common controls delivered with Windows XP contains features that enable you to provide an intuitive interface for your applications and to present visual information more effectively. The features include:
You can also apply visual styles to common controls defined by ComCtl32.dll version 6. Applying visual styles enables you to define the appearance of controls and windows. If you want your application to use visual styles, you must add an application manifest to ensure that ComCtl32.dll version 6 is available. For more information about using manifests and visual styles see Using Windows XP Visual Styles.
The grouping features of the list-view control in Windows XP, enable you to visually group logically related sets of items. These groups are separated on the screen by a horizontal group header that contains the title and the number of items in the group. The following image shows some grouped items.

The grouping application programming interface (API) enables group and group element management by providing messages that add items to groups, add groups to views, sort group items, and query groups for item size and other information.
Insertion marks show users where dragged items will be placed. Insertion marks currently display when the user drags an item into the Start menu or Quick Launch bar. The insertion mark also works for lists that are set to autoarrange. When a user drags an item to a point between two other items, the insertion mark shows the item's expected new location. The following image shows an insertion mark.

The insertion mark API elements enable placement of insertion marks by providing messages and flags that perform hit detection, that specify the location and appearance of the insertion mark by item, and that query for information about the current size and appearance of the insertion mark.
The tile-view features of the list-view control enable you to provide a visual balance between graphical and textual information. The textual information displayed for an item in tile view is the same as the columns information defined for report view. Tile view functions in combination with either the grouping or insertion mark features in the list-view control. The tile view uses a 32 x 32 icon and several lines of text, as shown in the following image.

The tile-view APIs enable you to specify which column fields to display for each item, and to collectively control the size and appearance of all items within a tile-view window. For clarity, the first line of text in a tile is always the item's name.