Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Anda menambahkan item ke kontrol tampilan pohon dengan mengirim pesan TVM_INSERTITEM ke kontrol. Pesan mencakup alamat struktur TVINSERTSTRUCT, yang menentukan item induk, item setelah item baru dimasukkan, dan struktur TVITEM yang menetapkan atribut item. Atribut termasuk label item, gambar yang dipilih dan tidak dipilih, dan nilai yang ditentukan oleh aplikasi 32-bit.
Apa yang perlu Anda ketahui
Teknologi
Prasyarat
- C/C++
- Pemrograman Antarmuka Pengguna Windows
Petunjuk
Tambahkan Item ke Tree-View
Contoh di bagian ini menunjukkan cara membuat daftar isi berdasarkan informasi judul dokumen yang disediakan dalam array yang ditentukan aplikasi. Setiap elemen array terdiri dari string judul dan bilangan bulat yang menunjukkan tingkat judul. Contoh mendukung tiga tingkat judul (1, 2, dan 3).
Contohnya mencakup dua fungsi. Fungsi pertama mengekstrak setiap judul dan tingkat judul yang menyertainya dan kemudian meneruskannya ke fungsi kedua.
Fungsi kedua menambahkan item ke kontrol tampilan pohon. Ini menggunakan teks judul sebagai label item, dan menggunakan tingkat judul untuk menentukan item induk untuk item baru. Judul tingkat satu ditambahkan ke akar kontrol tampilan pohon, judul tingkat dua ditambahkan sebagai item anak dari item tingkat satu sebelumnya, dan sebagainya. Fungsi menetapkan gambar ke item berdasarkan apakah memiliki item turunan. Jika item memiliki item turunan, item tersebut akan mendapatkan gambar yang mewakili folder tertutup. Jika tidak, ia mendapatkan gambar yang mewakili dokumen. Item menggunakan gambar yang sama untuk status yang dipilih dan tidak dipilih.
// 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;
}