0
点赞
收藏
分享

微信扫一扫

CTreeCtrl使用演示

小贴贴纸happy 2022-12-07 阅读 105


说明

以下只记录了使用的大概步骤,均是查阅MSDN得出或翻译出的结论,要想了解更多,还是自行去查阅MSDN吧。

步骤

  • 构造CTreeCtrl对象
  • 使用CTreeCtrl::Create创建对象
  • 需要在节点处显示图片的话,就用CTreeCtrl::SetImageList设置下图片资源
  • 使用CTreeCtrl::InsertItem插入节点数据
  • 使用ON_NOTIFY_REFLECT或 ON_NOTIFY 宏去添加CTreeCtrl控件的事件响应(ON_NOTIFY宏是用在CTreeCtrl控件父窗口中,而ON_NOTIFY_REFLECT则是在CTreeCtrl本身的通知,所以要使用此宏,需要从CTreeCtrl继承一个类下来)

MSDN原文教程

Typical usage of a tree control (CTreeCtrl) follows the pattern below:

  • The control is created. If the control is specified in a dialog box template or if you’re using CTreeView, creation is automatic when the
    dialog box or view is created. If you want to create the tree control
    as a child window of some other window, use theCreate member
    function.
  • If you want your tree control to use images, set an image list by callingSetImageList. You can also change the indentation by
    callingSetIndent. A good time to do this is inOnInitDialog (for
    controls in dialog boxes) orOnInitialUpdate (for views).
  • Put data into the control by calling the CTreeCtrl’sInsertItem function once for each data item. InsertItem returns a handle to the
    item you can use to refer to it later, such as when adding child
    items. A good time to initialize the data is in OnInitDialog (for
    controls in dialog boxes) or OnInitialUpdate (for views).
  • As the user interacts with the control, it will send various notification messages. You can specify a function to handle each of
    the messages you want to handle by adding an ON_NOTIFY_REFLECT macro
    in your control window’s message map or by adding an ON_NOTIFY macro
    to your parent window’s message map. See Tree Control Notification
    Messages later in this article for a list of possible notifications.
  • Call the various Set member functions to set values for the control. Changes that you can make include setting the indentation
    and changing the text, image, or data associated with an item.
  • Use the various Get functions to examine the contents of the control. You can also traverse the contents of the tree control
    with functions that allow you to retrieve handles to parents,
    children, and siblings of a specified item. You can even sort the
    children of a particular node.
  • When you’re done with the control, make sure it’s properly destroyed. If the tree control is in a dialog box or if it’s a
    view, it and the CTreeCtrl object will be destroyed automatically.
    If not, you need to ensure that both the control and the CTreeCtrl
    object are properly destroyed.

代码示例

void CTreeCtrlDemoDlg::InitTreeCtrl()
{
CRect clientRect;
GetClientRect( clientRect );

//创建树型控件
m_TreeCtrl.Create(
WS_VISIBLE | WS_BORDER | TVS_LINESATROOT |
TVS_HASLINES | TVS_HASBUTTONS,
clientRect,
this, 1000 ); //最后一个参数为控件id,这里演示就写死一个数据吧


//设置图片(不需要图片的,可屏蔽这两行代码)
m_ImageList.Create( IDB_TREE_CTRL_IMAGE, 20, 0, RGB(255, 255, 255) );
m_TreeCtrl.SetImageList( &m_ImageList, TVSIL_NORMAL );


//以下是从文件中读取树型结构的数据进入初始化控件数据
//主要是为了方便演示(用文件目录系统来演示也是个不错的选择)
FILE *fStream = NULL;
errno_t e = _tfopen_s(&fStream,
_T("tree_menu_utf8.txt"), _T("rt,ccs=UTF-8"));
if (e != 0 )
{
TRACE(_T("failed to open file for tree ctrl initialize"));
return;
}
CStdioFile tree_file(fStream);
try
{
CString strLine;
CArray<HTREEITEM, HTREEITEM> hItemStack;
hItemStack.Add( TVI_ROOT );
HTREEITEM hLastAddItem = NULL;
while( tree_file.ReadString( strLine ) && !hItemStack.IsEmpty() )
{
strLine.TrimRight();
CString strTabs = strLine.SpanIncluding( _T("\t") );
INT_PTR nTemp = hItemStack.GetSize() - 1;
int nImageIndex = strTabs.GetLength();
if ( nTemp < strTabs.GetLength() )
{
//进入下一层
ASSERT( hLastAddItem != NULL );
if ( hLastAddItem != NULL )
{
hItemStack.Add( hLastAddItem );
}

}
else if ( nTemp == strTabs.GetLength() )
{
//继续当前层
}
else
{
int nPopCnt = int(nTemp - strTabs.GetLength());
while( nPopCnt-- )
{
//回退一层
hItemStack.RemoveAt( hItemStack.GetCount() - 1 );
}
}


//获取当前父结点
ASSERT( !hItemStack.IsEmpty() );
if ( hItemStack.IsEmpty() )
{
break;
}
HTREEITEM hParent = hItemStack.GetAt( hItemStack.GetCount() - 1 );

//添加新项
strLine.TrimLeft();
HTREEITEM hNewItem = m_TreeCtrl.InsertItem( strLine,
nImageIndex, nImageIndex, hParent );
hLastAddItem = hNewItem;
}
}
catch (CException* e)
{
e->ReportError();
}

//展开全部节点
ExpandTreeItem( m_TreeCtrl.GetRootItem() );
}


void CTreeCtrlDemoDlg::ExpandTreeItem( HTREEITEM hItem )
{
if ( hItem == NULL )
{
return;
}

CString strText = m_TreeCtrl.GetItemText(hItem);

// 处理孩子节点
if ( m_TreeCtrl.ItemHasChildren( hItem ) )
{
m_TreeCtrl.Expand( hItem, TVE_EXPAND );
HTREEITEM hChildItem = m_TreeCtrl.GetChildItem( hItem );
ExpandTreeItem( hChildItem );
}

// 处理兄弟节点
HTREEITEM hSiblingItem = m_TreeCtrl.GetNextSiblingItem( hItem );
// while( hSiblingItem != NULL )
// {
// ExpandTreeItem( hSiblingItem );
// hSiblingItem = m_TreeCtrl.GetNextSiblingItem( hSiblingItem );
// }
if ( hSiblingItem != NULL )
{
ExpandTreeItem( hSiblingItem );
}
}

代码说明

  1. 以上初始化树型控件用到一份文件,文件是utf8格式的,以tab键来标识各级层次结构

中国
华南地区
广东
广西
海南
东北地区
辽宁
吉林
黑龙江
游戏
页游
页游1
页游2
页游21
页游211
页游2111
页游22
手游
端游
端游1
端游2

  1. 代码中用到一张位图,位图是120*20大小的,包含6张20*20大小的图片

运行截图

CTreeCtrl使用演示_控件


举报

相关推荐

0 条评论