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