当前位置:网站首页>[MFC development (16)] tree control
[MFC development (16)] tree control
2022-07-01 08:41:00 【Demo. demo】


1. Property configuration of tree control
(1)Check Boxes : The default is false, If you choose to true Each node will be preceded by a box

(2)Edit Labels: The default is false, If you choose to true The name of each node can be edited and modified

(3)Has Buttons: The default is false, If you choose to true The expanded node will be preceded by a plus or minus sign


(4)Has Lines: The default is false, If you choose to true The following lines will appear


(5) Lines At Root: The default is false, Choose as true If so, it will be wired from the root node


2. Common function methods
(1) Add variables to the tree control

(2) Insert node
// Add a node
// Each node in the tree control has a handle (HTREEITEM),
// When inserting nodes at the same time, the parent node handle must be provided ,
//( One of them Root There is only one node , You can neither add nor delete )
// utilize HTREEITEM InsertItem(LPCTSTR lpszItem, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST);
// You can insert a node ,pszItem For the displayed characters ,
//hParent Handle representing the parent node , The currently added nodes will be arranged in hInsertAfter After the node represented by , The return value is the handle of the currently created node .
hroot = m_Tree.InsertItem(_T(" father "));
HTREEITEM hroot_sub1 = m_Tree.InsertItem(_T(" son 1"), hroot);
HTREEITEM hroot_sub2 = m_Tree.InsertItem(_T(" son 2"), hroot);
HTREEITEM hroot_sub3 = m_Tree.InsertItem(_T(" son 3"), hroot);
HTREEITEM hroot_sub1_sub1 = m_Tree.InsertItem(_T(" Grandson 1-1"), hroot_sub1);
HTREEITEM hroot_sub1_sub2 = m_Tree.InsertItem(_T(" Grandson 1-2"), hroot_sub1);
HTREEITEM hroot_sub3_sub1 = m_Tree.InsertItem(_T(" Grandson 3-1"), hroot_sub3);
HTREEITEM hroot_sub3_sub2 = m_Tree.InsertItem(_T(" Grandson 3-2"), hroot_sub3);
(3) Get the text content of the node
// Get the text content of the node
CString hroot_text = m_Tree.GetItemText(hroot);
MessageBox(hroot_text);(4) Set the text content of the node
// Set the text content of the node
m_Tree.SetItemText(hroot_sub1_sub1, _T(" My name is grandson "));
(5) Get the currently selected node
// Get the selected node
HTREEITEM hselected = m_Tree.GetSelectedItem();
// Get the text content on the selected node
CString hselected_text = m_Tree.GetItemText(hselected);
MessageBox(hselected_text);(6) Modify the currently selected node
// focusing
m_Tree.SetFocus();
// Modify the selected node
m_Tree.SelectItem(hroot);(7) Delete node
// Delete a node , Delete according to the handle
// If the deleted node has child nodes , Will also be deleted
m_Tree.DeleteItem(hroot_sub3);
// Delete all nodes
m_Tree.DeleteAllItems();3. Traverse the tree control
By default, the above cases are all for single selection , that Tree The tree control does not support multiple selection ? The answer is : Not supported by default
However, you can add CheckBox Check box to resolve !
Create a new member function to write a recursive function to traverse the tree control
// Tree recursive function
void CMFC_Test1Dlg::EnumTreeItem(HTREEITEM hitem){
if (!hitem) return;
// If the current node is checked
if (m_Tree.GetCheck(hitem)){
// Get the text content of the node
CString hitem_text = m_Tree.GetItemText(hitem);
MessageBox(hitem_text);
}
// Get child node
HTREEITEM hitem_child = NULL;
if (m_Tree.ItemHasChildren(hitem)){
// Get child nodes
hitem_child = m_Tree.GetChildItem(hitem);
// If child nodes continue to exist
do
{
// Child nodes enter recursion
EnumTreeItem(hitem_child);
// Find the sibling of the child node , If it is empty, it will jump out
hitem_child = m_Tree.GetNextItem(hitem_child, TVGN_NEXT);;
} while (hitem_child);
}
}
4. Fully unfolded / Merge all nodes
Add code
Add one Button Control to control expansion and merging
void CMFC_Test1Dlg::OnBnClickedButton20()
{
// TODO: Add control notification handler code here
static UINT opcode = TVE_EXPAND;
// Extend parent node
ExpendTreeItem(hroot, opcode);
// If it is expanded, it is set to merge
opcode = (opcode == TVE_EXPAND) ? TVE_COLLAPSE : TVE_EXPAND;
}Merge and expand functions recursively
void CMFC_Test1Dlg::ExpendTreeItem(HTREEITEM hitem, UINT opcode){
if (!hitem) return;
// Expand
m_Tree.Expand(hitem, opcode);
// If the current node has children
if (m_Tree.ItemHasChildren(hitem)){
// Get child node
HTREEITEM hchild = m_Tree.GetChildItem(hitem);
while (hchild){
// Expand or merge child nodes
ExpendTreeItem(hchild, opcode);
// When the child node is finished, go to the adjacent node
hchild = m_Tree.GetNextItem(hchild,TVGN_NEXT);
}
}
}

5. How to edit the text content of a node and save changes
(1) Set the... Of the tree control label Can edit

(2) Add variable event
Start editing events

End editing event

(3) Code completion
// Start editing labels
void CMFC_Test1Dlg::OnTvnBeginlabeleditTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTVDISPINFO pTVDispInfo = reinterpret_cast<LPNMTVDISPINFO>(pNMHDR);
// TODO: Add control notification handler code here
// Set the length of characters that can only be entered
m_Tree.GetEditControl()->LimitText(5);
*pResult = 0;// If this parameter =1, Cannot edit
}
// End editing label
void CMFC_Test1Dlg::OnTvnEndlabeleditTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTVDISPINFO pTVDispInfo = reinterpret_cast<LPNMTVDISPINFO>(pNMHDR);
// TODO: Add control notification handler code here
// If the content of the node is not empty and the length is greater than 0
if (pTVDispInfo->item.pszText && _tcslen(pTVDispInfo->item.pszText)>0){
// Set the text content of the node
m_Tree.SetItemText(pTVDispInfo->item.hItem, pTVDispInfo->item.pszText);
}
*pResult = 0;
}
6. Insert node with icon
You can add icons in front of each node , When selected and unchecked, different icons are displayed
(1) Add resources
Several of the ico Add icon files to project resources ,ID Respectively :IDI_ICON1、IDI_ICON2、IDI_ICON3

Import resources into the project resource clock

(2) Add variables and complete the code
Add a picture resource variable CImageList
// Define the image resource variable
CImageList m_imglist;Create image resources and bind them to tree control variables
// Create resources , The parameters are long 、 wide 、 sign 、 Initialize several 、 growth
m_imglist.Create(16,16,ILC_MASK|ILC_COLOR32,2,1);
// Add resources
m_imglist.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
m_imglist.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
m_imglist.Add(AfxGetApp()->LoadIcon(IDI_ICON3));
// Tree controls are associated with picture resources , The type of icon
m_Tree.SetImageList(&m_imglist,TVSIL_NORMAL);Set the selected and unselected icon sequence numbers of nodes
You can set the icon when inserting the node
// The parameter is the name of the node at a time , Icon sequence number when selected , Icon sequence number when not selected
hroot = m_Tree.InsertItem(_T(" father "),0,1);
HTREEITEM hroot_sub1 = m_Tree.InsertItem(_T(" son 1"),0,1, hroot);
HTREEITEM hroot_sub2 = m_Tree.InsertItem(_T(" son 2"), 0, 1, hroot); 
边栏推荐
- Nacos - 服务发现
- 分享2022上半年我读过的7本书
- 2022 examination summary of quality controller civil engineering direction post skills (quality controller) and reexamination examination of quality controller civil engineering direction post skills
- TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
- Agrometeorological environment monitoring system
- 个人装修笔记
- 《单片机原理及应用》-片外拓展
- Audio audiorecord create (I)
- factory type_id::create过程解析
- 2022 ordinary scaffolder (special type of construction work) examination question bank and the latest analysis of ordinary scaffolder (special type of construction work)
猜你喜欢

Advanced C language pointer (Part 2)

What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?

It is designed with high bandwidth, which is almost processed into an open circuit?

《单片机原理与应用》——并行IO口原理

《微机原理》—总线及其形成

MAVROS发送自定义话题消息给PX4

FreeRTOS学习简易笔记

AES简单介绍

Pipeline detection of UAV Based on gazebo

Do you know how data is stored? (C integer and floating point)
随机推荐
个人装修笔记
长安链同步节点配置与启动
中考体育项目满分标准(深圳、安徽、湖北)
5mo3 UHI HII HII 17mn4 19Mn6 executive standard
分享2022上半年我读过的7本书
SPL installation and basic use (II)
我想知道手机注册股票开户的流程?另外,手机开户安全么?
Leetcode t39: combined sum
Advanced API
Memory size end
Foundation: 2 The essence of image
一文纵览主流 NFT 市场平台版税、服务费设计
Leetcode t31: next spread
How to recruit Taobao anchor suitable for your own store
Only in China! Alicloud container service enters the Forrester leader quadrant
电视机尺寸与观看距离
Introduction to R language
Intelligent water supply system solution
中断与其他函数共享变量、临界资源的保护
Intelligent water and fertilizer integrated control system