当前位置:网站首页>[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); 
边栏推荐
- 避免按钮重复点击的小工具bimianchongfu.queren()
- 明明设计的是高带宽,差点加工成开路?
- Pipeline detection of UAV Based on gazebo
- 公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度
- There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way
- View drawing process analysis
- What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
- Model and view of QT
- It is designed with high bandwidth, which is almost processed into an open circuit?
- Agrometeorological environment monitoring system
猜你喜欢
随机推荐
Huawei machine test questions column subscription Guide
3、Modbus通讯协议详解
Principle and application of single chip microcomputer - principle of parallel IO port
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
Advanced API
Screenshot tips
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
Memory size end
Yolov3, 4, 5 and 6 Summary of target detection
你了解数据是如何存储的吗?(C整型和浮点型两类)
性能提升2-3倍!百度智能云第二代昆仑芯服务器上线
19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
Computer tips
中断与其他函数共享变量、临界资源的保护
1.jetson与摄像头的对接
Serial port to WiFi module communication
一文纵览主流 NFT 市场平台版税、服务费设计
TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
嵌入式工程师常见面试题2-MCU_STM32
Mavros sends a custom topic message to Px4



![Matlab [function derivation]](/img/ba/9fb9da8a458d0c74b29b21a17328fc.png)



![[JS reverse] MD5 encryption parameter cracking](/img/06/0610045d287f646479d6eb5021a067.png)

