共用方式為


CTreeCtrl::InsertItem

呼叫此函式將新項目插入樹狀檢視控制項。

HTREEITEM InsertItem(
   LPTVINSERTSTRUCT lpInsertStruct 
);
HTREEITEM InsertItem(
   UINT nMask,
   LPCTSTR lpszItem,
   int nImage,
   int nSelectedImage,
   UINT nState,
   UINT nStateMask,
   LPARAM lParam,
   HTREEITEM hParent,
   HTREEITEM hInsertAfter 
);
HTREEITEM InsertItem(
   LPCTSTR lpszItem,
   HTREEITEM hParent = TVI_ROOT,
   HTREEITEM hInsertAfter = TVI_LAST 
);
HTREEITEM InsertItem(
   LPCTSTR lpszItem,
   int nImage,
   int nSelectedImage,
   HTREEITEM hParent = TVI_ROOT,
   HTREEITEM hInsertAfter = TVI_LAST
);

參數

  • lpInsertStruct
    指定要插入的樹狀檢視項目的屬性設定為 TVINSERTSTRUCT 的指標。

  • nMask
    指定哪些屬性的整數陣列。請參閱在 Windows SDK的 TVITEM 結構。

  • lpszItem
    包含項目的文字字串的位址。

  • nImage
    項目的影像索引在樹狀檢視控制項的影像清單的。

  • nSelectedImage
    項目已選取的影像索引在樹狀檢視控制項的影像清單的。

  • nState
    為項目狀態指定值。以適當的狀態清單中看到樹狀檢視控制項在 Windows SDK 的項目狀態。

  • nStateMask
    指定哪些狀態要設定屬性。請參閱在 Windows SDK的 TVITEM 結構。

  • lParam
    32 位元的應用程式專屬值與項目。

  • hParent
    插入項目之父代的控制代碼。

  • hInsertAfter
    要插入項目,之後新項目的控制代碼。

傳回值

新項目的控制代碼,如果成功,否則 NULL

備註

這個範例顯示您可能想要使用函式的每個版本中,當插入樹狀目錄控制項項目時的情況。

範例

// Insert a root item using the structure. We must
// initialize a TVINSERTSTRUCT structure and pass its
// address to the call. 

TVINSERTSTRUCT tvInsert;
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = _T("United States");

HTREEITEM hCountry = m_TreeCtrl.InsertItem(&tvInsert);

// Insert subitems of that root. Pennsylvania is
// a state in the United States, so its item will be a child
// of the United States item. We won't set any image or states,
// so we supply only the TVIF_TEXT mask flag. This
// override provides nearly complete control over the
// insertion operation without the tedium of initializing
// a structure. If you're going to add lots of items
// to a tree, you might prefer the structure override
// as it affords you a performance win by allowing you
// to initialize some fields of the structure only once,
// outside of your insertion loop.

HTREEITEM hPA = m_TreeCtrl.InsertItem(TVIF_TEXT,
   _T("Pennsylvania"), 0, 0, 0, 0, 0, hCountry, NULL);

// Insert the "Washington" item and assure that it is
// inserted after the "Pennsylvania" item. This override is 
// more appropriate for conveniently inserting items with 
// images.

HTREEITEM hWA = m_TreeCtrl.InsertItem(_T("Washington"),
   0, 0, hCountry, hPA);

// We'll add some cities under each of the states.
// The override used here is most appropriate
// for inserting text-only items.

m_TreeCtrl.InsertItem(_T("Pittsburgh"), hPA, TVI_SORT);
m_TreeCtrl.InsertItem(_T("Harrisburg"), hPA, TVI_SORT);
m_TreeCtrl.InsertItem(_T("Altoona"), hPA, TVI_SORT);

m_TreeCtrl.InsertItem(_T("Seattle"), hWA, TVI_SORT);
m_TreeCtrl.InsertItem(_T("Kalaloch"), hWA, TVI_SORT);
m_TreeCtrl.InsertItem(_T("Yakima"), hWA, TVI_SORT);

需求

Header: afxcmn.h

請參閱

參考

CTreeCtrl 類別

階層架構圖

CTreeCtrl::DeleteItem

CTreeCtrl::HitTest

CTreeCtrl::SelectDropTarget

CTreeCtrl::GetItem

Tree View Control Reference