Adding Items to a List View Control

An application can add items to a list view control by using the LVM_INSERTITEM message. The attributes of a list view item that are specified by an LV_ITEM structure include a state, a label, an icon, and item data. Associated with each item may be one or more subitems, which are strings that appear to the right of an item in report view.

The example in this section adds a list view item for each line in a text file. Semicolons are assumed to separate the item label and the subitem strings that follow it. The example saves each item's label and subitem strings in a structure, which is defined in the application's header file, as follows.

#define C_COLUMNS 6

typedef struct myitem_tag {

LPSTR aCols[C_COLUMNS];

} MYITEM;

The application fills in an LV_ITEM structure and adds a list view item by using the LVM_INSERTITEM message. Because the application saves the item label in its own application-defined MYITEM structure, it specifies the LPSTR_TEXTCALLBACK value for the pszText member of the LV_ITEM structure. Specifying this value causes the control to send an LVN_GETDISPINFO notification message to its owner window whenever it needs to display the item. The address of the application-defined structure is saved as item data.

// InitListViewItems - adds items and subitems to a list view.

// Returns TRUE if successful or FALSE otherwise.

// hwndLV - handle of the list view control

// pfData - text file containing list view items with columns

// separated by semicolons

BOOL WINAPI InitListViewItems(HWND hwndLV, FILE *pfData)

{

extern char g_achTemp[256]; // temporary buffer

PSTR pszStart;

PSTR pszEnd;

int iItem;

int iSubItem;

LV_ITEM lvi;

// Initialize LV_ITEM members that are common to all items.

lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;

lvi.state = 0;

lvi.stateMask = 0;

lvi.pszText = LPSTR_TEXTCALLBACK; // app. maintains text

lvi.iImage = 0; // image list index

// Read each line in the specified file.

for (iItem = 0;

fgets(g_achTemp, sizeof(g_achTemp), pfData);

iItem++) {

// Allocate an application-defined structure to store the

// item label and the text of each subitem.

MYITEM *pItem = LocalAlloc(LPTR, sizeof(MYITEM));

// Copy the first string (the label).

pszEnd = strchr(g_achTemp, ';');

*pszEnd = '\0';

pItem->aCols[0] = DupString(g_achTemp);

// Copy subsequent strings (subitems).

for (iSubItem = 1;

iSubItem < C_COLUMNS && pszEnd != NULL;

iSubItem++) {

pszStart = pszEnd + 1;

if ((pszEnd = strchr(pszStart, ';')) != NULL)

*pszEnd = '\0';

pItem->aCols[iSubItem] = DupString(pszStart);

}

// Initialize item-specific LV_ITEM members.

lvi.iItem = iItem;

lvi.iSubItem = 0;

lvi.lParam = (LPARAM) pItem; // item data

// Add the item.

ListView_InsertItem(hwndLV, &lvi);

// There is no need to set the text of the subitems. They

// default to LPSTR_TEXTCALLBACK.

}

return TRUE;

}

// DupString - allocates a copy of a string.

// lpsz - address of the null-terminated string to copy

LPSTR DupString(LPSTR lpsz)

{

int cb = lstrlen(lpsz) + 1;

LPSTR lpszNew = LocalAlloc(LMEM_FIXED, cb);

if (lpszNew != NULL)

CopyMemory(lpszNew, lpsz, cb);

return lpszNew;

}

Software for developers
Delphi Components
.Net Components
Software for Android Developers
More information resources
MegaDetailed.Net
Unix Manual Pages
Delphi Examples
Databases for Amazon shops developers
Amazon Categories Database
Browse Nodes Database