Adding Tree-View Items

You add an item to a tree-view control by sending the TVM_INSERTITEM message to the control. The message includes a pointer to a TV_INSERTSTRUCT structure, specifying the parent item, the item after which the new item is inserted, and a TV_ITEM structure that defines the attributes of the item. The attributes include the item's label, its selected and nonselected images, and a 32-bit application-defined value.

The example in this section creates a table of contents based on the information in a text file. The example includes two functions. The first function searches a file for headings. When it finds one, it extracts the text of the heading and the value that indicates the level of the heading and then passes them to the second function. Headings are assumed to be in the following form, .[heading].n, where heading is the text of the heading and n indicates the heading level. The example ignores heading levels greater than level four.

The second function adds an item to a tree-view control, using the heading text as the item's label and the heading level to determine the parent item for the new item. A level one heading is added to the root of the tree-view control, a level two heading is added as a child item of the previous level one item, and so on. The function assigns an image to an item based on whether it has any child items. If an item has child items, it gets an image representing a closed folder. Otherwise, it gets an image representing a document. An item uses the same image for both the selected and nonselected states.

// InitTreeViewItems - extracts headings from the specified file and

// passes them to a function that adds them to a tree-view control.

// Returns TRUE if successful or FALSE otherwise.

// hwndTV - handle of the tree-view control

// lpszFileName - name of file with headings

BOOL InitTreeViewItems(HWND hwndTV, LPSTR lpszFileName)

{

HANDLE hf; // handle of file

DWORD cbRead; // number of bytes read

char szItemText[128]; // label text of tree-view item

int nLevel; // heading level

LPCH pch; // pointer to data read from file

LPCH pchTmp; // temporary pointer

DWORD i, j; // counters

// Open the file to parse.

if ((hf = CreateFile(lpszFileName, GENERIC_READ,

FILE_SHARE_READ, (LPSECURITY_ATTRIBUTES) NULL,

OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,

(HANDLE) NULL)) == (HANDLE) INVALID_HANDLE_VALUE)

return FALSE;

// Parse the file looking for headings.

pch = (LPCH) LocalAlloc(LPTR, sizeof(char) * 2048);

pchTmp = pch;

do {

// Read a chunk of the file.

ReadFile(hf, pchTmp, sizeof(char) * 2048, &cbRead,

(LPOVERLAPPED) NULL);

// Parse the chunk looking for ".[".

for (i = 0, j = 0; i < cbRead; i++) {

if ((i + 2) > cbRead) // break if end of chunk

break;

// Extract the heading text from between the brackets.

if ((*pchTmp == '.') && (*(pchTmp+1) == '[')) {

pchTmp = pchTmp + 2;

i += 2;

while (*pchTmp != ']' && ((i++) <= cbRead) )

szItemText[j++] = *pchTmp++;

szItemText[j] = '\0';

j = 0;

nLevel = atoi(pchTmp + 2);

// Add the item to the tree-view control.

AddItemToTree(hwndTV, (LPSTR) &szItemText, nLevel);

} else

++pchTmp;

}

pchTmp = pch;

} while (cbRead != 0);

CloseHandle((HANDLE) hf);

return TRUE;

}

// AddItemToTree - adds items to a tree-view control.

// Returns the handle of the newly added item.

// hwndTV - handle of the tree-view control

// lpszItem - text of the item to add

// nLevel - level at which to add the item

HTREEITEM AddItemToTree(HWND hwndTV, LPSTR lpszItem, int nLevel)

{

TV_ITEM tvi;

TV_INSERTSTRUCT tvins;

static HTREEITEM hPrev = (HTREEITEM) TVI_FIRST;

static HTREEITEM hPrevRootItem = NULL;

static HTREEITEM hPrevLev2Item = NULL;

HTREEITEM hti;

tvi.mask = TVIF_TEXT | TVIF_IMAGE

| TVIF_SELECTEDIMAGE | TVIF_PARAM;

// Set the text of the item.

tvi.pszText = lpszItem;

tvi.cchTextMax = lstrlen(lpszItem);

// Assume the item is not a parent item, so give it a

// document image.

tvi.iImage = g_nDocument;

tvi.iSelectedImage = g_nDocument;

// Save the heading level in the item's application-defined

// data area.

tvi.lParam = (LPARAM) nLevel;

tvins.item = tvi;

tvins.hInsertAfter = hPrev;

// Set the parent item based on the specified level.

if (nLevel == 1)

tvins.hParent = TVI_ROOT;

else if (nLevel == 2)

tvins.hParent = hPrevRootItem;

else

tvins.hParent = hPrevLev2Item;

// Add the item to the tree-view control.

hPrev = (HTREEITEM) SendMessage(hwndTV, TVM_INSERTITEM, 0,

(LPARAM) (LPTV_INSERTSTRUCT) &tvins);

// Save the handle of the item.

if (nLevel == 1)

hPrevRootItem = hPrev;

else if (nLevel == 2)

hPrevLev2Item = hPrev;

// The new item is a child item. Give the parent item a

// closed folder bitmap to indicate it now has child items.

if (nLevel > 1) {

hti = TreeView_GetParent(hwndTV, hPrev);

tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;

tvi.hItem = hti;

tvi.iImage = g_nClosed;

tvi.iSelectedImage = g_nClosed;

TreeView_SetItem(hwndTV, &tvi);

}

return hPrev;

}

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