当前位置:网站首页>[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);
边栏推荐
- 动态代理
- 【面试必刷101】链表
- V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
- Introduction to R language
- MATLAB小技巧(16)矩阵特征向量特征值求解一致性验证--层次分析
- 中断与其他函数共享变量、临界资源的保护
- 2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
- Screenshot tips
- 【C】 Summary of wrong questions in winter vacation
- Model and view of QT
猜你喜欢
TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
内存大小端
Only in China! Alicloud container service enters the Forrester leader quadrant
【C】 Summary of wrong questions in winter vacation
Screenshot tips
Brief introduction to AES
截图小妙招
Intelligent water supply system solution
嵌入式工程师面试题3-硬件
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
随机推荐
The data analyst will be ruined without project experience. These 8 project resources will not be taken away
"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis
[深度剖析C语言] —— 数据在内存中的存储
Qt的模型与视图
任务、线程、进程 区别
《微机原理》——微处理器内部及外部结构
MySQL8.0学习记录17 -Create Table
Internet of things technology is widely used to promote intelligent water automation management
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
截图小妙招
factory type_ Id:: create process resolution
Matlab [function derivation]
Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
Agrometeorological environment monitoring system
明明设计的是高带宽,差点加工成开路?
Intelligent water supply system solution
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
1. Connection between Jetson and camera
Model and view of QT
VSYNC+三重缓存机制+Choreographer