Home   Index   About
Ultimate Pack


Custom Search
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;

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun