Come aggiungere elementi Tree-View
È possibile aggiungere un elemento a un controllo visualizzazione albero inviando il messaggio TVM_INSERTITEM al controllo . Il messaggio include l'indirizzo di una strutturaTVINSERTSTRUCT, specificando l'elemento padre, l'elemento dopo il quale viene inserito il nuovo elemento e una struttura TVITEM che definisce gli attributi dell'elemento. Gli attributi includono l'etichetta dell'elemento, le immagini selezionate e non selezionate e un valore definito dall'applicazione a 32 bit.
Cosa è necessario sapere
Tecnologie
Prerequisiti
- C/C++
- Programmazione dell'interfaccia utente di Windows
Disposizioni
Aggiungere elementi al Tree-View
Nell'esempio riportato in questa sezione viene illustrato come creare un sommario in base alle informazioni sull'intestazione del documento fornite in una matrice definita dall'applicazione. Ogni elemento della matrice è costituito da una stringa di intestazione e da un numero intero che indica il livello dell'intestazione. L'esempio supporta tre livelli di titolo (1, 2 e 3).
L'esempio include due funzioni. La prima funzione estrae ogni titolo con il relativo livello di intestazione e quindi li passa alla seconda funzione.
La seconda funzione aggiunge un elemento a un controllo visualizzazione albero. Usa il testo dell'intestazione come etichetta dell'elemento e utilizza il livello di intestazione del testo per identificare l'elemento principale per il nuovo elemento. Un'intestazione di livello 1 viene aggiunta alla radice del controllo visualizzazione albero, un'intestazione di livello due viene aggiunta come elemento figlio del livello 1 precedente e così via. La funzione assegna un'immagine a un elemento in base all'eventuale presenza di elementi figlio. Se un elemento contiene sotto-elementi, gli viene assegnata un'immagine che rappresenta una cartella chiusa. In caso contrario, ottiene un'immagine che rappresenta un documento. Un elemento usa la stessa immagine per gli stati selezionati e non selezionati.
// Adds items to a tree-view control.
// Returns the handle to the newly added item.
// hwndTV - handle to the tree-view control.
// lpszItem - text of the item to add.
// nLevel - level at which to add the item.
//
// g_nClosed, and g_nDocument - global indexes of the images.
HTREEITEM AddItemToTree(HWND hwndTV, LPTSTR lpszItem, int nLevel)
{
TVITEM tvi;
TVINSERTSTRUCT 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 = sizeof(tvi.pszText)/sizeof(tvi.pszText[0]);
// 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)(LPTVINSERTSTRUCT)&tvins);
if (hPrev == NULL)
return NULL;
// Save the handle to 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;
}
// Extracts heading text and heading levels from a global
// array and passes them to a function that adds them as
// parent and child items to a tree-view control.
// Returns TRUE if successful, or FALSE otherwise.
// hwndTV - handle to the tree-view control.
BOOL InitTreeViewItems(HWND hwndTV)
{
HTREEITEM hti;
// g_rgDocHeadings is an application-defined global array of
// the following structures:
// typedef struct
// {
// TCHAR tchHeading[MAX_HEADING_LEN];
// int tchLevel;
// } Heading;
for (int i = 0; i < ARRAYSIZE(g_rgDocHeadings); i++)
{
// Add the item to the tree-view control.
hti = AddItemToTree(hwndTV, g_rgDocHeadings[i].tchHeading,
g_rgDocHeadings[i].tchLevel);
if (hti == NULL)
return FALSE;
}
return TRUE;
}
Argomenti correlati