Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować się zalogować lub zmienić katalog.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Element można dodać do kontrolki widoku drzewa, wysyłając komunikat TVM_INSERTITEM do kontrolki. Komunikat zawiera adres struktura TVINSERTSTRUCT, określając element nadrzędny, element, po którym zostanie wstawiony nowy element, oraz struktura TVITEM definiującą atrybuty elementu. Atrybuty obejmują etykietę elementu, wybrane i niezaznaczone obrazy oraz 32-bitową wartość zdefiniowaną przez aplikację.
Co musisz wiedzieć
Technologie
Warunki wstępne
- C/C++
- Programowanie interfejsu użytkownika systemu Windows
Instrukcje
Dodaj elementy do Tree-View
W przykładzie w tej sekcji pokazano, jak utworzyć spis treści na podstawie informacji o nagłówku dokumentu udostępnianych w tablicy zdefiniowanej przez aplikację. Każdy element tablicy składa się z ciągu znaków pełniącego rolę nagłówka oraz liczby całkowitej określającej poziom tego nagłówka. W przykładzie są obsługiwane trzy poziomy nagłówków (1, 2 i 3).
Przykład zawiera dwie funkcje. Pierwsza funkcja wyodrębnia każdy nagłówek i towarzyszący poziom nagłówka, a następnie przekazuje je do drugiej funkcji.
Druga funkcja dodaje element do kontrolki widoku drzewa. Używa on tekstu nagłówka jako etykiety elementu i używa poziomu nagłówka do określenia elementu nadrzędnego dla nowego elementu. Nagłówek poziomu pierwszego jest dodawany do głównego węzła kontrolki widoku drzewa, a nagłówek poziomu drugiego jest dodawany jako element podrzędny poprzedniego elementu poziomu pierwszego i tak dalej. Funkcja przypisuje obraz do elementu na podstawie tego, czy ma elementy podrzędne. Jeśli element zawiera elementy podrzędne, pobiera obraz reprezentujący zamknięty folder. W przeciwnym razie pobiera obraz reprezentujący dokument. Element używa tego samego obrazu zarówno dla wybranych, jak i nie zaznaczonych stanów.
// 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;
}