多选树形控件






2.87/5 (7投票s)
这个树形控件允许用户选择多个树形项目,并启用通过矩形框选择的功能。
引言
这个树形控件是一个多选树形控件。它允许用户通过矩形框选择项目。虽然网络上已经发布了很多多选树形控件,但我尝试制作一个易于使用的控件。
使用代码
用户可以像使用 CTreeCtrl
一样简单地使用它。CTreeCtrl
可以被 CCustomTreeCtrl
替换。这允许一些方法来改变树形控件的样式,例如 AddLineStyle()
、AddLineAtRoot()
等。它具有一个回调机制,在循环遍历项目时调用该函数。为了循环遍历项目,提供了一个特殊的函数,即 IterateItems()
。该函数将回调函数作为其参数之一。用户可以指定循环的起始和结束项目。
/****************************************************************************
/DESCRIPTION:
/-----------
/ To iterate all the items and call the callback function for each item
/
/PARAM
/------
/ func[in] - Call back function to be called on each item
/ hStart[in] - Start node for the iteration
/ hEnd[in] - End node to stop the iteration
/ pInfo[in] - User parameter
/
/RESULT:
/-------
/ void
*/
void CCustomTreeCtrl::IterateItems( ScanCallBackFunc func,
HTREEITEM hStart /*= NULL*/,
HTREEITEM hEnd, /*= NULL*/
void* pInfo /*=NULL*/
)
{
//If there is no start then take the root item
if( !hStart )
hStart = GetRootItem();
ScanItems( func,hStart,hEnd,pInfo );
}
/***************************************************************************
/DESCRIPTION:
/-----------
/ To iterate all the items and call the callback function for each item
/
/PARAM
/------
/ func[in] - Call back function to be called on each item
/ hStart[in] - Start node for the iteration
/ hEnd[in] - End node to stop the iteration
/ pInfo[in] - User parameter
/
/RESULT:
/-------
/ void
*/
void CCustomTreeCtrl::ScanItems( ScanCallBackFunc func,
HTREEITEM hStart /*= NULL*/,
HTREEITEM hEnd, /*= NULL*/
void* pInfo /*= NULL*/
)
{
//Loop from start till the end is reached
while( hStart )
{
LOOPINFO lInfo;
lInfo.pTree = this;
lInfo.hItem = hStart;
lInfo.pParent = GetParent();
lInfo.param = pInfo;
CRect rc;
GetItemRect( hStart,&rc,FALSE );
//Callback is called for the current item
func( &lInfo );
//Check whether we have reached iterating upto the end node
if( hStart == hEnd )
return;
//Get the childern of the current item and call recursively
HTREEITEM hChild = NULL;
if( ItemHasChildren( hStart ) )
{
hChild = GetChildItem( hStart );
ScanItems( func,hChild,hEnd,pInfo);
}
hStart = GetNextSiblingItem( hStart );
}
}
关注点
在定制树形控件的过程中,我学到了很多新东西,例如如何在进行多选时创建拖动图像。我从 CodeProject 文章中获取了一些示例代码来完成这项工作。但我认为我已经使多选变得非常简单,并且我提供了大量的注释,这将非常有帮助地理解代码。
历史
这是代码的第一个版本。根据用户的反馈和错误报告,我将更新代码并努力提供最好的版本。请随时发送您的反馈。